2010-07-05 97 views
0

如何使用Ninject與樣本代碼接口及其實現這樣的:如何在Ninject中使用非默認的構造函數?

public interface IRepository 
{ 
    // common methods for all content types 
    void Insert(BaseContentObject o); 
    void Update(BaseContentObject o); 
    void Delete(string slug); 
    void Delete(ContentType contentType, string slug); 
    IEnumerable<BaseContentObject> GetInstances(); 
    BaseContentObject GetInstance(ContentType contentType, string slug); 
    BaseContentObject GetInstance(string contentType, string slug); 
    BaseContentObject GetInstance(string slug); 
    IEnumerable<string> GetSlugsForContentType(int limit = 0, string query = ""); 
    ContentList GetContentItems(); 
    bool IsUniqueSlug(string slug); 
    string ObjectPersistanceFolder { get; set; } 
} 

public class XmlDefaultRepository : IRepository 
{ 
    private ContentType SelectedType; 

    public XmlDefaultRepository(string contentType) 
    { 
     SelectedType = (ContentType) Enum.Parse(typeof(ContentType), contentType); 
    } 

    public void Insert(Models.ContentClasses.BaseContentObject o) 
    { 
     throw new NotImplementedException(); 
    } 

    // ... 
} 

public class PageController : BaseController 
{ 
    private IRepository ContentRepository { get; private set; } 

    public PageController(IRepository repository) 
    { 
     ContentRepository = repository; 
    } 

    // 
    // GET: /{slug} 
    // GET: /Pages/{slug} 
    public ActionResult Display(string slug) 
    { 
     // sample repository usage 
     Page page = ContentRepository.GetInstance(slug); 
     // ... 
    } 
} 

我的代碼不包含默認構造函數,因爲我並不需要一個(即使想創造它,我不能因爲我總是要求的內容類型來提供。

我不能讓一個默認的構造函數,因爲沒有邏輯上不提供默認的內容類型。

這是Ninject產生試圖加載我的ASP當異常.NET MVC頁面。

*Error activating string

No matching bindings are available, and the type is not self-bindable.

Activation path:

  1. Injection of dependency string into parameter contentType of constructor of type XmlDefaultRepository
  2. Injection of dependency IRepository into parameter repository of constructor of type PageController
  3. Request for IController

Suggestions:

  1. Ensure that you have defined a binding for string.
  2. If the binding was defined in a module, ensure that the module has been loaded into the kernel.
  3. Ensure you have not accidentally created more than one kernel.
  4. If you are using automatic module loading, ensure the search path and filters are correct.*

回答

3

你不應該需要引入一個默認的ctor來取悅Ninject。

假設你正在使用V2,the semantics for how a constructor is chosen are detailed here

(BTW默認情況下,string型不被視爲一個解析式開箱即用,但你應該能夠BindContentTypeTo的東西,有一個構造函數調用)。

如果以上都沒有讓你感動,請你提供一個使用場景和/或一個例外,詳細說明你遇到的問題。

編輯:這不是很清楚,我在這一點上,你在一個情況下,你不應該被添加ConstructorArgument((很可能是通過WithConstructorArgument() IIRC)在您的Bind<T>()聲明

+0

我已經更新了我的代碼是肯定。並提供Ninject的例外 – mare 2010-07-06 12:56:08

+0

如果我繼續上面的代碼,並在我的Ninject模塊中執行此操作 綁定().To ()。WithConstructorArgument(「contentType」,「Page」); 然後它工作,但是事情是,我無法將我的contentType構造函數參數綁定到我的Module中的「Page」中,這個構造函數參數與控制器有關,所以在我的PageController中,XmlDefaultRepository會使用contentType的「Page」值實例化,但在其他控制器(如ServiceController)中,「contentType」參數值應等於「Service」。 – mare 2010-07-06 14:25:52

+0

希望你明白我不能硬編碼模塊中「contentType」參數的值。我怎麼解決這個問題?控制器工廠等等是什麼? – mare 2010-07-06 14:27:18

相關問題