2011-11-01 29 views
2

即時嘗試獲得毫秒統一注入一個對象到構造函數和我有問題,由於有多個構造函數和相同數量的參數的類。我知道你可以註釋構造函數,但我不想那樣做。我怎樣才能得到統一使用正確的構造毫秒統一和多個構造函數

我的構造就像這樣: -

public JobsHandler(ICentralRepositoryContainer context) 
    public JobsHandler(ICentralRepositoryLifeTimehelper centralRepositoryLifeTimehelper) 

和我使用一些代碼,我發現: -

uContainer.RegisterType<ICentralRepositoryContainer, Entities>().Configure<InjectedMembers>(). 
      ConfigureInjectionFor<JobsHandler>(new InjectionConstructor()); 

但在得到一個

CentralRepository.BusinessLogic.JobsHandler類型沒有 構造函數,它需要t他參數()

我猜這是因爲我還沒有指定在injectionconstructor對象中的參數。我是否正確地做了這件事?

回答

2

我想你會想解決ICentralRepositoryContainer並在InjectionConstructor

uContainer.RegisterType<ICentralRepositoryContainer, Entities>().Configure<InjectedMembers>().ConfigureInjectionFor<JobsHandler>(
    new InjectionConstructor(new ResolvedParameter(typeof(ICentralRepositoryContainer))); 

使用它,或者同時指定構造函數註冊的作業處理程序。

uContainer.RegisterType<ICentralRepositoryContainer, Entities>(); 
uContainer.RegisterType<JobsHandler>(new InjectionConstructor(new ResolvedParameter(typeof(ICentralRepositoryContainer)))); 

請參閱此鏈接上面的一個更完整的例子:

http://msdn.microsoft.com/en-us/library/ff650036.aspx

+0

是不工作。 ConfigureInjectionFor不是容器的成員 –

+0

我的道歉。我的意思是使用RegisterType方法。我已經讚揚了上述內容,並使用您的原始方法添加了一個示例。 P.S.我不能100%確定這是否可行,因爲我目前無法驗證它。但我認爲這大致是你需要做的。 – Balthy