为 DotNetGuideBlog 补齐可运行的分层 Article CRUD 示例并打通项目依赖链#1
Merged
Conversation
Copilot
AI
changed the title
[WIP] Add standard three-tier architecture to DotNetGuideBlog
为 DotNetGuideBlog 补齐可运行的分层 Article CRUD 示例并打通项目依赖链
May 31, 2026
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
DotNetGuideBlog的 5 个分层项目此前是空壳且彼此无引用,导致分层无法协同。该 PR 按WebAPI → BLL → Repository → DAL → Model建立单向依赖,并补齐基于内存存储的 Article CRUD 示例,确保开箱可编译运行。项目依赖与包管理
.csproj中补充ProjectReference,形成标准单向依赖链(并允许各层使用 Model)。DotNetGuideBlogWebAPI增加Swashbuckle.AspNetCore依赖,用于 Swagger。Model 层:领域模型与契约
Entities/Article(Id/Title/Content/Author/CreateTime/UpdateTime/IsDeleted)。Dtos/ArticleDto、CreateArticleRequest、UpdateArticleRequest。Common/ApiResult<T>,统一Success/Message/Data与Ok/Fail工厂方法。DAL 层:线程安全内存存储
IArticleDal/ArticleDal。ConcurrentDictionary<int, Article>+Interlocked自增 ID 模拟数据库。GetListAsync/GetByIdAsync/AddAsync/UpdateAsync/DeleteAsync,删除采用软删除(IsDeleted)。Repository 层:数据访问封装
IArticleRepository/ArticleRepository。IArticleDal,对 DAL 能力进行仓储封装并暴露统一接口。BLL 层:业务编排与 DTO 映射
IArticleService/ArticleService。false/null。WebAPI 层:REST 接口与启动配置
Controllers/ArticleController:GET list、GET by id、POST、PUT、DELETE,统一返回ApiResult<T>。Program.cs:IArticleDal/ArticleDal注册为Singleton(共享内存数据)IArticleRepository、IArticleService注册为ScopedWeatherForecast.cs、WeatherForecastController.cs。示例(启动注入链):
Original prompt
背景
YyUpDaily/FastFramework是一款基于 .NET 8 的快速开发框架(fork 自 YSGStudyHards/FastFramework)。目前Platforms/DotNetGuideBlog平台已经创建了 5 个分层项目,但它们全部是空壳:DotNetGuideBlogWebAPI(表现层):只有默认模板Program.cs、WeatherForecast.cs,无实质逻辑DotNetGuideBlogBLL(业务逻辑层):空项目DotNetGuideBlogRepository(仓储层):空项目DotNetGuideBlogDAL(数据访问层):空项目DotNetGuideBlogModel(模型层):空项目关键问题:从
FastFramework.sln可以看出,这 5 个子项目之间没有任何ProjectReference依赖关系,各层是孤立的,无法协同工作。所有项目均为 net8.0,并启用了
ImplicitUsings和Nullable。WebAPI 项目使用Microsoft.NET.Sdk.Web,其余使用Microsoft.NET.Sdk。目标
请为
DotNetGuideBlog平台填充一套标准、可编译运行的三层架构 CRUD 示例,以"博客文章(Article)"为业务实体,遵循项目现有命名风格(项目名DotNetGuideBlog{层名},类用XxxService/IXxxService、XxxRepository/IXxxRepository、XxxDal/IXxxDal约定)。具体要求
1. 建立各层项目引用(依赖链)
请在各
.csproj中添加ProjectReference,建立单向依赖:DotNetGuideBlogModel:不引用任何项目DotNetGuideBlogDAL:引用ModelDotNetGuideBlogRepository:引用DAL、ModelDotNetGuideBlogBLL:引用Repository、ModelDotNetGuideBlogWebAPI:引用BLL、Model依赖方向:
WebAPI → BLL → Repository → DAL → Model(所有层都可引用 Model)。2. Model 层(DotNetGuideBlogModel)
Entities/Article.cs:博客文章实体(Id、Title、Content、Author、CreateTime、UpdateTime、IsDeleted 等字段)Dtos/ArticleDto.cs:用于查询返回的数据传输对象Dtos/CreateArticleRequest.cs、Dtos/UpdateArticleRequest.cs:创建/更新的请求模型Common/ApiResult.cs:通用返回结果包装类(含 Success、Message、Data,及 Ok/Fail 静态方法)3. DAL 层(DotNetGuideBlogDAL)
ConcurrentDictionary<int, Article>+Interlocked自增Id)**来模拟数据库,避免引入数据库依赖导致无法编译运行。IArticleDal/ArticleDal,提供GetListAsync、GetByIdAsync、AddAsync、UpdateAsync、DeleteAsync(删除用软删除 IsDeleted)。4. Repository 层(DotNetGuideBlogRepository)
IArticleRepository/ArticleRepository:通过构造函数注入IArticleDal,封装对 DAL 的调用,提供GetByIdAsync、GetListAsync、AddAsync、UpdateAsync、DeleteAsync。5. BLL 层(DotNetGuideBlogBLL)
IArticleService/ArticleService:通过构造函数注入IArticleRepository,编排仓储调用,处理实体与 DTO 的转换,包含基本业务校验(如标题不能为空、文章不存在时返回 false/null)。6. WebAPI 层(DotNetGuideBlogWebAPI)
Controllers/ArticleController.cs:提供 RESTful CRUD 接口(GET 列表、GET 单个、POST 创建、PUT 更新、DELETE 删除),统一返回ApiResult<T>。Program.cs:注册依赖注入,IArticleDal/ArticleDal注册为 Singleton(内存存储需共享数据),IArticleRepository和IArticleService注册为 Scoped;添加并启用 Swagger(开发环境)。WeatherForecast.cs示例文件。Swashbuckle.AspNetCore包引用(用于 Swagger)。验收标准
dotnet build成功)。Core、Shared项目和解决方案结构。注意事项
The following is the prior conversation context from the user's chat exploration (may be truncated):
User: .net ddd领域驱动分层标准,该怎么分层,每次该怎么命名,有什么作用,放什么东西
Assistant: # .NET DDD 领域驱动分层标准
DDD 经典分层是四层架构,在 .NET 中通常映射为多个项目(每层一个
.csproj)。下面给出业界主流的命名约定、职责和内容划分。一、四层架构总览
依赖方向(重点):所有层最终依赖 Domain,Domain 不依赖任何其他层。这就是 DDD 的核心原则——领域层保持纯净。
二、各层详解
1. Domain 层(领域层)★ 最核心
YourApp.Domain作用:承载所有核心业务逻辑和规则,与技术无关(不引用 EF Core、不引用 ASP.NET)。
放什么:
命名约定:
Order、CustomerI+ 聚合根 +Repository→IOrderRepositoryEvent→OrderCancelledEventxxxException2. Application 层(应用层)
YourApp.Application作用:编排业务用例(Use Case),协调领域对象完成一个完整操作。本身不含业务规则,只负责"流程指挥"。常配合 CQRS + MediatR。
放什么: