I don't normally like to answer my own question but seeing as this has been quiet for a while, I've been testing my code sample and it appears to work fine. To clean up the creation of these dependencies and the whole event broker registration process I created some extension methods. First off an IsPublisher
extension that creates a scoped event broker:
public static ISubscriberBuildingSyntax IsPublisher<TPublisher>(this IBindingWhenInNamedWithOrOnSyntax<TPublisher> syntax)
{
string name = Guid.NewGuid().ToString();
syntax.Named(name);
syntax.OwnsEventBroker(name).RegisterOnEventBroker(name);
return new SubscriberBuildingSyntax<TPublisher>(syntax, name);
}
Secondly, a generic CreateSubscriberDependency
method that creates a dependency using Dependency Creator:
public ISubscriberBuildingSyntax CreateSubscriberDependency<TSubscriber>() where TSubscriber : class
{
this.syntax.Kernel.DefineDependency<TPublisher, TSubscriber>();
this.syntax.Kernel.Bind<TSubscriber>().ToSelf().WhenParentNamed(this.name).InDependencyCreatorScope().RegisterOnEventBroker(this.name);
return this;
}
I can then call this like so:
this.Bind<IRegistrationService>().To<RegistrationService>()
.IsPublisher()
.CreateSubscriberDependency<RoleService>();
This creates an Event Broker scoped to the RegistrationService
instance with a RoleService
dependency that is tied to the life of RegistrationService
.
I can then register RegistrationService
with InRequestScope
to limit this to the life of one request.