依赖注入 – 在类库中使用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

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...