diff --git a/backend/IM_API/Configs/ServiceCollectionExtensions.cs b/backend/IM_API/Configs/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..b7ee2c6 --- /dev/null +++ b/backend/IM_API/Configs/ServiceCollectionExtensions.cs @@ -0,0 +1,10 @@ +namespace IM_API.Configs +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddAllService(this IServiceCollection services, IConfiguration configuration) + { + return services; + } + } +} diff --git a/backend/IM_API/Dtos/TestDto.cs b/backend/IM_API/Dtos/TestDto.cs new file mode 100644 index 0000000..bd689d6 --- /dev/null +++ b/backend/IM_API/Dtos/TestDto.cs @@ -0,0 +1,8 @@ +namespace IM_API.Dtos +{ + public class TestDto + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/backend/IM_API/Interface/Services/IDemo.cs b/backend/IM_API/Interface/Services/IDemo.cs new file mode 100644 index 0000000..75bf461 --- /dev/null +++ b/backend/IM_API/Interface/Services/IDemo.cs @@ -0,0 +1,9 @@ +using IM_API.Dtos; + +namespace IM_API.Interface.Services +{ + public interface IDemo + { + TestDto Test(); + } +} diff --git a/backend/IM_API/Models/IMDbContext.cs b/backend/IM_API/Models/IMDbContext.cs index 39bc38f..5215258 100644 --- a/backend/IM_API/Models/IMDbContext.cs +++ b/backend/IM_API/Models/IMDbContext.cs @@ -50,10 +50,6 @@ public partial class IMDbContext : DbContext public virtual DbSet Users { get; set; } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) -#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263. - => optionsBuilder.UseMySql("server=192.168.5.100;port=3306;database=IM;user=root;password=768788Dyw", Microsoft.EntityFrameworkCore.ServerVersion.Parse("5.7.44-mysql")); - protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder diff --git a/backend/IM_API/Program.cs b/backend/IM_API/Program.cs index 96341b4..e906aee 100644 --- a/backend/IM_API/Program.cs +++ b/backend/IM_API/Program.cs @@ -1,4 +1,8 @@ +using IM_API.Configs; +using IM_API.Models; +using Microsoft.EntityFrameworkCore; + namespace IM_API { public class Program @@ -8,7 +12,17 @@ namespace IM_API var builder = WebApplication.CreateBuilder(args); // Add services to the container. + IConfiguration configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + .Build(); + string conStr = builder.Configuration.GetConnectionString("DefaultConnection")!; + builder.Services.AddDbContext(options => + { + options.UseMySql(conStr,ServerVersion.AutoDetect(conStr)); + }); + builder.Services.AddAllService(configuration); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); diff --git a/backend/IM_API/Services/Demo.cs b/backend/IM_API/Services/Demo.cs new file mode 100644 index 0000000..4c12ef6 --- /dev/null +++ b/backend/IM_API/Services/Demo.cs @@ -0,0 +1,13 @@ +using IM_API.Dtos; +using IM_API.Interface.Services; + +namespace IM_API.Services +{ + public class Demo : IDemo + { + public TestDto Test() + { + throw new NotImplementedException(); + } + } +} diff --git a/backend/IM_API/appsettings.json b/backend/IM_API/appsettings.json index 10f68b8..eb0a86a 100644 --- a/backend/IM_API/appsettings.json +++ b/backend/IM_API/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ConnectionStrings": { + "DefaultConnection": "Server=192.168.5.100;Port=3306;Database=IM;User=root;Password=768788Dyw;" + } } diff --git a/docs/后端代码规范文档.md b/docs/后端代码规范文档.md new file mode 100644 index 0000000..6dbdf9a --- /dev/null +++ b/docs/后端代码规范文档.md @@ -0,0 +1,69 @@ +# 项目规范文档 + +## 1. 命名规范 + +* **全局命名** + + * 采用 **小驼峰命名法**(camelCase),例如:`aBbCc`。 + * 特殊情况除外,例如 **.NET 编译器要求方法名使用大驼峰**(PascalCase)。 + +* **各层命名规范** + + * **Controllers**:自定义命名 + `Controller` + * **Services**:自定义命名 + `Service` + * **Dtos**:自定义命名 + `Dto` + +--- + +## 2. 文件/文件夹规范 + +| 文件夹 | 功能描述 | 注意事项 | +| -------------------- | -------------------------- | -------------------------------------------- | +| **Controllers** | 存放控制器和 Actions,用于处理请求和响应 | 禁止直接操作数据库。如需数据库操作,应先在 Services 层编写业务逻辑再调用 | +| **Services** | 存放业务逻辑代码,包括数据库交互 | 应在 Service 层处理所有业务逻辑,保证 Controller 层纯粹用于请求处理 | +| **Dtos** | 存放不同层之间的数据传输模型(DTO) | 用于数据类型转换,例如返回用户信息时需剔除密码等敏感信息。禁止直接返回数据库模型类 | +| **Models** | 存放数据库模型类(Entity) | 一般情况下请勿随意修改 | +| **appsettings.json** | 存放配置文件,如数据库连接字符串、Redis 配置等 | 禁止在业务代码中硬编码配置信息,统一放在此文件中 | + +--- + +## 3. Service 编写与使用规范 + +### 3.1 编写 Service + +1. 在 `Interface/Services` 文件夹下创建接口,定义业务方法框架。 +2. 提交接口代码至 Gitea,合并并通过审核。 +3. 在 `Services` 文件夹下新建类,实现上述接口。 + +### 3.2 注册 Service + +1. 在 `Configs/ServiceCollectionExtensions.cs` 文件的 `AddAllService` 方法中添加: + +```csharp +services.AddTransient<接口类型, 实现类>(); +``` + +2. 根据业务逻辑选择生命周期: + + * **AddTransient**:瞬时 + * **AddScoped**:请求范围 + * **AddSingleton**:单例 + +### 3.3 在 Controller 中使用 Service + +1. 在 Controller 内定义属性: + +```csharp +private readonly IDemo _demo; +``` + +2. 在构造函数中通过依赖注入接收接口实例: + +```csharp +public WeatherForecastController(IDemo demo) +{ + _demo = demo; +} +``` + +3. 在 Controller 方法中使用 `_demo` 调用业务逻辑方法。