依赖注入 – 在类库中使用Ninject的DI [复制]
发布时间:2020-12-13 20:11:33 所属栏目:百科 来源:网络整理
导读:参见英文答案 Ninject – how and when to inject2个 我通过从NinjectHttpApplication派生Global并在web.config中使用NinjectHttpModule,在我的Web应用程序中成功使用Ninject 我现在要做的是在我的一个类库中使用DI,我不知道如何去做.我有以下虚拟课程: ///
参见英文答案 >
Ninject – how and when to inject2个
我通过从NinjectHttpApplication派生Global并在web.config中使用NinjectHttpModule,在我的Web应用程序中成功使用Ninject 我现在要做的是在我的一个类库中使用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; } } 当我实例化Class1并调用GetCustomer()时,CustomerRepository为null,所以我显然做错了. 此外,如果我使用构造函数注入并具有我的构造函数 public Class1([Inject] ICustomerRepository repository) 我将如何实例化Class1? Ninject相当新,所以这可能都很容易.
看起来我已经知道怎么做了 – 哎呀:)
Ninject – how and when to inject (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |