forked from anjoy8/ChristDDD
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'anjoy8:master' into master
- Loading branch information
Showing
75 changed files
with
2,933 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
**/.classpath | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/azds.yaml | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml | ||
LICENSE | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Christ3D.Application.EventSourcedNormalizers; | ||
using Christ3D.Application.ViewModels; | ||
|
||
namespace Christ3D.Application.Interfaces | ||
{ | ||
/// <summary> | ||
/// 定义 IOrderAppService 服务接口 | ||
/// 并继承IDisposable,显式释放资源 | ||
/// 注意这里我们使用的对象,是视图对象模型 | ||
/// </summary> | ||
public interface IOrderAppService : IDisposable | ||
{ | ||
void Register(OrderViewModel OrderViewModel); | ||
IEnumerable<OrderViewModel> GetAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using AutoMapper; | ||
using AutoMapper.QueryableExtensions; | ||
using Christ3D.Application.EventSourcedNormalizers; | ||
using Christ3D.Application.Interfaces; | ||
using Christ3D.Application.ViewModels; | ||
using Christ3D.Domain.Commands; | ||
using Christ3D.Domain.Core.Bus; | ||
using Christ3D.Domain.Interfaces; | ||
using Christ3D.Domain.Models; | ||
using Christ3D.Infra.Data.Repository.EventSourcing; | ||
|
||
namespace Christ3D.Application.Services | ||
{ | ||
/// <summary> | ||
/// OrderAppService 服务接口实现类,继承 服务接口 | ||
/// 通过 DTO 实现视图模型和领域模型的关系处理 | ||
/// 作为调度者,协调领域层和基础层, | ||
/// 这里只是做一个面向用户用例的服务接口,不包含业务规则或者知识 | ||
/// </summary> | ||
public class OrderAppService : IOrderAppService | ||
{ | ||
// 注意这里是要IoC依赖注入的,还没有实现 | ||
private readonly IOrderRepository _OrderRepository; | ||
// 用来进行DTO | ||
private readonly IMapper _mapper; | ||
// 中介者 总线 | ||
private readonly IMediatorHandler Bus; | ||
// 事件源仓储 | ||
private readonly IEventStoreRepository _eventStoreRepository; | ||
|
||
public OrderAppService( | ||
IOrderRepository OrderRepository, | ||
IMapper mapper, | ||
IMediatorHandler bus, | ||
IEventStoreRepository eventStoreRepository | ||
) | ||
{ | ||
_OrderRepository = OrderRepository; | ||
_mapper = mapper; | ||
Bus = bus; | ||
_eventStoreRepository = eventStoreRepository; | ||
} | ||
|
||
public IEnumerable<OrderViewModel> GetAll() | ||
{ | ||
var orders = _OrderRepository.GetAll(); | ||
var orderList = orders.ToList(); | ||
return _mapper.Map<IEnumerable<OrderViewModel>>(orders); | ||
} | ||
|
||
public OrderViewModel GetById(Guid id) | ||
{ | ||
return _mapper.Map<OrderViewModel>(_OrderRepository.GetById(id)); | ||
} | ||
|
||
public void Register(OrderViewModel OrderViewModel) | ||
{ | ||
var registerCommand = _mapper.Map<RegisterOrderCommand>(OrderViewModel); | ||
Bus.SendCommand(registerCommand); | ||
} | ||
|
||
|
||
public void Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Christ3D.Domain.Models; | ||
using System; | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Christ3D.Application.ViewModels | ||
{ | ||
public class OrderItemViewModel | ||
{ | ||
|
||
[Required(ErrorMessage = "The Item Name is Required")] | ||
[MinLength(2)] | ||
[MaxLength(100)] | ||
[DisplayName("Name")] | ||
public string Name { get; set; } | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Christ3D.Domain.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Christ3D.Application.ViewModels | ||
{ | ||
public class OrderViewModel | ||
{ | ||
[Key] | ||
public Guid Id { get; set; } | ||
|
||
[Required(ErrorMessage = "The Order Name is Required")] | ||
[MinLength(2)] | ||
[MaxLength(100)] | ||
[DisplayName("Name")] | ||
public string Name { get; set; } | ||
|
||
public List<OrderItemViewModel> Items { get; set; } | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Christ3D.Domain.Commands; | ||
using Christ3D.Domain.Core.Bus; | ||
using Christ3D.Domain.Core.Notifications; | ||
using Christ3D.Domain.Events; | ||
using Christ3D.Domain.Interfaces; | ||
using Christ3D.Domain.Models; | ||
using MediatR; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace Christ3D.Domain.CommandHandlers | ||
{ | ||
/// <summary> | ||
/// Order命令处理程序 | ||
/// 用来处理该Order下的所有命令 | ||
/// 注意必须要继承接口IRequestHandler<,>,这样才能实现各个命令的Handle方法 | ||
/// </summary> | ||
public class OrderCommandHandler : CommandHandler, | ||
IRequestHandler<RegisterOrderCommand, bool> | ||
{ | ||
// 注入仓储接口 | ||
private readonly IOrderRepository _OrderRepository; | ||
// 注入总线 | ||
private readonly IMediatorHandler Bus; | ||
private IMemoryCache Cache; | ||
|
||
/// <summary> | ||
/// 构造函数注入 | ||
/// </summary> | ||
/// <param name="OrderRepository"></param> | ||
/// <param name="uow"></param> | ||
/// <param name="bus"></param> | ||
/// <param name="cache"></param> | ||
public OrderCommandHandler(IOrderRepository OrderRepository, | ||
IUnitOfWork uow, | ||
IMediatorHandler bus, | ||
IMemoryCache cache | ||
) : base(uow, bus, cache) | ||
{ | ||
_OrderRepository = OrderRepository; | ||
Bus = bus; | ||
Cache = cache; | ||
} | ||
|
||
|
||
// RegisterOrderCommand命令的处理程序 | ||
// 整个命令处理程序的核心都在这里 | ||
// 不仅包括命令验证的收集,持久化,还有领域事件和通知的添加 | ||
public Task<bool> Handle(RegisterOrderCommand message, CancellationToken cancellationToken) | ||
{ | ||
// 命令验证 | ||
if (!message.IsValid()) | ||
{ | ||
// 错误信息收集 | ||
NotifyValidationErrors(message); | ||
// 返回,结束当前线程 | ||
return Task.FromResult(false); | ||
} | ||
|
||
// 实例化领域模型,这里才真正的用到了领域模型 | ||
// 注意这里是通过构造函数方法实现 | ||
|
||
var Order = new Order(Guid.NewGuid(),message.Name, message.Items); | ||
|
||
// 判断邮箱是否存在 | ||
// 这些业务逻辑,当然要在领域层中(领域命令处理程序中)进行处理 | ||
if (_OrderRepository.GetByName(Order.Name) != null) | ||
{ | ||
|
||
//引发错误事件 | ||
Bus.RaiseEvent(new DomainNotification("", "该Name已经被使用!")); | ||
return Task.FromResult(false); | ||
} | ||
|
||
// 持久化 | ||
_OrderRepository.Add(Order); | ||
|
||
// 统一提交 | ||
if (Commit()) | ||
{ | ||
// 提交成功后,这里需要发布领域事件 | ||
// 比如欢迎用户注册邮件呀,短信呀等 | ||
|
||
//Bus.RaiseEvent(new OrderRegisteredEvent(Order.Id, Order.Name, Order.Email, Order.BirthDate, Order.Phone)); | ||
} | ||
|
||
return Task.FromResult(true); | ||
|
||
} | ||
|
||
|
||
// 手动回收 | ||
public void Dispose() | ||
{ | ||
_OrderRepository.Dispose(); | ||
} | ||
} | ||
} |
Oops, something went wrong.