Fluxera.DomainEvents.MediatR 9.0.1

Prefix Reserved
dotnet add package Fluxera.DomainEvents.MediatR --version 9.0.1                
NuGet\Install-Package Fluxera.DomainEvents.MediatR -Version 9.0.1                
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="Fluxera.DomainEvents.MediatR" Version="9.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fluxera.DomainEvents.MediatR --version 9.0.1                
#r "nuget: Fluxera.DomainEvents.MediatR, 9.0.1"                
#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 Fluxera.DomainEvents.MediatR as a Cake Addin
#addin nuget:?package=Fluxera.DomainEvents.MediatR&version=9.0.1

// Install Fluxera.DomainEvents.MediatR as a Cake Tool
#tool nuget:?package=Fluxera.DomainEvents.MediatR&version=9.0.1                

Build Status

Fluxera.Entity

An aggregate root and entity objects library.

This library helps in implementing Entity, Aggregate Root and Domain Events. The base class for entities implement Equality and Uniqueness based on selected attributes of the entity instead of the references.

Equality and Uniqueness

The equality check consist of two major steps:

1.) When comparing two entities for Equality at first we check if the entites are both not transient and their IDs are equal. In this case the entites are considered equal, even if their values are different.

2.) If the first step did not signal Equality we check if the entities are both transient and their domain signature attributes are equal. You define which attributes of the entity make up their domain signature by adding the [DomainSignature] attribute to the corresponding properties. The attributes are then picked up by the default implementation using reflection. If you do not want to use the default implementation you can override the GetEqualityComponents() method an return the values to use manually.

Domain Events

This library provides the infrastructure to implement, register and dispatch domain events from entities to loosely-coupled domain event handlers. You can add events to the DomainEvents collection of an entity and implement two different types of domain event handlers for it.

This library provides the nessessary dispatcher service which can be used to integrate the event dispatching in a Repository implementation. A IDomainEventHandler implementation handles a published domain event.

Configuration

Two different publishing infrastructures exist:

  • A default implementation using a custom domain event dispatcher.

    • Add the Fluxera.DomainEvents package to use this implementation.
  • A MediatR based implementation using the IPublisher of the MediatR library.

    • Add the Fluxera.DomainEvents.MediatR package to use this implementation.

You can NOT use both packages at the same time.

Default implementation (Fluxera.DomainEvents)

// A domain event support.
services.AddDomainEvents();

// Add domain event handlers.
services.AddDomainEventHandler<SalaryRaisedEventHandler>();

IDomainEventDispatcher dispatcher = /* Get the dispatcher ... */;

SalaryRaisedEvent salaryRaisedEvent = new SalaryRaisedEvent(100_000);

await dispatcher.DispatchAsync(salaryRaisedEvent);

MediatR implementation (Fluxera.DomainEvents.MediatR)

// A domain event support.
services.AddDomainEvents();

// Add domain event handlers by configuring MediatR.
// This will automatically register all domain event handlers in the given assembly.
services.AddMediatR(cfg =>
{
	cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
});

IDomainEventDispatcher dispatcher = /* Get the dispatcher ... */;

SalaryRaisedEvent salaryRaisedEvent = new SalaryRaisedEvent(100_000);

await dispatcher.DispatchAsync(salaryRaisedEvent);

Usage

public class Employee : AggregateRoot<Employee>
{
	[DomainSignature]
	public string Name { get; set; }

	[DomainSignature]
	public int EmployeeNumber { get; set; }

	public decimal Salary { get; set; }

	public void GiveRaise(decimal raiseAmount)
	{
		Guard.Against.NegativeOrZero(raiseAmount, nameof(raiseAmount));

		this.Salary += raiseAmount;

		this.DomainEvents.Add(new SalaryRaisedEvent(this.Salary));
	}
}
public class SalaryRaisedEvent : IDomainEvent
{
	public SalaryRaisedEvent(decimal newSalary)
	{
		this.NewSalary = newSalary;
		this.HandlerNames = new List<string>();
	}

	public decimal NewSalary { get; }
}
public class SalaryRaisedEventHandler : IDomainEventHandler<SalaryRaisedEvent>
{
	public Task HandleAsync(SalaryRaisedEvent domainEvent, CancellationToken cancellationToken = default)
	{
		// Do something ...
		return Task.CompletedTask;
	}
}

Future

With the upcoming v9.0 release in november 2024 the domain events libraries will be moved to a separate repository, because it can be used with your own entities and repositories.

References

Jimmy Bogard - A better domain events pattern

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Fluxera.DomainEvents.MediatR:

Package Downloads
Fluxera.Repository

A generic repository implementation.

Fluxera.Extensions.Hosting.Modules.Application

A module that enables the application.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.1 228 11/16/2024
9.0.0 122 11/14/2024
8.3.4 357 11/1/2024
8.3.3 111 7/9/2024
8.3.2 852 6/12/2024
8.3.1 195 6/8/2024
8.3.0 627 5/26/2024
8.2.1 123 5/24/2024
8.2.0 512 4/28/2024