2013-10-26 3 views
3

Unity를 사용 중입니다. 정규 오픈 타입을 등록 할 수 있습니다. 그러나이 경우 인터페이스에는 내부에 열려있는 일반 중첩 된 한 단계가 있습니다.Unity에 중첩 된 일반 일반 유형 등록

Unity와 함께 이런 종류의 것을 등록 할 방법이 있습니까?

class DoSomethingCommandHandler<TModel> : ICommandHandler<DoSomethingCommand<TModel>> 
{ 
    public void Handle(DoSomethingCommand<TModel> cmd) 
    { 
     var model = cmd.Model; 
     //do thing with model 
    } 
} 

class QuickTest 
{ 
    static void Go() 
    { 
     var container = new UnityContainer(); 
     container.RegisterType(
      typeof(ICommandHandler<>).MakeGenericType(typeof(DoSomethingCommand<>)), 
      typeof(DoSomethingCommandHandler<>)); 

     //This blows up: 
     var res = container.Resolve<ICommandHandler<DoSomethingCommand<object>>>(); 
    } 
} 
+0

어쩌면 내가 부족 뭔가 :

container.RegisterType( typeof(IDoSomethingCommandCommandHandler<>), typeof(DoSomethingCommandHandler<>)); var res = container.Resolve<IDoSomethingCommandCommandHandler<object>>(); 

또는 명시 적으로 모든 중첩 된 형식에 대한 등록

당신은 다른 인터페이스를 구현할 필요 (예 : 몇 가지 여분의 코드)하지만 시도해보십시오 :'ICommandHandler > handler = new DoSo methingCommand ();'컴파일러가 캐스트가 필요하다는 불평을합니다. 그게 기대 되니? –

+0

'DoSomethingCommand'는'ICommandHandler'를 구현하지 않으므로 컴파일러 오류가 예상됩니다. 'DoSomethingCommandHandler'만이'ICommandHandler'를 구현합니다. – Joel

답변

2

샷 대답은 - 그것은 불가능합니다 :) 나는이 문제를 탐구 몇 시간을 소비하는 방법과이 작업을 수행하는 유니티을 말할 수있는 방법을 찾지 못했습니다.

internal interface IDoSomethingCommandCommandHandler<out T> { } 

class DoSomethingCommandHandler<TModel> : ICommandHandler<DoSomethingCommand<TModel>>, IDoSomethingCommandCommandHandler<TModel> 

그리고이 인터페이스에 의해 등록 :

var container = new UnityContainer(); 
container.RegisterType(
      typeof(ICommandHandler<>).MakeGenericType(typeof(DoSomethingCommand<object>)), 
      typeof(DoSomethingCommandHandler<object>)); 
var res = container.Resolve<ICommandHandler<DoSomethingCommand<object>>>();