完善DDD领域驱动设计

This commit is contained in:
橙子
2023-01-26 21:00:01 +08:00
parent 38d69c9e6f
commit dfefd0452d
16 changed files with 175 additions and 35 deletions

View File

@@ -0,0 +1,27 @@
using Autofac.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using StartupModules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Core.Autofac
{
public class YiFrameworkCoreAutofacModule : IStartupModule
{
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
{
}
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
{
//将控制器交由autofac注册
services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
}
}
}

View File

@@ -14,6 +14,11 @@
Banner抽象 Banner抽象
</summary> </summary>
</member> </member>
<member name="T:Yi.BBS.Application.Contracts.Forum.Dtos.CommentCreateInputVo">
<summary>
Comment输入创建对象
</summary>
</member>
<member name="T:Yi.BBS.Application.Contracts.Forum.Dtos.Discuss.DiscussCreateInputVo"> <member name="T:Yi.BBS.Application.Contracts.Forum.Dtos.Discuss.DiscussCreateInputVo">
<summary> <summary>
Discuss输入创建对象 Discuss输入创建对象
@@ -24,6 +29,11 @@
Plate输入创建对象 Plate输入创建对象
</summary> </summary>
</member> </member>
<member name="T:Yi.BBS.Application.Contracts.Forum.ICommentService">
<summary>
Comment服务抽象
</summary>
</member>
<member name="T:Yi.BBS.Application.Contracts.Forum.IDiscussService"> <member name="T:Yi.BBS.Application.Contracts.Forum.IDiscussService">
<summary> <summary>
Discuss服务抽象 Discuss服务抽象

View File

@@ -9,9 +9,30 @@
Banner服务实现 Banner服务实现
</summary> </summary>
</member> </member>
<member name="T:Yi.BBS.Application.Forum.CommentService">
<summary>
Comment服务实现
</summary>
</member>
<member name="M:Yi.BBS.Application.Forum.CommentService.GetDiscussIdAsync(System.Int64,Yi.BBS.Application.Contracts.Forum.Dtos.CommentGetListInputVo)">
<summary>
获取改主题下的评论
</summary>
<param name="discussId"></param>
<param name="input"></param>
<returns></returns>
</member>
<member name="M:Yi.BBS.Application.Forum.CommentService.CreateAsync(Yi.BBS.Application.Contracts.Forum.Dtos.CommentCreateInputVo)">
<summary>
发表评论
</summary>
<param name="input"></param>
<returns></returns>
<exception cref="T:Yi.Framework.Core.Exceptions.UserFriendlyException"></exception>
</member>
<member name="T:Yi.BBS.Application.Forum.DiscussService"> <member name="T:Yi.BBS.Application.Forum.DiscussService">
<summary> <summary>
Discuss服务实现 Discuss应用服务实现,用于参数效验、领域服务业务组合、日志记录、事务处理、账户信息
</summary> </summary>
</member> </member>
<member name="M:Yi.BBS.Application.Forum.DiscussService.GetPlateIdAsync(System.Int64,Yi.BBS.Application.Contracts.Forum.Dtos.Discuss.DiscussGetListInputVo)"> <member name="M:Yi.BBS.Application.Forum.DiscussService.GetPlateIdAsync(System.Int64,Yi.BBS.Application.Contracts.Forum.Dtos.Discuss.DiscussGetListInputVo)">
@@ -22,11 +43,10 @@
<param name="input"></param> <param name="input"></param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:Yi.BBS.Application.Forum.DiscussService.CreatePlateIdAsync(System.Int64,Yi.BBS.Application.Contracts.Forum.Dtos.Discuss.DiscussCreateInputVo)"> <member name="M:Yi.BBS.Application.Forum.DiscussService.CreateAsync(Yi.BBS.Application.Contracts.Forum.Dtos.Discuss.DiscussCreateInputVo)">
<summary> <summary>
创建主题 创建主题
</summary> </summary>
<param name="plateId"></param>
<param name="input"></param> <param name="input"></param>
<returns></returns> <returns></returns>
</member> </member>

View File

@@ -3,6 +3,8 @@ using Yi.Framework.Core.Autofac.Extensions;
using Yi.Framework.Core.Autofac.Modules; using Yi.Framework.Core.Autofac.Modules;
using Yi.Framework.Core.Extensions; using Yi.Framework.Core.Extensions;
using Yi.BBS.Web; using Yi.BBS.Web;
using Yi.BBS.Application;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseStartUrlsServer(builder.Configuration); builder.WebHost.UseStartUrlsServer(builder.Configuration);
@@ -11,9 +13,10 @@ builder.UseYiModules(typeof(YiBBSWebModule));
builder.Host.ConfigureAutoFacContainer(container => builder.Host.ConfigureAutoFacContainer(container =>
{ {
container.RegisterYiModule(AutoFacModuleEnum.PropertiesAutowiredModule, typeof(YiBBSWebModule).Assembly); container.RegisterYiModule(AutoFacModuleEnum.PropertiesAutowiredModule,typeof(YiBBSWebModule).Assembly , typeof(YiBBSApplicationModule).Assembly);
}); });
var app = builder.Build(); var app = builder.Build();
app.UseErrorHandlingServer(); app.UseErrorHandlingServer();

View File

@@ -6,10 +6,12 @@ using Yi.Framework.Core.Attributes;
using Yi.BBS.Application; using Yi.BBS.Application;
using Yi.BBS.Sqlsugar; using Yi.BBS.Sqlsugar;
using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection; using Yi.Framework.AspNetCore.Microsoft.Extensions.DependencyInjection;
using Yi.Framework.Core.Autofac;
namespace Yi.BBS.Web namespace Yi.BBS.Web
{ {
[DependsOn( [DependsOn(
typeof(YiFrameworkCoreAutofacModule),
typeof(YiBBSSqlsugarModule), typeof(YiBBSSqlsugarModule),
typeof(YiBBSApplicationModule) typeof(YiBBSApplicationModule)
)] )]

View File

@@ -11,10 +11,7 @@ namespace Yi.BBS.Application.Contracts.Forum.Dtos
/// </summary> /// </summary>
public class CommentCreateInputVo public class CommentCreateInputVo
{ {
public long Id { get; set; }
public DateTime? CreateTime { get; set; }
public string Content { get; set; } public string Content { get; set; }
public long DiscussId { get; set; } public long DiscussId { get; set; }
public long UserId { get; set; }
} }
} }

View File

@@ -9,10 +9,5 @@ namespace Yi.BBS.Application.Contracts.Forum.Dtos
{ {
public class CommentGetListInputVo : PagedAndSortedResultRequestDto public class CommentGetListInputVo : PagedAndSortedResultRequestDto
{ {
public long Id { get; set; }
public DateTime? CreateTime { get; set; }
public string Content { get; set; }
public long DiscussId { get; set; }
public long UserId { get; set; }
} }
} }

View File

@@ -17,5 +17,7 @@ namespace Yi.BBS.Application.Contracts.Forum.Dtos.Discuss
public DateTime? CreateTime { get; set; } public DateTime? CreateTime { get; set; }
public string Content { get; set; } public string Content { get; set; }
public string? Color { get; set; } public string? Color { get; set; }
public long plateId { get; set; }
} }
} }

View File

@@ -3,6 +3,13 @@ using NET.AutoWebApi.Setting;
using Yi.BBS.Application.Contracts.Forum.Dtos; using Yi.BBS.Application.Contracts.Forum.Dtos;
using Yi.BBS.Domain.Forum.Entities; using Yi.BBS.Domain.Forum.Entities;
using Yi.Framework.Ddd.Services; using Yi.Framework.Ddd.Services;
using Microsoft.AspNetCore.Mvc;
using Yi.BBS.Domain.Forum;
using Yi.Framework.Core.CurrentUsers;
using Yi.Framework.Ddd.Repositories;
using Yi.BBS.Domain.Shared.Forum.ConstClasses;
using Yi.BBS.Application.Contracts.Forum.Dtos.Discuss;
using Yi.Framework.Ddd.Dtos;
namespace Yi.BBS.Application.Forum namespace Yi.BBS.Application.Forum
{ {
@@ -13,5 +20,52 @@ namespace Yi.BBS.Application.Forum
public class CommentService : CrudAppService<CommentEntity, CommentGetOutputDto, CommentGetListOutputDto, long, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>, public class CommentService : CrudAppService<CommentEntity, CommentGetOutputDto, CommentGetListOutputDto, long, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>,
ICommentService, IAutoApiService ICommentService, IAutoApiService
{ {
[Autowired]
private ForumManager _forumManager { get; set; }
[Autowired]
private ICurrentUser _currentUser { get; set; }
[Autowired]
private IRepository<DiscussEntity> _discussRepository { get; set; }
/// <summary>
/// 获取改主题下的评论
/// </summary>
/// <param name="discussId"></param>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PagedResultDto<CommentGetListOutputDto>> GetDiscussIdAsync([FromRoute] long discussId, [FromQuery] CommentGetListInputVo input)
{
var entities = await _repository.GetPageListAsync(x => x.DiscussId == discussId, input);
var items = await MapToGetListOutputDtosAsync(entities);
var total = await _repository.CountAsync(x => x.IsDeleted == false);
return new PagedResultDto<CommentGetListOutputDto>(total, items);
}
/// <summary>
/// 发表评论
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public override async Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input)
{
//if (_currentUser.Id == default(long))
//{
// throw new UserFriendlyException("用户不存在");
//}
if (!await _discussRepository.IsAnyAsync(x => x.Id == input.DiscussId))
{
throw new UserFriendlyException(DiscussConst.);
}
var entity = await _forumManager.CreateCommentAsync(input.DiscussId, _currentUser.Id, input.Content);
return await MapToGetOutputDtoAsync(entity);
}
} }
} }

View File

@@ -10,21 +10,23 @@ using Yi.BBS.Domain.Forum;
using AutoMapper; using AutoMapper;
using SqlSugar; using SqlSugar;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Yi.BBS.Domain.Shared.Forum.ConstClasses;
using Yi.Framework.Ddd.Repositories;
namespace Yi.BBS.Application.Forum namespace Yi.BBS.Application.Forum
{ {
/// <summary> /// <summary>
/// Discuss服务实现 /// Discuss应用服务实现,用于参数效验、领域服务业务组合、日志记录、事务处理、账户信息
/// </summary> /// </summary>
[AppService] [AppService]
public class DiscussService : CrudAppService<DiscussEntity, DiscussGetOutputDto, DiscussGetListOutputDto, long, DiscussGetListInputVo, DiscussCreateInputVo, DiscussUpdateInputVo>, public class DiscussService : CrudAppService<DiscussEntity, DiscussGetOutputDto, DiscussGetListOutputDto, long, DiscussGetListInputVo, DiscussCreateInputVo, DiscussUpdateInputVo>,
IDiscussService, IAutoApiService IDiscussService, IAutoApiService
{ {
private readonly ForumManager _forumManager; [Autowired]
public DiscussService(ForumManager forumManager) private ForumManager _forumManager { get; set; }
{
_forumManager = forumManager; [Autowired]
} private IRepository<PlateEntity> _plateEntityRepository { get; set; }
/// <summary> /// <summary>
/// 获取改板块下的主题 /// 获取改板块下的主题
@@ -41,21 +43,18 @@ namespace Yi.BBS.Application.Forum
return new PagedResultDto<DiscussGetListOutputDto>(total, items); return new PagedResultDto<DiscussGetListOutputDto>(total, items);
} }
[AutoApi(false)]
public override Task<DiscussGetOutputDto> CreateAsync(DiscussCreateInputVo input)
{
return base.CreateAsync(input);
}
/// <summary> /// <summary>
/// 创建主题 /// 创建主题
/// </summary> /// </summary>
/// <param name="plateId"></param>
/// <param name="input"></param> /// <param name="input"></param>
/// <returns></returns> /// <returns></returns>
public async Task<DiscussGetOutputDto> CreatePlateIdAsync([FromRoute] long plateId, DiscussCreateInputVo input) public override async Task<DiscussGetOutputDto> CreateAsync(DiscussCreateInputVo input)
{ {
var entity = await _forumManager.CreateDiscussAsync(plateId, input.Title, input.Types, input.Content, input.Introduction); if (!await _plateEntityRepository.IsAnyAsync(x => x.Id == input.plateId))
{
throw new UserFriendlyException(PlateConst.);
}
var entity = await _forumManager.CreateDiscussAsync(input.plateId, input.Title, input.Types, input.Content, input.Introduction);
return await MapToGetOutputDtoAsync(entity); return await MapToGetOutputDtoAsync(entity);
} }
} }

View File

@@ -12,5 +12,6 @@ namespace Yi.BBS.Domain.Shared.Forum.ConstClasses
public class DiscussConst public class DiscussConst
{ {
public const string = "传入的主题id不存在";
} }
} }

View File

@@ -12,6 +12,16 @@ namespace Yi.BBS.Domain.Forum.Entities
[SugarTable("Comment")] [SugarTable("Comment")]
public class CommentEntity : IEntity<long>, ISoftDelete public class CommentEntity : IEntity<long>, ISoftDelete
{ {
public CommentEntity()
{
}
internal CommentEntity(long discussId, long userId)
{
DiscussId= discussId;
UserId = userId;
}
[SugarColumn(IsPrimaryKey = true)] [SugarColumn(IsPrimaryKey = true)]
public long Id { get; set; } public long Id { get; set; }
public bool IsDeleted { get; set; } public bool IsDeleted { get; set; }

View File

@@ -17,20 +17,17 @@ namespace Yi.BBS.Domain.Forum
{ {
private readonly IRepository<DiscussEntity> _discussRepository; private readonly IRepository<DiscussEntity> _discussRepository;
private readonly IRepository<PlateEntity> _plateEntityRepository; private readonly IRepository<PlateEntity> _plateEntityRepository;
private readonly IRepository<CommentEntity> commentRepository; private readonly IRepository<CommentEntity> _commentRepository;
public ForumManager(IRepository<DiscussEntity> discussRepository, IRepository<PlateEntity> plateEntityRepository,IRepository<CommentEntity> commentRepository) public ForumManager(IRepository<DiscussEntity> discussRepository, IRepository<PlateEntity> plateEntityRepository, IRepository<CommentEntity> commentRepository)
{ {
_discussRepository = discussRepository; _discussRepository = discussRepository;
_plateEntityRepository = plateEntityRepository; _plateEntityRepository = plateEntityRepository;
_commentRepository = commentRepository;
} }
//主题是不能直接创建的,需要由领域服务统一创建 //主题是不能直接创建的,需要由领域服务统一创建
public async Task<DiscussEntity> CreateDiscussAsync(long plateId, string title, string types, string content, string? introduction = null) public async Task<DiscussEntity> CreateDiscussAsync(long plateId, string title, string types, string content, string? introduction = null)
{ {
if (!await _plateEntityRepository.IsAnyAsync(x => x.Id == plateId))
{
throw new UserFriendlyException(PlateConst.);
}
var entity = new DiscussEntity(plateId); var entity = new DiscussEntity(plateId);
entity.Id = SnowflakeHelper.NextId; entity.Id = SnowflakeHelper.NextId;
entity.Title = title; entity.Title = title;
@@ -38,9 +35,18 @@ namespace Yi.BBS.Domain.Forum
entity.Introduction = introduction; entity.Introduction = introduction;
entity.Content = content; entity.Content = content;
entity.CreateTime = DateTime.Now; entity.CreateTime = DateTime.Now;
entity.AgreeNum= 0; entity.AgreeNum = 0;
entity.SeeNum= 0; entity.SeeNum = 0;
return await _discussRepository.InsertReturnEntityAsync(entity); return await _discussRepository.InsertReturnEntityAsync(entity);
} }
public async Task<CommentEntity> CreateCommentAsync(long discussId, long userId, string content)
{
var entity = new CommentEntity(discussId, userId);
entity.Id = SnowflakeHelper.NextId;
entity.Content = content;
entity.CreateTime = DateTime.Now;
return await _commentRepository.InsertReturnEntityAsync(entity);
}
} }
} }

View File

@@ -0,0 +1,8 @@
namespace Yi.BBS.Web
{
public class TestAutofac
{
[Autowired]
public TestAutofac2 _testAutofac2 { get; set; }
}
}

View File

@@ -0,0 +1,6 @@
namespace Yi.BBS.Web
{
public class TestAutofac2
{
}
}