2012-11-02 2 views
0

이 코드는 작동하지 않습니다. 나는 기본적으로 슈퍼 인터페이스와 서브 인터페이스의 구현을 가지고있다. 수퍼 인터페이스가 모든 구현을 리턴하고 서브 인터페이스가 서브 구현만을 리턴하고 싶습니다. 서브 구현을 슈퍼 인터페이스와 슈퍼 인터페이스에 명시 적으로 바인드하고 싶지 않습니다. 하위 인터페이스에 바인딩하고 어떻게 든 서브를 슈퍼에 바인딩하고 싶습니다. 나는 이런 식으로 설정하기 위해 노력하고있어,하지만 작동하지 않습니다 :Ninject와 하위 인터페이스에 슈퍼 인터페이스 바인딩

class Program 
{ 
    static void Main(string[] args) 
    { 
     IKernel kernel = new StandardKernel(); 
     kernel.Bind<ISuperInterface>().To<ISubInterface>(); // What can I do instead of this? 
     kernel.Bind<ISuperInterface>().To<SuperImplementation>(); 
     kernel.Bind<ISubInterface>().To<SubImplementation>(); 

     var subs = kernel.GetAll<ISubInterface>(); // I want this to return a SubImplementation 
     var supers = kernel.GetAll<ISuperInterface>(); // I want this to return a SuperImplementation and a SubImplementation 

     Console.WriteLine(subs.Count()); 
     Console.WriteLine(supers.Count()); 
    } 
} 

public class SubImplementation : ISubInterface 
{ 
} 

public class SuperImplementation : ISuperInterface 
{ 
} 

public interface ISuperInterface 
{ 
} 

public interface ISubInterface : ISuperInterface 
{ 
} 

답변

0

해서 Ninject는 인터페이스 seggregation 지원 : 당신이 규칙을 사용하는 경우

kernel.Bind<ISuperInterface>().To<SuperImplementation>(); 
kernel.Bind<ISuperInterface, ISubInterface>().To<SubImplementation>(); 

또는 당신은 사용할 수 BindAlInterfaces

관련 문제