Ninject Event Broker拡張機能を使用しており、2つのサービスがあります。ServiceOne
イベントの発行者です。ServiceTwo
サブスクライバーです。ServiceOne
に強い依存関係はありませんServiceTwo
。DependencyCreation拡張機能を使用して依存関係を作成しています。
要件は次のとおりです。
- これら2つのオブジェクト間で1対1のイベントを定義したいと思います。
ServiceTwo
DependencyCreationによって作成されたインスタンスのみがイベントを受信する必要があります。 ServiceTwo
オブジェクトグラフのさらに下に他のインスタンスがある場合、それらはイベントを受信しないはずです。(これは当てはまらないはずですが、私はそれを説明したいと思います)ServiceTwo
廃棄時に廃棄する必要がありますServiceOne
。- これはWebアプリケーションであり、の有効期間は
ServiceOne
1つのリクエストのみである必要があります。
基本的に、私は自分が書いている動作を再現しようとしています。
var publisher = new Publisher();
var subscriber = new Subscriber();
var subscriber2 = new Subscriber();
publisher.MyEvent += subscriber.MyEventHandler;
1つの出版社。1人の加入者。Subscriber2はイベントを取得しません。
これが私のコードです:
this.Bind<IServiceOne, ServiceOne>().To<ServiceOne>().Named("ServiceOne").OwnsEventBroker("ServiceOne").RegisterOnEventBroker("ServiceOne");
this.Kernel.DefineDependency<IServiceOne, IServiceTwo>();
this.Bind<IServiceTwo>().To<ServiceTwo>().WhenParentNamed("ServiceOne").InDependencyCreatorScope().RegisterOnEventBroker("ServiceOne");
2つの質問。
これは私の要件を満たしていますか?もっと良い方法はありますか?