- Notifications
You must be signed in to change notification settings - Fork2
A lightweight and easy-to-use CQRS event handling library
License
XerProjects/Xer.Cqrs.EventStack
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
| Branch | Status |
|---|---|
| Master | |
| Dev |
Simple CQRS library
This project composes of components for implementing the CQRS pattern (Event Handling). This library was built with simplicity, modularity and pluggability in mind.
- Send event to registered event handlers.
- Provides simple abstraction for hosted event handlers which can be registered just like an regular event handler.
- Multiple ways of registering event handlers:
Simple handler registration (no IoC container).
IoC container registration
Attribute registration
achieved by marking methods with [EventHandler] attributes from the Xer.Cqrs.EventStack.Extensions.Attributes package.
Seehttps://github.com/XerProjects/Xer.Cqrs.EventStack.Extensions.Attributes for documentation.
You can simply clone this repository, build the source, reference the dll from the project, and code away!
Xer.Cqrs.EventStack library is available as a Nuget package:
To install Nuget packages:
- Open command prompt
- Go to project directory
- Add the packages to the project:
dotnetadd package Xer.Cqrs.EventStack
- Restore the packages:
dotnetrestore
(Samples are in ASP.NET Core)
publicclassProductRegisteredEvent{publicintProductId{get;}publicstringProductName{get;}publicProductRegisteredEvent(intproductId,stringproductName){ProductId=productId;ProductName=productName;}}// Sync event handlerpublicclassProductRegisteredEventHandler:IEventHandler<ProductRegisteredEvent>{publicvoidHandle(ProductRegisteredEvent@event){System.Console.WriteLine($"ProductRegisteredEventHandler handled{@event.GetType()}.");}}// Async event handlerpublicclassProductRegisteredEmailNotifier:IEventAsyncHandler<ProductRegisteredEvent>{publicTaskHandleAsync(ProductRegisteredEvent@event,CancellationTokenct=default(CancellationToken)){System.Console.WriteLine($"Sending email notification...");returnTask.CompletedTask;}}
Before we can delegate any events, first, we need to register our event handlers. There are several ways to do this:
// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){ ...// Repository.services.AddSingleton<IProductRepository,InMemoryProductRepository>();// Register event delegator.services.AddSingleton<EventDelegator>((serviceProvider)=>{// Allows registration of a multiple message handlers per message type.varregistration=newMultiMessageHandlerRegistration();registration.RegisterEventHandler<ProductRegisteredEvent>(()=>newProductRegisteredEventHandler());registration.RegisterEventHandler<ProductRegisteredEvent>(()=>newProductRegisteredEmailNotifier());returnnewEventDelegator(registration.BuildMessageHandlerResolver());}); ...}
// This method gets called by the runtime. Use this method to add services to the container.publicvoidConfigureServices(IServiceCollectionservices){ ...// Repository.services.AddSingleton<IProductRepository,InMemoryProductRepository>();// Register event handlers to the container.// The AddCqrs extension method is in Xer.Cqrs.Extensions.Microsoft.DependencyInjection package.services.AddCqrs(typeof(ProductRegisteredEventHandler).Assembly); ...}
After setting up the event delegator in the Ioc container, events can now be delegated by simply doing:
...private readonly EventDelegator_eventDelegator;publicProductsController(EventDelegator eventDelegator){_eventDelegator=eventDelegator;}[HttpGet("{productId}")]publicasyncTask<IActionResult>Notify(ProductRegisteredEventDtomodel){await_eventDelegator.SendAsync(newProductRegisteredEvent(model.ProductId,model.ProductName)) return Accepted();}...
About
A lightweight and easy-to-use CQRS event handling library
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.