Merge pull request 'feature-nxdev' (#10) from feature-nxdev into main
Reviewed-on: #10
This commit is contained in:
commit
24316b4599
10
backend/IM_API/Configs/ServiceCollectionExtensions.cs
Normal file
10
backend/IM_API/Configs/ServiceCollectionExtensions.cs
Normal file
@ -0,0 +1,10 @@
|
||||
namespace IM_API.Configs
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddAllService(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
8
backend/IM_API/Dtos/TestDto.cs
Normal file
8
backend/IM_API/Dtos/TestDto.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace IM_API.Dtos
|
||||
{
|
||||
public class TestDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
||||
9
backend/IM_API/Interface/Services/IDemo.cs
Normal file
9
backend/IM_API/Interface/Services/IDemo.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using IM_API.Dtos;
|
||||
|
||||
namespace IM_API.Interface.Services
|
||||
{
|
||||
public interface IDemo
|
||||
{
|
||||
TestDto Test();
|
||||
}
|
||||
}
|
||||
@ -50,10 +50,6 @@ public partial class IMDbContext : DbContext
|
||||
|
||||
public virtual DbSet<User> 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
|
||||
|
||||
@ -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<IMDbContext>(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();
|
||||
|
||||
13
backend/IM_API/Services/Demo.cs
Normal file
13
backend/IM_API/Services/Demo.cs
Normal file
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -5,5 +5,8 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Server=192.168.5.100;Port=3306;Database=IM;User=root;Password=768788Dyw;"
|
||||
}
|
||||
}
|
||||
|
||||
69
docs/后端代码规范文档.md
Normal file
69
docs/后端代码规范文档.md
Normal file
@ -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` 调用业务逻辑方法。
|
||||
Loading…
Reference in New Issue
Block a user