# CSharpInit **Repository Path**: jergon/csharp-init ## Basic Information - **Project Name**: CSharpInit - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-26 - **Last Updated**: 2025-11-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: Web ## README # CRM WebAPI 项目 基于 .NET 8.0 和 SqlSugar 的分层架构 WebAPI 项目。 ## 项目结构 ``` crm/ ├── crm.Core/ # 核心层(实体、枚举、常量) │ ├── Entities/ │ │ └── Entity.cs # 基础实体类(Id、CreatedAt、UpdatedAt、DeletedAt) │ └── Common/ # 通用模型(ApiResult、PagedResult) ├── crm.DAL/ # 数据访问层 │ ├── Context/ │ │ └── SqlSugarDbContext.cs # SqlSugar上下文 │ ├── Repositories/ │ │ ├── IBaseRepository.cs # 通用仓储接口 │ │ └── BaseRepository.cs # 通用仓储实现 │ └── Migrations/ # 数据库迁移(CodeFirst版本维护) ├── crm.BLL/ # 业务逻辑层(按业务模块组织) │ ├── Modules/ # 业务模块 │ │ └── User/ # 用户模块示例 │ │ ├── Controllers/ │ │ ├── DTOs/ │ │ ├── Interfaces/ │ │ ├── Services/ │ │ └── Entities/ │ └── Extensions/ │ └── ServiceCollectionExtensions.cs # BLL服务注册扩展 └── crm.API/ # API层 ├── Middleware/ # 中间件 ├── Services/ # API服务 └── Extensions/ # 扩展方法 ``` ## 核心特性 ### 1. 基础实体类 (Entity) 所有实体必须继承 `Entity` 基类,包含以下字段: - `Id` (int, 自增主键) - `CreatedAt` (DateTime?, 创建时间) - `UpdatedAt` (DateTime?, 更新时间) - `DeletedAt` (DateTime?, 删除时间,用于软删除) ### 2. 通用仓储模式 提供 `IBaseRepository` 接口和 `BaseRepository` 实现,包含以下方法: - `CreateOrUpdateAsync(T entity)` - 新增或更新(根据Id判断) - `GetByIdAsync(int id)` - 根据Id获取实体(排除已删除) - `GetListAsync()` - 获取列表(排除已删除) - `GetPagedListAsync(...)` - 分页查询(排除已删除) - `SoftDeleteAsync(int id)` - 软删除(设置DeletedAt) ### 3. 业务模块组织 每个业务模块独立文件夹,包含: - `Controllers/` - API控制器 - `DTOs/` - 数据传输对象 - `Interfaces/` - 服务接口 - `Services/` - 服务实现 - `Entities/` - 实体类 ### 4. 服务注册 使用扩展方法批量注册服务: - `AddSqlSugar()` - 注册SqlSugar - `AddRepositories()` - 注册仓储服务 - `AddBllServices()` - 自动扫描并注册所有BLL服务 - `AddJwtAuthentication()` - 注册JWT认证 ## 配置 ### 1. 数据库连接 在 `appsettings.json` 中配置数据库连接字符串: ```json { "ConnectionStrings": { "DefaultConnection": "Server=localhost;Port=3306;Database=crm;Uid=root;Pwd=yourpassword;CharSet=utf8mb4;" } } ``` ### 2. JWT配置 在 `appsettings.json` 中配置JWT: ```json { "Jwt": { "SecretKey": "YourSecretKeyForJWTTokenGenerationMustBeAtLeast32CharactersLong", "Issuer": "crm", "Audience": "crm", "ExpireHours": "24" } } ``` ## 使用示例 ### 创建新业务模块 1. 在 `crm.BLL/Modules/` 下创建模块文件夹(如 `Product`) 2. 创建实体类,继承 `Entity` 3. 创建DTOs(CreateDto、UpdateDto、ResponseDto) 4. 创建服务接口和实现 5. 创建控制器 ### 使用通用仓储 ```csharp public class ProductService : IProductService { private readonly IBaseRepository _productRepository; public ProductService(IBaseRepository productRepository) { _productRepository = productRepository; } public async Task CreateProductAsync(Product product) { product.Id = 0; // 新增时Id为0 return await _productRepository.CreateOrUpdateAsync(product); } } ``` ## 技术栈 - .NET 8.0 - SqlSugar ORM - MySQL数据库 - JWT认证 - Swagger/OpenAPI ## 运行项目 1. 配置数据库连接字符串(在 `appsettings.json` 或 `appsettings.Development.json` 中) 2. 运行项目: ```bash dotnet run --launch-profile http ``` 数据库和表会自动创建(CodeFirst),启动日志中会显示初始化信息 3. 访问 Swagger UI:`http://localhost:8076/swagger` **注意:** - 首次运行会自动创建数据库和表结构 - 如果数据库初始化失败,请检查连接字符串和数据库权限 - 查看日志文件 `logs/info-{date}.log` 了解详细信息