From 93236c4c2b0a315f79f00539bdfb06d7341a2afb Mon Sep 17 00:00:00 2001 From: dingsongjie Date: Thu, 16 Jan 2020 12:05:27 +0800 Subject: [PATCH] 简化 command --- samples/apis/Product.Api/Application/Command/AddProductTypeCommand.cs | 16 ---------------- samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs | 47 ----------------------------------------------- samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ samples/apis/Product.Api/Grpc/ProductTypeService.cs | 3 +-- src/Pole.Application/Command/Command.cs | 15 +++++++++++++++ 5 files changed, 63 insertions(+), 65 deletions(-) delete mode 100644 samples/apis/Product.Api/Application/Command/AddProductTypeCommand.cs delete mode 100644 samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs create mode 100644 samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs create mode 100644 src/Pole.Application/Command/Command.cs diff --git a/samples/apis/Product.Api/Application/Command/AddProductTypeCommand.cs b/samples/apis/Product.Api/Application/Command/AddProductTypeCommand.cs deleted file mode 100644 index 9647a16..0000000 --- a/samples/apis/Product.Api/Application/Command/AddProductTypeCommand.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Pole.Application.Command; -using Pole.Application.Cqrs; -using Pole.Grpc.ExtraType; -using PoleSample.Apis.Product; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Product.Api.Application.Command -{ - public class AddProductTypeCommand: ICommand - { - public AddProductTypeRequest Request { get; set; } - } -} diff --git a/samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs b/samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs deleted file mode 100644 index c23894d..0000000 --- a/samples/apis/Product.Api/Application/Command/CommandHandler/AddProductTypeCommandHandler.cs +++ /dev/null @@ -1,47 +0,0 @@ -using NewArchitectureLab.Apps.Product; -using Pole.Application.Command; -using Pole.Application.Cqrs; -using Pole.Domain.UnitOfWork; -using Pole.Grpc.ExtraType; -using PoleSample.Apis.Product; -using Product.Api.Domain.Event; -using Product.Api.Domain.ProductTypeAggregate; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace Product.Api.Application.Command.CommandHandler -{ - public class AddProductTypeCommandHandler : ICommandHandler - { - private readonly IProductTypeRepository _productTypeRepository; - private readonly IUnitOfWorkManager _unitOfWorkManager; - public AddProductTypeCommandHandler(IProductTypeRepository productTypeRepository, IUnitOfWorkManager unitOfWorkManager) - { - _productTypeRepository = productTypeRepository; - _unitOfWorkManager = unitOfWorkManager; - } - public async Task Handle(AddProductTypeCommand request, CancellationToken cancellationToken) - { - var productType = new Domain.ProductTypeAggregate.ProductType(request.Request.Id, request.Request.Name); - - - _productTypeRepository.Add(productType); - ProductTypeAddedDomainEvent productTypeAddedDomainEvent = new ProductTypeAddedDomainEvent - { - ProductTypeId = productType.Id, - ProductTypeName = productType.Name - }; - using(var unitOfWork= await _unitOfWorkManager.BeginUnitOfWork()) - { - productType.AddDomainEvent(productTypeAddedDomainEvent); - var result = await _productTypeRepository.SaveEntitiesAsync(); - - await unitOfWork.CompeleteAsync(); - return CommonCommandResponse.SuccessResponse; - } - } - } -} diff --git a/samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs b/samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs new file mode 100644 index 0000000..f41998d --- /dev/null +++ b/samples/apis/Product.Api/Application/CommandHandler/AddProductTypeCommandHandler.cs @@ -0,0 +1,47 @@ +using NewArchitectureLab.Apps.Product; +using Pole.Application.Command; +using Pole.Application.Cqrs; +using Pole.Domain.UnitOfWork; +using Pole.Grpc.ExtraType; +using PoleSample.Apis.Product; +using Product.Api.Domain.Event; +using Product.Api.Domain.ProductTypeAggregate; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace Product.Api.Application.CommandHandler +{ + public class AddProductTypeCommandHandler : ICommandHandler, CommonCommandResponse> + { + private readonly IProductTypeRepository _productTypeRepository; + private readonly IUnitOfWorkManager _unitOfWorkManager; + public AddProductTypeCommandHandler(IProductTypeRepository productTypeRepository, IUnitOfWorkManager unitOfWorkManager) + { + _productTypeRepository = productTypeRepository; + _unitOfWorkManager = unitOfWorkManager; + } + public async Task Handle(Command request, CancellationToken cancellationToken) + { + var productType = new Domain.ProductTypeAggregate.ProductType(request.Data.Id, request.Data.Name); + + + _productTypeRepository.Add(productType); + ProductTypeAddedDomainEvent productTypeAddedDomainEvent = new ProductTypeAddedDomainEvent + { + ProductTypeId = productType.Id, + ProductTypeName = productType.Name + }; + using(var unitOfWork= await _unitOfWorkManager.BeginUnitOfWork()) + { + productType.AddDomainEvent(productTypeAddedDomainEvent); + var result = await _productTypeRepository.SaveEntitiesAsync(); + + await unitOfWork.CompeleteAsync(); + return CommonCommandResponse.SuccessResponse; + } + } + } +} diff --git a/samples/apis/Product.Api/Grpc/ProductTypeService.cs b/samples/apis/Product.Api/Grpc/ProductTypeService.cs index 37fbd16..57568e9 100644 --- a/samples/apis/Product.Api/Grpc/ProductTypeService.cs +++ b/samples/apis/Product.Api/Grpc/ProductTypeService.cs @@ -4,7 +4,6 @@ using Pole.Application.Command; using Pole.Application.Cqrs; using Pole.Grpc.ExtraType; using PoleSample.Apis.Product; -using Product.Api.Application.Command; using Product.Api.Infrastructure; using System; using System.Collections.Generic; @@ -22,7 +21,7 @@ namespace Product.Api.Grpc } public override Task Add(AddProductTypeRequest request, ServerCallContext context) { - var cpmmand = new AddProductTypeCommand { Request = request }; + var cpmmand = new Command(request); return _commandBus.Send(cpmmand); } } diff --git a/src/Pole.Application/Command/Command.cs b/src/Pole.Application/Command/Command.cs new file mode 100644 index 0000000..c71ac02 --- /dev/null +++ b/src/Pole.Application/Command/Command.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Pole.Application.Command +{ + public class Command:ICommand + { + public Command(TRequest request) + { + Data = request; + } + public TRequest Data { get; private set; } + } +} -- libgit2 0.25.0