# GeneralManagementSystem **Repository Path**: pys6/general-management-system ## Basic Information - **Project Name**: GeneralManagementSystem - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-01 - **Last Updated**: 2025-07-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 在整洁架构(Clean Architecture)中,项目被划分为多个层次,每个层次都有明确的职责和功能。这种分层结构有助于保持代码的清晰性和可维护性。以下是一个典型的整洁架构项目结构,展示各个层次的职责和位置,以及如何在.NET项目中实现这种结构。 典型的整洁架构项目结构 复制 MyProject ├── Domain │ ├── Entities │ │ └── EntityBase.cs │ ├── Aggregates │ ├── ValueObjects │ ├── Interfaces │ │ └── IMyProjectDbContext.cs │ └── ... ├── Application │ ├── Commands │ ├── Queries │ ├── Services │ │ └── ProductService.cs │ └── ... ├── Infrastructure │ ├── Data │ │ ├── DbContexts │ │ │ └── MyProjectDbContext.cs │ │ ├── Repositories │ │ └── ... │ ├── ExternalServices │ └── ... ├── WebApi │ ├── Controllers │ │ └── ProductController.cs │ ├── Startup.cs │ └── ... └── Tests 各层次的职责和功能 1. Domain层(领域层) 职责:包含业务逻辑和业务规则,是整个架构的核心。 内容: Entities:定义了业务实体,如 Product。 Aggregates:定义了聚合根,用于管理实体的集合。 ValueObjects:定义了值对象,用于表示不可变的业务概念。 Interfaces:定义了领域层需要的接口,如 IMyProjectDbContext,这些接口由基础设施层实现。 2. Application层(应用层) 职责:协调领域层和基础设施层,处理应用逻辑,如命令和查询的处理。 内容: Commands:定义了命令类,用于封装业务操作。 Queries:定义了查询类,用于封装数据查询。 Services:定义了应用服务,如 ProductService,用于处理具体的业务逻辑。 3. Infrastructure层(基础设施层) 职责:实现具体的技术细节,如数据库访问、外部服务调用等。 内容: Data:包含数据访问相关的代码,如 MyProjectDbContext 和仓库实现。 ExternalServices:包含外部服务的实现,如第三方API调用。 4. WebApi层(接口层) 职责:提供系统的外部接口,如REST API。 内容: Controllers:定义了控制器,如 ProductController,用于处理HTTP请求。 Startup.cs:配置了应用程序的依赖注入、中间件等。 示例代码 Domain层 csharp 复制 namespace MyProject.Domain.Entities { public abstract class EntityBase { public int Id { get; set; } public bool IsActived { get; set; } = true; public bool IsDeleted { get; set; } = false; public DateTime CreatedAt { get; set; } = DateTime.Now; public DateTime UpdatedAt { get; set; } = DateTime.Now; public int CreatedBy { get; set; } public int UpdatedBy { get; set; } public int DisplayOrder { get; set; } public string? Remarks { get; set; } } public class Product : EntityBase { public string Name { get; set; } public decimal Price { get; set; } public string Description { get; set; } } } Application层 csharp 复制 namespace MyProject.Application.Services { public class ProductService { private readonly IMyProjectDbContext _context; public ProductService(IMyProjectDbContext context) { _context = context; } public Product GetProductById(int id) { return _context.Products.FirstOrDefault(p => p.Id == id); } public void AddProduct(Product product) { _context.Products.Add(product); _context.SaveChanges(); } } } Infrastructure层 csharp 复制 using Microsoft.EntityFrameworkCore; using MyProject.Domain.Entities; using MyProject.Domain.Interfaces; namespace MyProject.Infrastructure.Data.DbContexts { public class MyProjectDbContext : DbContext, IMyProjectDbContext { public MyProjectDbContext(DbContextOptions options) : base(options) { } public DbSet Products { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity(entity => { entity.ToTable("Products"); entity.HasKey(p => p.Id); entity.Property(p => p.Name).IsRequired().HasMaxLength(100); entity.Property(p => p.Price).HasColumnType("decimal(18, 2)"); entity.Property(p => p.Description).IsRequired(false); }); } } } WebApi层 csharp 复制 using Microsoft.AspNetCore.Mvc; using MyProject.Application.Services; namespace MyProject.WebApi.Controllers { [ApiController] [Route("api/[controller]")] public class ProductController : ControllerBase { private readonly ProductService _productService; public ProductController(ProductService productService) { _productService = productService; } [HttpGet("{id}")] public IActionResult GetProduct(int id) { var product = _productService.GetProductById(id); if (product == null) { return NotFound(); } return Ok(product); } [HttpPost] public IActionResult AddProduct([FromBody] Product product) { _productService.AddProduct(product); return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product); } } } 总结 在整洁架构中,通过将项目划分为不同的层次,每个层次都有明确的职责和功能,有助于保持代码的清晰性和可维护性。这种结构使得业务逻辑与技术实现相分离,便于测试和扩展。在.NET项目中,这种结构通常通过分层项目来实现,每个层次都有其特定的职责和功能。