0-1搭建标准web API 项目

0-1搭建标准web API 项目 第一步创建 MNAIL 解决方案复制执行打开CMD / PowerShell进入你要放项目的文件夹bash运行mkdir MNAIL cd MNAIL dotnet new sln -n MNAIL第二步创建 5 个标准项目美甲项目专用一条一条复制执行bash运行dotnet new webapi -n MNAIL.Api dotnet new classlib -n MNAIL.Application dotnet new classlib -n MNAIL.Domain dotnet new classlib -n MNAIL.Infrastructure dotnet new classlib -n MNAIL.Core加入解决方案bash运行dotnet sln add MNAIL.Api/MNAIL.Api.csproj dotnet sln add MNAIL.Application/MNAIL.Application.csproj dotnet sln add MNAIL.Domain/MNAIL.Domain.csproj dotnet sln add MNAIL.Infrastructure/MNAIL.Infrastructure.csproj dotnet sln add MNAIL.Core/MNAIL.Core.csproj第三步建立依赖关系必须按这个来bash运行dotnet add MNAIL.Api reference MNAIL.Application dotnet add MNAIL.Application reference MNAIL.Domain dotnet add MNAIL.Application reference MNAIL.Core dotnet add MNAIL.Infrastructure reference MNAIL.Domain dotnet add MNAIL.Infrastructure reference MNAIL.Application dotnet add MNAIL.Infrastructure reference MNAIL.Core依赖规则Api → ApplicationApplication → Domain CoreInfrastructure → Domain Application CoreDomain、Core 不依赖任何项目第四步创建标准文件夹直接照着建MNAIL.CoreplaintextExceptions Extensions Helpers ModelsMNAIL.DomainplaintextEntities Enums InterfacesMNAIL.ApplicationplaintextDTOs Interfaces Services MappingsMNAIL.InfrastructureplaintextData Repositories ExternalServicesMNAIL.ApiplaintextControllers Middlewares Properties appsettings.json Program.cs第五步安装必需的 NuGet 包1. Infrastructure数据库bash运行dotnet add MNAIL.Infrastructure package Microsoft.EntityFrameworkCore dotnet add MNAIL.Infrastructure package Microsoft.EntityFrameworkCore.SqlServer dotnet add MNAIL.Infrastructure package Microsoft.EntityFrameworkCore.Tools2. Api日志bash运行dotnet add MNAIL.Api package Serilog.AspNetCore3. Application对象映射bash运行dotnet add MNAIL.Application package AutoMapper dotnet add MNAIL.Application package AutoMapper.Extensions.Microsoft.DependencyInjection第六步开始写代码全部直接复制1. MNAIL.Core → 统一返回格式Models/ApiResponse.cscsharp运行namespace MNAIL.Core.Models; public class ApiResponseT { public bool Success { get; set; } public string Message { get; set; } public T Data { get; set; } public static ApiResponseT SuccessResult(T data, string message 请求成功) { return new ApiResponseT { Success true, Data data, Message message }; } public static ApiResponseT FailResult(string message) { return new ApiResponseT { Success false, Message message }; } }2. MNAIL.Domain → 美甲项目实体顾客美甲核心实体Entities/Customer.cscsharp运行namespace MNAIL.Domain.Entities; public class Customer { public int Id { get; set; } public string Name { get; set; } public string Phone { get; set; } public DateTime? LastServiceDate { get; set; } }美甲服务项目Entities/NailService.cscsharp运行namespace MNAIL.Domain.Entities; public class NailService { public int Id { get; set; } public string Name { get; set; } public decimal Price { get; set; } public int DurationMinutes { get; set; } }仓储接口Interfaces/ICustomerRepository.cscsharp运行using MNAIL.Domain.Entities; namespace MNAIL.Domain.Interfaces; public interface ICustomerRepository { TaskCustomer GetByIdAsync(int id); Task AddAsync(Customer customer); }3. MNAIL.Application → 服务与 DTODTODTOs/CustomerDto.cscsharp运行namespace MNAIL.Application.DTOs; public class CustomerDto { public int Id { get; set; } public string Name { get; set; } public string Phone { get; set; } public DateTime? LastServiceDate { get; set; } }服务接口Interfaces/ICustomerService.cscsharp运行using MNAIL.Application.DTOs; namespace MNAIL.Application.Interfaces; public interface ICustomerService { TaskCustomerDto GetCustomerByIdAsync(int id); }服务实现Services/CustomerService.cscsharp运行using MNAIL.Application.DTOs; using MNAIL.Application.Interfaces; using MNAIL.Domain.Entities; using MNAIL.Domain.Interfaces; namespace MNAIL.Application.Services; public class CustomerService : ICustomerService { private readonly ICustomerRepository _customerRepository; public CustomerService(ICustomerRepository customerRepository) { _customerRepository customerRepository; } public async TaskCustomerDto GetCustomerByIdAsync(int id) { var customer await _customerRepository.GetByIdAsync(id); return new CustomerDto { Id customer.Id, Name customer.Name, Phone customer.Phone, LastServiceDate customer.LastServiceDate }; } }4. MNAIL.Infrastructure → 数据库 仓储数据库上下文Data/AppDbContext.cscsharp运行using Microsoft.EntityFrameworkCore; using MNAIL.Domain.Entities; namespace MNAIL.Infrastructure.Data; public class AppDbContext : DbContext { public AppDbContext(DbContextOptionsAppDbContext options) : base(options) { } public DbSetCustomer Customers SetCustomer(); public DbSetNailService NailServices SetNailService(); }仓储实现Repositories/CustomerRepository.cscsharp运行using Microsoft.EntityFrameworkCore; using MNAIL.Domain.Entities; using MNAIL.Domain.Interfaces; using MNAIL.Infrastructure.Data; namespace MNAIL.Infrastructure.Repositories; public class CustomerRepository : ICustomerRepository { private readonly AppDbContext _db; public CustomerRepository(AppDbContext db) { _db db; } public async TaskCustomer GetByIdAsync(int id) { return await _db.Customers.FindAsync(id); } public async Task AddAsync(Customer customer) { await _db.Customers.AddAsync(customer); await _db.SaveChangesAsync(); } }5. MNAIL.Api → 控制器Controllers/CustomersController.cscsharp运行using Microsoft.AspNetCore.Mvc; using MNAIL.Application.DTOs; using MNAIL.Application.Interfaces; using MNAIL.Core.Models; namespace MNAIL.Api.Controllers; [Route(api/[controller])] [ApiController] public class CustomersController : ControllerBase { private readonly ICustomerService _customerService; public CustomersController(ICustomerService customerService) { _customerService customerService; } [HttpGet({id})] public async TaskActionResultApiResponseCustomerDto Get(int id) { var data await _customerService.GetCustomerByIdAsync(id); return ApiResponseCustomerDto.SuccessResult(data); } }第七步Program.cs 最终配置复制全覆盖csharp运行using Microsoft.EntityFrameworkCore; using MNAIL.Application.Interfaces; using MNAIL.Application.Services; using MNAIL.Domain.Interfaces; using MNAIL.Infrastructure.Data; using MNAIL.Infrastructure.Repositories; var builder WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); // 数据库 builder.Services.AddDbContextAppDbContext(options options.UseSqlServer(builder.Configuration.GetConnectionString(Default))); // 注入服务 builder.Services.AddScopedICustomerService, CustomerService(); builder.Services.AddScopedICustomerRepository, CustomerRepository(); var app builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.MapControllers(); app.Run();第八步appsettings.json 数据库连接jsonConnectionStrings: { Default: Server.;DatabaseMNAILDb;Trusted_ConnectionTrue;TrustServerCertificateTrue }第九步运行bash运行cd MNAIL.Api dotnet run打开 https://localhost:5001/swagger✅你的 MNAIL 美甲项目 企业级架构搭建完成你现在拥有标准 5 层架构可维护、可扩展、高可靠领域驱动设计基础DTO 隔离仓储模式依赖注入统一返回格式可直接用于正式开发