pmilet.DomainEvents
1.1.0
See the version list below for details.
dotnet add package pmilet.DomainEvents --version 1.1.0
NuGet\Install-Package pmilet.DomainEvents -Version 1.1.0
<PackageReference Include="pmilet.DomainEvents" Version="1.1.0" />
paket add pmilet.DomainEvents --version 1.1.0
#r "nuget: pmilet.DomainEvents, 1.1.0"
// Install pmilet.DomainEvents as a Cake Addin #addin nuget:?package=pmilet.DomainEvents&version=1.1.0 // Install pmilet.DomainEvents as a Cake Tool #tool nuget:?package=pmilet.DomainEvents&version=1.1.0
Purpose
The purppose of this library is to simplify the development of applications that use the Domain Event design pattern. This library is based on an article from Jimmy Boggard A better domain events pattern..
Domain Events
Martin Fowlers defines the domainEvent DDD pattern as: Domain Events captures the memory of something interesting which affects the domain. The essence of a Domain Event is that you use it to capture important things that happens into the domain and that can produce a change into the state of the application you are developing. Another good source of information is this chapter about Domain Events from the Microsoft ebook: .NET Microservices. Architecture for Containerized .NET Applications
1. Model your domain event
To create your DomainEvent class you could either inherit from the base Class DomainEvent or implement the IDomainEvent interface.
/// <summary>
/// Represents the play choosen by a player
/// </summary>
public class PlayMade : DomainEvent
{
public PlayMade(PlayerType player, PlayType play)
: base( "PlayMade", "1.0")
{
Player = player;
Play = play;
}
public PlayerType Player { get; private set; }
public PlayType Play { get; private set; }
}
2. Create an instance of the DomainEventDispatcher
From your business logic you can publish events or subscribe to events by means of a DomainEventDispatcher instance.
// we create the domain event dispatcher and inject it into the objects of our domain model (normally done using a IoC container)
IDomainEventDispatcher dispatcher = new DomainEventDispatcher();
Player j1 = new Player1(dispatcher);
Player j2 = new Player2(dispatcher);
Match match = new Match(dispatcher);
Outcome outcome = new Outcome(dispatcher);
Note: a better option is to register the DomainEventDispatcher instance in your IoC container.
3. Trigger domain events from your business logic
To trigger an domain event immediately we should use the DomainEventDipatcher Publish<T> method:
//publish an event notifying that the match ended and Player1 is the winner
_dispatcher.Publish<MatchEnded>(new MatchEnded(PlayerType.Player1));
To add a delayed domain event we should use the Add<T> method :
//delayed event to notify of the move choosen by the player
_dispatcher.Add<PlayMade>(new PlayMade( _player, play ));
To trigger all the delayed domain events of a specific type we should use the Commit<T> method :
//commit all registered delayed events
_dispatcher.Commit<PlayMade>();
4. Respond to specific domain events from your business logic by subscribing to them
To subscribe to specific domain events we should inherit from the IHandleDomainEvents<T> interface and subscribe to the dispatcher instance (normally in the constructor).
public class Outcome : IHandleDomainEvents<MatchEnded>
{
PlayerType _lastWinner;
private readonly IDomainEventDispatcher _dispatcher;
public Outcome(IDomainEventDispatcher dispatcher)
{
_dispatcher = dispatcher;
dispatcher.Subscribe<MatchEnded>(this);
}
public Guid SubscriberId => throw new NotImplementedException();
public void HandleEvent(MatchEnded domainEvent)
{
_lastWinner = domainEvent.Winner;
}
public PlayerType LastWinner()
{
return _lastWinner;
}
}
```
### See a example app that uses domain events
To see a running sample take a look to the StonePaperScissors app or specflow functional tests from the [github](https://github.com/pmilet/domainevents) repo
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp1.1 is compatible. netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net452 is compatible. net46 was computed. net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
target frameworks: netcoreapp1.1;netcoreapp2.0;netstandard2.0;net461;net452