参见英文答案 >
Ninject – how and when to inject2个
我通过从NinjectHttpApplication派生Global并在web.config中使用NinjectHttpModule,在我的Web应用程序中成功使用Ninject @H_301_2@我现在要做的是在我的一个类库中使用DI,我不知道如何去做.我有以下虚拟课程:
我通过从NinjectHttpApplication派生Global并在web.config中使用NinjectHttpModule,在我的Web应用程序中成功使用Ninject @H_301_2@我现在要做的是在我的一个类库中使用DI,我不知道如何去做.我有以下虚拟课程:
/// <summary> /// Testing Ninject DI in a class library /// </summary> public class Class1 { [Inject] ICustomerRepository CustomerRepository { get; set; } public string SomeText { get; set; } public Class1(string text) { MyConfig config = new MyConfig(); config.Configure(); SomeText = text; } public Customer GetCustomer() { var customer = CustomerRepository.GetCustomer(); return customer; } } public class MyConfig { public IKernel Configure() { IKernel kernel = new StandardKernel(new NinjectRepositoryModule()); return kernel; } }@H_301_2@当我实例化Class1并调用GetCustomer()时,CustomerRepository为null,所以我显然做错了. @H_301_2@此外,如果我使用构造函数注入并具有我的构造函数
public Class1([Inject] ICustomerRepository repository)@H_301_2@我将如何实例化Class1? @H_301_2@Ninject相当新,所以这可能都很容易.
看起来我已经知道怎么做了 – 哎呀:)
@H_301_2@Ninject – how and when to inject