pmilet.DomainEvents
1.0.3
See the version list below for details.
dotnet add package pmilet.DomainEvents --version 1.0.3
NuGet\Install-Package pmilet.DomainEvents -Version 1.0.3
<PackageReference Include="pmilet.DomainEvents" Version="1.0.3" />
paket add pmilet.DomainEvents --version 1.0.3
#r "nuget: pmilet.DomainEvents, 1.0.3"
// Install pmilet.DomainEvents as a Cake Addin #addin nuget:?package=pmilet.DomainEvents&version=1.0.3 // Install pmilet.DomainEvents as a Cake Tool #tool nuget:?package=pmilet.DomainEvents&version=1.0.3
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 that can produce a change into the state of the application you are developing.
This package is based on an article from Jimmy Boggard A better domain events pattern..
How to use it
Install-Package pmilet.DomainEvents -Version 1.0.3
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; }
}
To be able to publish events and subscribe to events our domain objects will use a DomainEventBus instance injected into the constructor:
// we create the domain event bus and inject it into the objects of our domain model (normally done using a IoC container)
IDomainEventBus bus = new DomainEventBus();
Player j1 = new Player1(bus);
Player j2 = new Player2(bus);
Match match = new Match(bus);
Outcome outcome = new Outcome(bus);
To trigger an event immediately we should use the Publish method:
//publish an event notifying that the match ended and Player1 is the winner
_bus.Publish<MatchEnded>(new MatchEnded(PlayerType.Player1));
To record a delayed event we should use the Add method :
//delayed event to notify of the move choosen by the player
_bus.Add<PlayMade>(new PlayMade( _player, play ));
To trigger all the delayed events we should use the Commit method (only the delayed events of the specified type will be triggered):
//commit all registered delayed events
_bus.Commit<PlayMade>();
To subscribe a domain object to handle specifics events we should inherit from the IHandleEvent interface and subscribe to the bus (note that we could subscribe to one or more type of events by just inheriting to the IHandleEvent of the specific Type)
public class Outcome : IHandleDomainEvents<MatchEnded>
{
PlayerType _lastWinner;
private readonly IDomainEventBus _bus;
public Outcome(IDomainEventBus bus)
{
_bus = bus;
bus.Subscribe<MatchEnded>(this);
}
public Guid SubscriberId => throw new NotImplementedException();
public void HandleEvent(MatchEnded domainEvent)
{
_lastWinner = domainEvent.Winner;
}
public PlayerType LastWinner()
{
return _lastWinner;
}
}
```
To see a running sample take a look to the StonePaperScissors specflow test example
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 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.
fix: handler method not executed when subscribed to multiple events