DI

2009-09-25 9 views
7

私は正常に1でDIを使用している私は今、何をしたい私のweb.configファイルにNinjectHttpApplicationから私の世界を導出し、NinjectHttpModuleを使用して自分のWebアプリケーションにDI

をNinjectを使用しています私のクラス図書館の私はこれについてどうやって行くのかわかりません。私は、次のダミークラスを持っている:私はClass1をインスタンス化し、GetCustomer()を呼び出すと

/// <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; 
    } 
} 

CustomerRepositoryはnullであるので、私は明らかに間違って何かをやっています。また

、私はコンストラクタ・インジェクションを使用して、私はClass1をインスタンス化については行くだろうか

public Class1([Inject] ICustomerRepository repository) 

のように私のコンストラクタを持っている場合は?

Ninjectには全く新しいので、これはすべて非常に簡単なものになる可能性があります。

答えて