2017-10-18 2 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")); 

これは明らかに、かなり基本的なもの、すべてが素晴らしい仕事をしています。

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

そして、それはあまりにも動作しますが、重複コンストラクタ引数が私に巣箱を与える:私はこのようなバインディング秒を追加することができます

public class ApplePushNotifier : IApplePushNotifier, IMessageProcessor 

:今、私は、そのクラスへの第2のインタフェースを追加しました。私はこのような明示的な自己結合の追加を試みた:

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

しかし、無サイコロを - 私は古い「該当バインディングが用意されていません」というエラーが表示されます。

コンストラクタの引数を独自のバインド可能な型に昇格するか、またはクラスが実装するすべてのインターフェイスで繰り返さずに、このようなコンストラクタ引数を指定する方法はありますか?

答えて

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); 

希望します。

関連する問題