Skip to content

Commit

Permalink
Merge branch 'anjoy8:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Lu-Lucifer authored Oct 26, 2023
2 parents 20239a3 + e450254 commit c57166c
Show file tree
Hide file tree
Showing 75 changed files with 2,933 additions and 142 deletions.
25 changes: 25 additions & 0 deletions .dockerignore
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -331,4 +331,5 @@ ASALocalRun/
.mfractor/

# Local History for Visual Studio
.localhistory/
.localhistory/
Christ3D.UI.Web/dbCountPswSolsticeALDDD.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public DomainToViewModelMappingProfile()
.ForMember(d => d.Street, o => o.MapFrom(s => s.Address.Street))
;


CreateMap<Order, OrderViewModel>();
CreateMap<OrderItem, OrderItemViewModel>();
CreateMap<OrderItemViewModel, OrderItem>();


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public ViewModelToDomainMappingProfile()
CreateMap<StudentViewModel, UpdateStudentCommand>()
.ConstructUsing(c => new UpdateStudentCommand(c.Id, c.Name, c.Email, c.BirthDate, c.Phone, c.Province, c.City,
c.County, c.Street));


CreateMap<OrderViewModel, Order>();

CreateMap<OrderViewModel, RegisterOrderCommand>();
}
}
}
6 changes: 4 additions & 2 deletions Christ3D.Application/Christ3D.Application.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="8.0.0" />
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions Christ3D.Application/Services/IOrderAppService.cs
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();
}
}
71 changes: 71 additions & 0 deletions Christ3D.Application/Services/OrderAppService.cs
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);
}
}
}
20 changes: 20 additions & 0 deletions Christ3D.Application/ViewModels/OrderItemViewModel.cs
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; }



}
}
25 changes: 25 additions & 0 deletions Christ3D.Application/ViewModels/OrderViewModel.cs
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; }



}
}
6 changes: 3 additions & 3 deletions Christ3D.Domain.Core/Christ3D.Domain.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="8.1.2" />
<PackageReference Include="MediatR" Version="6.0.0" />
<PackageReference Include="FluentValidation" Version="8.6.2" />
<PackageReference Include="MediatR" Version="8.0.1" />
</ItemGroup>


Expand Down
2 changes: 1 addition & 1 deletion Christ3D.Domain.Core/Events/Message.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Christ3D.Domain.Core.Events
/// 抽象类Message,用来获取我们事件执行过程中的类名
/// 然后并且添加聚合根
/// </summary>
public abstract class Message : IRequest
public abstract class Message : IRequest<bool>
{
public string MessageType { get; protected set; }
public Guid AggregateId { get; protected set; }
Expand Down
8 changes: 6 additions & 2 deletions Christ3D.Domain/Christ3D.Domain.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.1" />
<Compile Remove="Commands\Order\OrderItem.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.3" />
</ItemGroup>

<ItemGroup>
Expand Down
101 changes: 101 additions & 0 deletions Christ3D.Domain/CommandHandlers/OrderCommandHandler.cs
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();
}
}
}
Loading

0 comments on commit c57166c

Please sign in to comment.