NPatterns is a collection of patterns often used in .NET projects, such as patterns of eaa.
- from github
- from nuget: by searching the Name "NPatterns"
- NPatterns: defines the contracts of patterns, like interface, base class, and basic implementations without any other dependencies.
- NPatterns.XXX: implement some patterns with specified dependencies.
NPatterns.Messaging.MessageBus, NPatterns
public class UserCreatedEvent //UserName,Email etc;
you could keep it as Singleton, so it could handle all messages in your application via registered handlers
public static IMessageBus Bus = new MessageBus();
you may keep the disposer if the disposing needed sometime
var disposer = Bus.Subscribe<UserCreatedEvent>(m=>{
//log for m.UserName
//send welcome mail to m.Email
});
then registered handler of previous step would be invoked
var aMessage=new UserCreatedEvent{UserName="abc",Email="abc@abc.com"};
Bus.Publish<UserCreatedEvent>(aMessage);
kernel.Bind<IMessageBus>() //Ninject
.ToConstructor(ctorArg => new MessageBus(type => ctorArg.Context.Kernel.GetAll(type))) //setup handler factory
.InSingletonScope(); //make it singleton
public class UserCreatedEventHandler:IHandler<UserCreatedEvent>
{
public void Handle(UserCreatedEvent message)
{
//log for message.UserName
//send welcome mail to message.Email
}
}
kernel.Bind<IHandler<UserCreatedEvent>>().To<UserCreatedEventHandler>(); //Ninject
//
//
//