Commit 2d8ef67d by dingsongjie

重构 domain event

parent 450dd303
...@@ -31,7 +31,8 @@ namespace Product.Api.Application.Command.CommandHandler ...@@ -31,7 +31,8 @@ namespace Product.Api.Application.Command.CommandHandler
ProductTypeName = productType.Name ProductTypeName = productType.Name
}; };
productType.AddDomainEvent(productTypeAddedDomainEvent); productType.AddDomainEvent(productTypeAddedDomainEvent);
return await _productTypeRepository.UnitOfWork.CompeleteAsync(); var result= await _productTypeRepository.UnitOfWork.CompeleteAsync();
return result;
} }
} }
} }
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<ProductTypeAddedDomainEvent>
{
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();
}
}
}
...@@ -17,12 +17,11 @@ namespace Product.Api.Application.DomainEventHandler ...@@ -17,12 +17,11 @@ namespace Product.Api.Application.DomainEventHandler
_productRepository = productRepository; _productRepository = productRepository;
} }
public async Task<DomainHandleResult> 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); Product.Api.Domain.ProductAggregate.Product product = new Product.Api.Domain.ProductAggregate.Product(Guid.NewGuid().ToString("N"), request.ProductTypeName, 100, request.ProductTypeId);
_productRepository.Add(product); _productRepository.Add(product);
return await _productRepository.UnitOfWork.CompeleteAsync(); await _productRepository.UnitOfWork.CompeleteAsync();
} }
} }
} }
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, "操作成功");
// /// <summary>
// /// 1 Command Success 2 Command Faild ...
// /// </summary>
// public int Status { get;private set; }
// public string Message { get;private set; }
//}
}
...@@ -23,12 +23,8 @@ namespace Pole.EntityframeworkCore ...@@ -23,12 +23,8 @@ namespace Pole.EntityframeworkCore
{ {
var result = CompleteResult.SuccessResult; var result = CompleteResult.SuccessResult;
var eventHnadlersResult = await _mediator.DispatchDomainEventsAsync(this); await _mediator.DispatchDomainEventsAsync(this);
if (eventHnadlersResult.Status != 1) await base.SaveChangesAsync(cancellationToken);
{
return new CompleteResult(eventHnadlersResult);
}
var dbResult = await base.SaveChangesAsync(cancellationToken);
return result; return result;
} }
......
...@@ -11,10 +11,8 @@ namespace Pole.EntityframeworkCore.MediatR ...@@ -11,10 +11,8 @@ namespace Pole.EntityframeworkCore.MediatR
{ {
public static class MediatorExtension public static class MediatorExtension
{ {
public static async Task<DomainHandleResult> DispatchDomainEventsAsync(this IMediator mediator, DbContext ctx) public static async Task DispatchDomainEventsAsync(this IMediator mediator, DbContext ctx)
{ {
var result = DomainHandleResult.SuccessResult;
var domainEntities = ctx.ChangeTracker var domainEntities = ctx.ChangeTracker
.Entries<Entity>() .Entries<Entity>()
.Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any()); .Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any());
...@@ -28,14 +26,8 @@ namespace Pole.EntityframeworkCore.MediatR ...@@ -28,14 +26,8 @@ namespace Pole.EntityframeworkCore.MediatR
foreach(var domainEvent in domainEvents) foreach(var domainEvent in domainEvents)
{ {
var currentDomainHandleResult = await mediator.Send(domainEvent); await mediator.Publish(domainEvent);
if (currentDomainHandleResult.Status != 1)
{
result = currentDomainHandleResult;
break;
}
} }
return result;
} }
} }
} }
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);
}
/// <summary>
/// 1 Success 2 Faild ...
/// </summary>
public int Status { get;private set; }
public string Message { get;private set; }
}
}
...@@ -5,7 +5,7 @@ using System.Text; ...@@ -5,7 +5,7 @@ using System.Text;
namespace Pole.Domain namespace Pole.Domain
{ {
public interface IDomainEvent : IRequest<DomainHandleResult> public interface IDomainEvent : INotification
{ {
} }
......
...@@ -7,7 +7,7 @@ using System.Threading.Tasks; ...@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace Pole.Domain namespace Pole.Domain
{ {
public interface IDomainEventHandler<TCommand> : IRequestHandler<TCommand, DomainHandleResult> where TCommand : IRequest<DomainHandleResult> public interface IDomainEventHandler<TCommand> : INotificationHandler<TCommand> where TCommand : IDomainEvent
{ {
} }
......
...@@ -12,7 +12,6 @@ namespace Pole.Domain.UnitOfWork ...@@ -12,7 +12,6 @@ namespace Pole.Domain.UnitOfWork
Status = status; Status = status;
Message = message; Message = message;
} }
public CompleteResult(DomainHandleResult domainHandleResult) : this(domainHandleResult.Status, domainHandleResult.Message) { }
/// <summary> /// <summary>
/// 1 Success 2 Faild ... /// 1 Success 2 Faild ...
......
...@@ -11,7 +11,7 @@ namespace Pole.Grpc.ExtraType ...@@ -11,7 +11,7 @@ namespace Pole.Grpc.ExtraType
{ {
public static implicit operator CommonCommandResponse(CompleteResult domainHandleResult) public static implicit operator CommonCommandResponse(CompleteResult domainHandleResult)
{ {
return new CompleteResult(domainHandleResult.Status, domainHandleResult.Message); return new CommonCommandResponse { Status = domainHandleResult.Status, Message = domainHandleResult.Message };
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment