MaxDomainEventCore.Net 1.0.10

There is a newer version of this package available.
See the version list below for details.
dotnet add package MaxDomainEventCore.Net --version 1.0.10                
NuGet\Install-Package MaxDomainEventCore.Net -Version 1.0.10                
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="MaxDomainEventCore.Net" Version="1.0.10" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MaxDomainEventCore.Net --version 1.0.10                
#r "nuget: MaxDomainEventCore.Net, 1.0.10"                
#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 MaxDomainEventCore.Net as a Cake Addin
#addin nuget:?package=MaxDomainEventCore.Net&version=1.0.10

// Install MaxDomainEventCore.Net as a Cake Tool
#tool nuget:?package=MaxDomainEventCore.Net&version=1.0.10                

MaxDomainEventCore.Net

此包依赖于 Autofac,它有助于实现中介和域事件模式,使您能够专注于逻辑实现,而无需处理事件订阅和服务注册。(后续会更新通过微软原生容器实现的包)

This package relies on Autofac, which helps implement mediation and domain event patterns, allowing you to focus on logical implementation without dealing with event subscriptions and service registration.(The package implemented through Microsoft native containers will be updated in the future)

创建领域事件(Create Domain Event)

1.1 创建无响应领域事件(Create No Response Domain Event)

interface: IDomainCommand<Command>, IDomainEvent<Event>, IDomainCommand<Command, Response>, IDomainRequest<Command, Response>

public class OrderCreateCommand : IDomainCommand<OrderCreateCommand>
{
    public Order Order { get; set; } = null!;

    public async Task Run(
        IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
    }
}

public class OrderPayEvent : IDomainEvent<OrderPayEvent>
{
    public Order Order { get; set; }

    public async Task Run(IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
    }
}

1.2 创建有响应领域事件(Create Has Response Domain Event)

public class OrderGetRequest : IDomainRequest<OrderGetRequest, OrderDto>
{
    public Guid OrderId { get; set; }

    public async Task<OrderDto> Run(
        IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
    }
}

public class OrderDeleteCommand : IDomainCommand<OrderDeleteCommand, OrderDto>
{
    public async Task<OrderDto> Run(IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
    }
}

1.3 领域事件中的依赖注入(Dependency injection in domain events)

After creating a property, whether it is private or public, property injection will be automatically performed 创建一个属性后,无论是私有/公开的,都会自动进行属性注入

public class OrderGetRequest : IDomainRequest<OrderGetRequest, OrderDto>
{
    public Guid OrderId { get; set; }

    private IService Service { get; set; }

    public IDbContext DbContext { get; set;}

    public async Task<OrderDto> Run(
        IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
    }
}

1.4 领域事件中的发起领域事件(Initiating Domain Events in Domain Events)

public class OrderGetRequest : IDomainRequest<OrderGetRequest, OrderDto>
{
    public Guid OrderId { get; set; }

    private IService Service { get; set; }

    public async Task<OrderDto> Run(
        IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
        await domainEventInitiator.CommandAsync(new OrderCreateCommand());
    }
}

触发你的领域事件(Trigger Domain Events)

1.1 发布无响应领域事件(Publish Not Response DomainEvent)

    [HttpPost]
    [ProducesResponseType<OrderDto>(200)]
    public async Task<IActionResult> CreateOrder()
    {
        await _domainEventInitiator.PublishAsync(new OrderPayEvent());

        await _domainEventInitiator.SendAsync(new OrderCreateCommand());

        return Ok();
    }

1.2 请求有响应领域请求(Request Has Response DomainEvent)

    [HttpGet]
    [ProducesResponseType<OrderDto>(200)]
    public async Task<IActionResult> GetOrder()
    {
        var dto = await _domainEventInitiator.RequestAsync<OrderGetRequest, OrderDto>(new OrderGetRequest { OrderId = Guid.NewGuid() });

        return Ok(dto);
    }

1.3 发送有响应的领域命令(Send Has Response DomainEvent)

    [HttpGet]
    [ProducesResponseType<OrderDto>(200)]
    public async Task<IActionResult> GetOrder()
    {
        var dto = await _domainEventInitiator.SendAsync<OrderDeleteCommand, OrderDto>(new OrderDeleteCommand { OrderId = Guid.NewGuid() });
        return Ok(dto);
    }

注册 MaxDomainEventCore(Register MaxDomainEventCore)

1.1 注册 IDomainEventInitiator(Register IDomainEventInitiator)

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory(containerBuilder =>
{
    containerBuilder.RegisterMaxDomainEventInitiator();
}));

1.2 使用 IDomainEventInitiator(Using IDomainEventInitiator)

[Route("Order")]
public class OrderController : BaseController
{
    private readonly IDomainEventInitiator _domainEventInitiator;

    public OrderController(IDomainEventInitiator domainEventInitiator)
    {
        _domainEventInitiator = domainEventInitiator;
    }
}

领域事件拦截器(Domain Event Interceptor)

1.1 注册领域事件拦截器(Register Max Domain Event Interceptor)

builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory(containerBuilder =>
{
    //通过Autofac注入服务
    //Inject services through Autofac
    containerBuilder.RegisterMaxDomainEventInterceptor();
}));

app.InitializeMaxDomainEventInterceptor();

1.2 使用领域事件拦截器(Using Max Domain Event Interceptor)

public class LogDomainEventEventInterceptor : MaxDomainEventInterceptor
{
    public override Task BeforeExecuteAsync(IMaxDomainEventInterceptorContext<IDomainEvent, IDomainResponse> context, CancellationToken cancellationToken)
    {
        Console.WriteLine("LogDomainEventFilter.BeforeExecuteAsync");
        return base.BeforeExecuteAsync(context, cancellationToken);
    }

    public override Task AfterExecuteAsync(IMaxDomainEventInterceptorContext<IDomainEvent, IDomainResponse> context, CancellationToken cancellationToken)
    {
        Console.WriteLine("LogDomainEventFilter.AfterExecuteAsync");
        return base.AfterExecuteAsync(context, cancellationToken);
    }

    public override Task OnException(Exception ex, IMaxDomainEventInterceptorContext<IDomainEvent, IDomainResponse> context)
    {
        return base.OnException(ex, context);
    }
}

Ps:使用领域事件拦截器后,其会在对应的领域事件 Run 方法前与方法后 进行逻辑处理。 After using the domain event interceptor, it will perform logical processing before and after the corresponding domain event Run method.

1.3 领域事件拦截器中进行依赖注入(Dependency injection in domain event interceptors)

public class LogDomainEventEventInterceptor : MaxDomainEventInterceptor
{
    private IService _service;

    //Constructor Inject
    //构造函数注入
    LogDomainEventEventInterceptor(IService service)
    {
        _service = service;
    }

    public override Task BeforeExecuteAsync(IMaxDomainEventInterceptorContext<IDomainEvent, IDomainResponse> context, CancellationToken cancellationToken)
    {
        Console.WriteLine("LogDomainEventFilter.BeforeExecuteAsync");
        return base.BeforeExecuteAsync(context, cancellationToken);
    }

    public override Task AfterExecuteAsync(IMaxDomainEventInterceptorContext<IDomainEvent, IDomainResponse> context, CancellationToken cancellationToken)
    {
        Console.WriteLine("LogDomainEventFilter.AfterExecuteAsync");
        return base.AfterExecuteAsync(context, cancellationToken);
    }

    public override Task OnException(Exception ex, IMaxDomainEventInterceptorContext<IDomainEvent, IDomainResponse> context)
    {
        return base.OnException(ex, context);
    }
}
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1.3 81 10/24/2024
1.1.2 89 10/23/2024
1.0.10 79 10/22/2024

Add IDomainCommand<,> and IDomainEvent<>