2017-10-18 3 views
0

처리 알림에 사용하는 간단한 클래스가 있습니다.다중 바인딩을위한 Ninject 생성자 인수

public class ApplePushNotifier : IApplePushNotifier 
{ 
    public ApplePushNotifier(
     ILog log, 
     IMessageRepository messageRepository, 
     IUserRepository userRepository, 
     CloudStorageAccount account, 
     string certPath) 
    { 
     // yadda 
    } 

    // yadda 
} 

그리고 로컬 인증서 파일을 찾을 문자열 인수 포함 바인딩 간단한 Ninject에, :

kernel.Bind<IApplePushNotifier>().To<ApplePushNotifier>() 
      .WithConstructorArgument("certPath", 
       System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12")); 

이 분명 아주 기본적인, 그리고 모든 위대한 일을했습니다.

public class ApplePushNotifier : IApplePushNotifier, IMessageProcessor 

내가 두 번째 같은 바인딩을 추가 할 수 있습니다 :

kernel.Bind<IMessageProcessor>().To<ApplePushNotifier>() 
      .WithConstructorArgument("certPath", 
       System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12")); 

을 그리고도 작동하지만 중복 생성자 인수가 나에게 두드러기를 제공 지금, 나는 그 클래스에 두 번째 인터페이스를 추가했습니다. 다음과 같이 명시 적 자체 바인딩을 추가해 보았습니다.

하지만 주사위가 없습니다. "일치하는 바인딩 없음"오류가 발생합니다.

바인더 블 유형으로 자체를 승격하거나 클래스가 구현하는 모든 인터페이스에 대해 반복하지 않고 이와 같이 생성자 인수를 지정하는 방법이 있습니까?

답변

0

그냥 하나의 두 서비스 인터페이스에 대한 바인딩을 만들 :

kernel.Bind<IApplePushNotifier, IMessageProcessor>().To<ApplePushNotifier>() 
    .WithConstructorArgument(
     "certPath", System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12")) 
+0

거의 너무 쉽게! 이 문제를 처리하는 간단한 방법이 있어야한다는 것을 알고있었습니다. 감사합니다! – superstator

0

ApplePushNotifier의 내부 동작 특성에 따라 상수에 바인딩하면 도움이 될 수 있으며 직접 반복하지 않을 수 있습니다.

kernel.Bind<ApplePushNotifier>().ToSelf() 
     .WithConstructorArgument("certPath", System.Web.Hosting.HostingEnvironment.MapPath("~/bin/apns_universal.p12")); 

    var applePushNotifier = Kernel.Get<ApplePushNotifier>(); 

    kernel.Bind<IApplePushNotifier>().ToConstant(applePushNotifier); 
    kernel.Bind<IMessageProcessor>().ToConstant(applePushNotifier); 

희망이 있습니다.