2011-08-28 77 views
4

我正在嘗試註冊所有實現與溫莎接口IProcess<T1, T2>的類。要做到這一點我在安裝下面的代碼:爲什麼我不能在Windsor註冊多個接口的一個類?

 // Register all implemented process interfaces 
     var procTypes = AppDomain.CurrentDomain 
           .GetAssemblies() 
           .SelectMany(x => x.GetTypes()) 
           .Where(x => x.IsDerivedFromOpenGenericType(typeof(IProcess<,>))) 
           .ToList(); 

     foreach (var procType in procTypes) 
      foreach (var procInterface in procType.GetInterfaces().Where(x => x.IsDerivedFromOpenGenericType(typeof(IProcess<,>)))) 
       container.Register(Component.For(procInterface).ImplementedBy(procType).LifeStyle.Transient); 

一類我一個嘗試註冊的是:

public class PositionProcesses 
    : IProcess<CreatePositionParams, PositionDisplayViewModel>, 
     IProcess<EditPositionParams, PositionDisplayViewModel> 
{ 
} 

第一接口被正確註冊,但在註冊第二個接口由這個類來實現,我收到以下錯誤:

Test method MyJobLeads.Tests.Controllers.PositionControllerTests.Windsor_Can_Resolve_PositionController_Dependencies threw exception: 
Castle.MicroKernel.ComponentRegistrationException: There is a component already registered for the given key MyJobLeads.DomainModel.Processes.Positions.PositionProcesses 

第一循環迭代我的變量是:

+  procInterface {Name = "IProcess`2" FullName = "MyJobLeads.DomainModel.Data.IProcess`2[[MyJobLeads.DomainModel.ProcessParams.Positions.CreatePositionParams, MyJobLeads.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[MyJobLeads.DomainModel.ViewModels.Positions.PositionDisplayViewModel, MyJobLeads.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"} System.Type {System.RuntimeType} 
+  procType {Name = "PositionProcesses" FullName = "MyJobLeads.DomainModel.Processes.Positions.PositionProcesses"} System.Type {System.RuntimeType} 

在第二:

+  procInterface {Name = "IProcess`2" FullName = "MyJobLeads.DomainModel.Data.IProcess`2[[MyJobLeads.DomainModel.ProcessParams.Positions.EditPositionParams, MyJobLeads.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[MyJobLeads.DomainModel.ViewModels.Positions.PositionDisplayViewModel, MyJobLeads.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"} System.Type {System.RuntimeType} 
+  procType {Name = "PositionProcesses" FullName = "MyJobLeads.DomainModel.Processes.Positions.PositionProcesses"} System.Type {System.RuntimeType} 

(這兩個都來自VS調試器。

任何想法?

回答

4

你應該使用常規的基於組件註冊

BasedOnDescriptor processes = AllTypes.FromAssembly(assemblyWithProcesses) 
    .BasedOn(typeof (IProcess<,>)) 
    .WithService.AllInterfaces() 
    .Configure(x => x.LifeStyle.Transient); 

container.Register(processes) 

如上面提到的編輯取出第一個樣品@剋日什托夫·-kozmic

+0

啊我沒有注意到覆蓋。工作就像一個魅力,謝謝:) – KallDrexx

+0

我不得不重讀這個幾次,以瞭解從OP的代碼的差異。您一次將接口數組全部傳遞給'For',而不是一次傳遞一個接口。我會留下我的回答,但我更喜歡這個,因爲溫莎處理你的命名:) –

+0

添加基於約定的註冊示例 – hazzik

2

如果你有一個組件註冊爲多個服務,我想你」必須手動命名每個註冊。

參見:http://docs.castleproject.org/Windsor.Registering-components-by-conventions.ashx#Configuring_registration_13

container.Register(
    Component.For(procInterface) 
      .ImplementedBy(procType) 
      .LifeStyle.Transient 
      .Named(component.Implementation.FullName 
       + "-" 
       + procInterface.Name) 
    ); 

這應該通過註冊類型的全名你註冊它的接口每個組件。

+0

此選項應該可以工作,但[我更喜歡hazzik的答案](http://stackoverflow.com/questions/7218885/why-can-i-not-register-one-class-for-multiple-interfaces-in-windsor/7218994#7218994)。 –

相關問題