pmilet.DomainEvents 1.0.3

DomainEvents

There is a newer version of this package available.
See the version list below for details.
Install-Package pmilet.DomainEvents -Version 1.0.3
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 
    



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 
    



Release Notes

fix: handler method not executed when subscribed to multiple events

Dependencies

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 History

Version Downloads Last updated
1.3.0 606 4/1/2019
1.2.0 391 2/16/2018
1.1.1 419 1/26/2018
1.1.0 448 1/23/2018
1.0.5 460 10/25/2017
1.0.4 344 10/22/2017
1.0.3 395 10/18/2017
1.0.1 758 10/1/2017
1.0.0 747 9/27/2017
Show less