From 2d8ef67db82de4d3c7ed19f2253d5c35520f982b Mon Sep 17 00:00:00 2001 From: dingsongjie Date: Wed, 15 Jan 2020 14:42:04 +0800 Subject: [PATCH] 重构 domain event --- samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs | 3 ++- samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs | 27 +++++++++++++++++++++++++++ samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs | 5 ++--- src/Pole.Application/Cqrs/CommandResult.cs | 21 --------------------- src/Pole.Domain.EntityframeworkCore/DbContextBase.cs | 8 ++------ src/Pole.Domain.EntityframeworkCore/MediatR/MediatorExtension.cs | 12 ++---------- src/Pole.Domain/DomainHandleResult.cs | 26 -------------------------- src/Pole.Domain/IDomainEvent.cs | 2 +- src/Pole.Domain/IDomainEventHandler.cs | 2 +- src/Pole.Domain/UnitOfWork/CompleteResult.cs | 1 - src/Pole.Grpc/ExtraType/CommonCommandResponse.cs | 2 +- 11 files changed, 38 insertions(+), 71 deletions(-) create mode 100644 samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs delete mode 100644 src/Pole.Application/Cqrs/CommandResult.cs delete mode 100644 src/Pole.Domain/DomainHandleResult.cs diff --git a/samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs b/samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs index 679cf4e..a80b6f8 100644 --- a/samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs +++ b/samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs @@ -31,7 +31,8 @@ namespace Product.Api.Application.Command.CommandHandler ProductTypeName = productType.Name }; productType.AddDomainEvent(productTypeAddedDomainEvent); - return await _productTypeRepository.UnitOfWork.CompeleteAsync(); + var result= await _productTypeRepository.UnitOfWork.CompeleteAsync(); + return result; } } } diff --git a/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs b/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs new file mode 100644 index 0000000..80832f5 --- /dev/null +++ b/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAdded2DomainEventHandler.cs @@ -0,0 +1,27 @@ +using Pole.Domain; +using Product.Api.Domain.Event; +using Product.Api.Domain.ProductAggregate; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Product.Api.Application.DomainEventHandler +{ + public class AddDefaultProductWhenProductTypeAdded2DomainEventHandler : IDomainEventHandler + { + private readonly IProductRepository _productRepository; + public AddDefaultProductWhenProductTypeAdded2DomainEventHandler(IProductRepository productRepository) + { + _productRepository = productRepository; + } + + public async Task Handle(ProductTypeAddedDomainEvent request, CancellationToken cancellationToken) + { + Product.Api.Domain.ProductAggregate.Product product = new Product.Api.Domain.ProductAggregate.Product(Guid.NewGuid().ToString("N"), request.ProductTypeName, 100, request.ProductTypeId); + _productRepository.Add(product); + await _productRepository.UnitOfWork.CompeleteAsync(); + } + } +} diff --git a/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs b/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs index e5568d6..697ded7 100644 --- a/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs +++ b/samples/apis/Product.Api/Application/DomainEventHandler/AddDefaultProductWhenProductTypeAddedDomainEventHandler.cs @@ -17,12 +17,11 @@ namespace Product.Api.Application.DomainEventHandler _productRepository = productRepository; } - public async Task Handle(ProductTypeAddedDomainEvent request, CancellationToken cancellationToken) + public async Task Handle(ProductTypeAddedDomainEvent request, CancellationToken cancellationToken) { Product.Api.Domain.ProductAggregate.Product product = new Product.Api.Domain.ProductAggregate.Product(Guid.NewGuid().ToString("N"), request.ProductTypeName, 100, request.ProductTypeId); _productRepository.Add(product); - return await _productRepository.UnitOfWork.CompeleteAsync(); - + await _productRepository.UnitOfWork.CompeleteAsync(); } } } diff --git a/src/Pole.Application/Cqrs/CommandResult.cs b/src/Pole.Application/Cqrs/CommandResult.cs deleted file mode 100644 index 378ff74..0000000 --- a/src/Pole.Application/Cqrs/CommandResult.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Pole.Application.Cqrs -{ - //public class CommandResult - //{ - // public CommandResult(int status,string message) - // { - // Status = status; - // Message = message; - // } - // public static CommandResult SuccessResult = new CommandResult(1, "操作成功"); - // /// - // /// 1 Command Success 2 Command Faild ... - // /// - // public int Status { get;private set; } - // public string Message { get;private set; } - //} -} diff --git a/src/Pole.Domain.EntityframeworkCore/DbContextBase.cs b/src/Pole.Domain.EntityframeworkCore/DbContextBase.cs index 06a341e..7dc42fe 100644 --- a/src/Pole.Domain.EntityframeworkCore/DbContextBase.cs +++ b/src/Pole.Domain.EntityframeworkCore/DbContextBase.cs @@ -23,12 +23,8 @@ namespace Pole.EntityframeworkCore { var result = CompleteResult.SuccessResult; - var eventHnadlersResult = await _mediator.DispatchDomainEventsAsync(this); - if (eventHnadlersResult.Status != 1) - { - return new CompleteResult(eventHnadlersResult); - } - var dbResult = await base.SaveChangesAsync(cancellationToken); + await _mediator.DispatchDomainEventsAsync(this); + await base.SaveChangesAsync(cancellationToken); return result; } diff --git a/src/Pole.Domain.EntityframeworkCore/MediatR/MediatorExtension.cs b/src/Pole.Domain.EntityframeworkCore/MediatR/MediatorExtension.cs index 8c68e99..f91d849 100644 --- a/src/Pole.Domain.EntityframeworkCore/MediatR/MediatorExtension.cs +++ b/src/Pole.Domain.EntityframeworkCore/MediatR/MediatorExtension.cs @@ -11,10 +11,8 @@ namespace Pole.EntityframeworkCore.MediatR { public static class MediatorExtension { - public static async Task DispatchDomainEventsAsync(this IMediator mediator, DbContext ctx) + public static async Task DispatchDomainEventsAsync(this IMediator mediator, DbContext ctx) { - var result = DomainHandleResult.SuccessResult; - var domainEntities = ctx.ChangeTracker .Entries() .Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any()); @@ -28,14 +26,8 @@ namespace Pole.EntityframeworkCore.MediatR foreach(var domainEvent in domainEvents) { - var currentDomainHandleResult = await mediator.Send(domainEvent); - if (currentDomainHandleResult.Status != 1) - { - result = currentDomainHandleResult; - break; - } + await mediator.Publish(domainEvent); } - return result; } } } diff --git a/src/Pole.Domain/DomainHandleResult.cs b/src/Pole.Domain/DomainHandleResult.cs deleted file mode 100644 index 0118de3..0000000 --- a/src/Pole.Domain/DomainHandleResult.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Pole.Domain.UnitOfWork; -using System; -using System.Collections.Generic; -using System.Text; - -namespace Pole.Domain -{ - public class DomainHandleResult - { - public DomainHandleResult(int status, string message) - { - Status = status; - Message = message; - } - public static DomainHandleResult SuccessResult = new DomainHandleResult(1, "处理成功"); - public static implicit operator DomainHandleResult(CompleteResult completeResult) - { - return new DomainHandleResult(completeResult.Status, completeResult.Message); - } - /// - /// 1 Success 2 Faild ... - /// - public int Status { get;private set; } - public string Message { get;private set; } - } -} diff --git a/src/Pole.Domain/IDomainEvent.cs b/src/Pole.Domain/IDomainEvent.cs index fdfafba..b337c54 100644 --- a/src/Pole.Domain/IDomainEvent.cs +++ b/src/Pole.Domain/IDomainEvent.cs @@ -5,7 +5,7 @@ using System.Text; namespace Pole.Domain { - public interface IDomainEvent : IRequest + public interface IDomainEvent : INotification { } diff --git a/src/Pole.Domain/IDomainEventHandler.cs b/src/Pole.Domain/IDomainEventHandler.cs index 18b32e3..f2ff36c 100644 --- a/src/Pole.Domain/IDomainEventHandler.cs +++ b/src/Pole.Domain/IDomainEventHandler.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace Pole.Domain { - public interface IDomainEventHandler : IRequestHandler where TCommand : IRequest + public interface IDomainEventHandler : INotificationHandler where TCommand : IDomainEvent { } diff --git a/src/Pole.Domain/UnitOfWork/CompleteResult.cs b/src/Pole.Domain/UnitOfWork/CompleteResult.cs index 0586fa3..c1c7a6d 100644 --- a/src/Pole.Domain/UnitOfWork/CompleteResult.cs +++ b/src/Pole.Domain/UnitOfWork/CompleteResult.cs @@ -12,7 +12,6 @@ namespace Pole.Domain.UnitOfWork Status = status; Message = message; } - public CompleteResult(DomainHandleResult domainHandleResult) : this(domainHandleResult.Status, domainHandleResult.Message) { } /// /// 1 Success 2 Faild ... diff --git a/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs b/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs index ffdd9b6..4117179 100644 --- a/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs +++ b/src/Pole.Grpc/ExtraType/CommonCommandResponse.cs @@ -11,7 +11,7 @@ namespace Pole.Grpc.ExtraType { public static implicit operator CommonCommandResponse(CompleteResult domainHandleResult) { - return new CompleteResult(domainHandleResult.Status, domainHandleResult.Message); + return new CommonCommandResponse { Status = domainHandleResult.Status, Message = domainHandleResult.Message }; } } } -- libgit2 0.25.0