pmilet.DomainEvents 1.0.3

.NET Framework 4.6.1
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package pmilet.DomainEvents -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
dotnet add package pmilet.DomainEvents --version 1.0.3
<PackageReference Include="pmilet.DomainEvents" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add pmilet.DomainEvents --version 1.0.3
#r "nuget: pmilet.DomainEvents, 1.0.3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// 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

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;

        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
.NET Framework net461 net462 net463 net47 net471 net472 net48
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
1.3.0 1,382 4/1/2019
1.2.0 673 2/16/2018
1.1.1 766 1/26/2018
1.1.0 798 1/23/2018
1.0.5 803 10/25/2017
1.0.4 633 10/22/2017
1.0.3 707 10/18/2017
1.0.1 1,079 10/1/2017
1.0.0 1,068 9/27/2017

fix: handler method not executed when subscribed to multiple events