我是一个依赖注入的菜鸟,我按照教程并重构了我的代码以使用Ninject。这就是我所拥有的(简化)。 Bindings.cs
public class Bindings : NinjectModule
{
public override void Load()
{
Bind<IConnectionToModbusCreator>().To<ConnectionToModbusCreator>();
Bind<IInputProcessor>().To<InputProcessor>();
}}
Program.cs;
static class Program
{
[STAThread]
static void Main()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
var InputProcessor = kernel.Get<IInputProcessor>();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(InputProcessor));
}
这个启动一个表单,用户通过单击按钮启动程序,这里是Form1:
public partial class Form1 : Form
{
private readonly IInputProcessor _inputProcessor;
public Form1(IInputProcessor inputProcessor)
{ _inputProcessor = inputProcessor;
InitializeComponent();
}
private void Test_Click_1(object sender, EventArgs e)
{
ISequenceController sequenceController = new SequenceController( _inputProcessor)
sequenceController.StartSequence();
}
我的问题是:
这是最好的方法吗?我担心我必须在构造函数中传递的所有引用,我试图使用一个类Container,它保存变量与实例化的对象并将它们用作属性但我无法在任何Ninject手册中找到sich方法。
答案 0 :(得分:0)
您的程序遵循构造函数注入DI模式,这是一种很好的做法,但它有一些问题会引入模块之间的耦合。拨打new
的大量电话会提醒您。
Program
不应该了解IInputProcessor
。这是Form1
Form1
不应该了解IInputProcessor
。它是SequenceController
Form1
不应该知道SequenceController
,它应该只知道它的抽象,ISequenceController
这可以通过更好地使用构造函数注入来获得更少的耦合代码来解决:
public class Bindings : NinjectModule
{
public override void Load()
{
Bind<IConnectionToModbusCreator>().To<ConnectionToModbusCreator>();
Bind<IInputProcessor>().To<InputProcessor>();
Bind<ISequenceController>().To<SequenceController>();
Bind<Form1>().ToSelf(); // in fact useless unless you add a scope, like .InSingletonScope()
}
}
static class Program
{
[STAThread]
static void Main()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form1= kernel.Get<Form1>();
Application.Run(form1);
}
public partial class Form1 : Form
{
private readonly ISequenceController _sequenceController;
public Form1(ISequenceController sequenceController)
{
_sequenceController = sequenceController;
InitializeComponent();
}
private void Test_Click_1(object sender, EventArgs e)
{
_sequenceController.StartSequence();
}