I'm using Ninject Event Broker extensions and I have two services. ServiceOne
is the Publisher of an event. ServiceTwo
is the subscriber. ServiceOne
doesn't have a hard dependency to ServiceTwo
, I'm creating the dependency using the DependencyCreation extension.
Here are the requirements:
- I want to define a one-to-one event between these two objects. Only the
ServiceTwo
instance created by DependencyCreation should receive the event. - If there are other instances of
ServiceTwo
further down in the object graph they shouldn't receive the event. (this shouldn't be the case but I want to account for it) ServiceTwo
should be disposed of whenServiceOne
is disposed.- This is a web application and the life of
ServiceOne
should only be for one request.
Basically I'm just trying to recreate the behaviour of me writing:
var publisher = new Publisher();
var subscriber = new Subscriber();
var subscriber2 = new Subscriber();
publisher.MyEvent += subscriber.MyEventHandler;
One publisher. One subscriber. Subscriber2 doesn't get the event.
Here's my code:
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");
Two questions.
Does this fulfill my requirements? Is there a better way?