Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A lightweight and easy-to-use CQRS event handling library

License

NotificationsYou must be signed in to change notification settings

XerProjects/Xer.Cqrs.EventStack

Repository files navigation

BranchStatus
MasterBuild status
DevBuild status

Table of contents

Overview

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.

Features

  • 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

      • achieved by creating implementations of IContainerAdapter or using pre-made extensions packages for supported containers:
        • Microsoft.DependencyInjection

          NuGet

        • SimpleInjector

          NuGet

        • Autofac

          NuGet

    • Attribute registration

Installation

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:

NuGet

To install Nuget packages:

  1. Open command prompt
  2. Go to project directory
  3. Add the packages to the project:
    dotnetadd package Xer.Cqrs.EventStack
  4. Restore the packages:
    dotnetrestore

Getting Started

(Samples are in ASP.NET Core)

Sample Event and Event Handlers

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;}}

Event Handler Registration

Before we can delegate any events, first, we need to register our event handlers. There are several ways to do this:

1. Simple Registration (No IoC container)
// 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());});    ...}
2. Container Registration
// 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);    ...}

Delegating Events to Event Handlers

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

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp