pmilet.DomainEvents 1.0.3

There is a newer version of this package available.
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
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.
<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 and Polyglot Notebooks. 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
    _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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
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 2,629 4/1/2019
1.2.0 1,344 2/16/2018
1.1.1 1,431 1/26/2018
1.1.0 1,552 1/23/2018
1.0.5 1,527 10/25/2017
1.0.4 1,339 10/22/2017
1.0.3 1,389 10/18/2017
1.0.1 1,807 10/1/2017
1.0.0 1,844 9/27/2017

fix: handler method not executed when subscribed to multiple events