7
我成功地利用Ninject在我的web应用程序通过派生我从全局和
NinjectHttpApplication
在我的web.config使用
NinjectHttpModule
我现在想做一个正在使用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
为空,所以我明明做错事。
另外,如果我使用构造器注入,有我喜欢的构造
public Class1([Inject] ICustomerRepository repository)
我将如何去实例Class1
?
相当新的Ninject,所以这可能都是非常容易的东西。
现在,很有趣 – GONeale 2013-03-29 03:17:32