MaxDomainEventCore.Net 1.1.3

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

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

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)

MessageInterface: IDomainCommand, IDomainEvent, IDomainRequest, IDomainResponse HandlerInterface: IDomainCommandHandler<IDomainCommand, IDomainResponse>, IDomainCommandHandler<IDomainCommand>, IDomainRequestHandler<IDomainRequest, IDomainResponse>, IDomainEventHandler<IDomainEvent>

1.1 创建无响应领域命令,领域事件和对应的处理器(Create unresponsive domain commands, domain events, and corresponding processors)

public class CreateOrderCommand : IDomainCommand
{
}

public class CreateOrderCommandHandler : IDomainCommandHandler<CreateOrderCommand>
{
    //Implement the Handle method for the interface
    public Task Handle(CreateOrderCommand message, IDomainEventInitiator domainEventInitiator,
        CancellationToken cancellationToken)
    {
        //Your logic
    }
}

public class DeleteOrderEvent : IDomainEvent
{
}

public class DeleteOrderEventHandler : IDomainEventHandler<DeleteOrderEvent>
{
    //Implement the Handle method for the interface
    public Task Handle(DeleteOrderEvent message, IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
    }
}

1.2 创建有响应领域请求,领域命令和对应的处理器(Create response domain requests, domain commands, and corresponding processors)

public class GetOrderRequest : IDomainRequest
{
}

public class GetOrderRequestHandler : IDomainRequestHandler<GetOrderRequest, OrderDto>
{
    public Task<OrderDto> Handle(GetOrderRequest message, IDomainEventInitiator domainEventInitiator, CancellationToken cancellationToken)
    {
        //Your logic
    }
}

public class ExportOrderCommand : IDomainCommand
{
}

public class ExportOrderCommandHandler : IDomainCommandHandler<ExportOrderCommand, OrderDto>
{
    public Task<OrderDto> Handle(ExportOrderCommand message, IDomainEventInitiator domainEventInitiator,
        CancellationToken cancellationToken)
    {
        //Your logic
    }
}

1.3 领域事件处理器中中的依赖注入(Dependency injection in domain event processors)

Dependency injection can be done through constructors or properties 可以通过构造函数或者属性进行依赖注入

public class ExportOrderCommandHandler : IDomainCommandHandler<ExportOrderCommand, OrderDto>
{
    private readonly IOrderRepository _orderRepository;
    private readonly IMapper _mapper;

    //Dependency Injection
    public ExportOrderCommandHandler(IOrderRepository orderRepository, IMapper mapper)
    {
        _orderRepository = orderRepository;
        _mapper = mapper;
    }

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

1.4 领域处理器中的发起领域事件(Initiating domain events in domain processors)

public class ExportOrderCommandHandler : IDomainCommandHandler<ExportOrderCommand, OrderDto>
{
    private readonly IOrderRepository _orderRepository;
    private readonly IMapper _mapper;

    //Dependency Injection
    public ExportOrderCommandHandler(IOrderRepository orderRepository, IMapper mapper)
    {
        _orderRepository = orderRepository;
        _mapper = mapper;
    }

    public async Task<OrderDto> Handle(ExportOrderCommand message, IDomainEventInitiator domainEventInitiator,
        CancellationToken cancellationToken)
    {
        await domainEventInitiator.SendAsync<UpdateOrderCommand>(new UpdateOrderCommand());
        //Your logic
    }
}

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

1.1 触发无响应的领域事件和领域命令(Trigger unresponsive domain events and commands)

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

        await _domainEventInitiator.SendAsync(new UpdateOrderCommand());

        return Ok();
    }

1.2 触发有响应领域请求和领域命令(Triggering has responsive domain requests and domain commands)

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

        var dto2 = await _domainEventInitiator.SendAsync<ExportOrderCommand, OrderDto>(new ExportOrderCommand());

        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 使用领域事件拦截器(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.2 领域事件拦截器中进行依赖注入(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

调整在不创建拦截器的情况下无法在程序中抛出异常的问题
Adjust the issue of not being able to throw exceptions in the program without creating interceptors