- 定义实体
首先定义一个实体,例如 Product:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
CopyInsert
2. 创建 DbContext
创建一个 DbContext 类:
public class AppDbContext : DbContext
{
public DbSet
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
CopyInsert
3. 定义仓库接口和实现
定义一个通用仓库接口:
public interface IRepository
{
Task<IEnumerable
Task
Task AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(int id);
}
CopyInsert
实现该接口:
public class Repository
{
private readonly AppDbContext _context;
private readonly DbSet
public Repository(AppDbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public async Task<IEnumerable<T>> GetAllAsync() => await _dbSet.ToListAsync();
public async Task<T> GetByIdAsync(int id) => await _dbSet.FindAsync(id);
public async Task AddAsync(T entity) => await _dbSet.AddAsync(entity);
public async Task UpdateAsync(T entity) => _dbSet.Update(entity);
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
_dbSet.Remove(entity);
}
}
CopyInsert
4. 定义工作单元接口和实现
定义工作单元接口:
public interface IUnitOfWork
{
Task
}
CopyInsert
实现工作单元:
public class UnitOfWork : IUnitOfWork
{
private readonly AppDbContext _context;
public UnitOfWork(AppDbContext context)
{
_context = context;
}
public async Task<int> SaveChangesAsync() => await _context.SaveChangesAsync();
}
CopyInsert
5. 创建服务层
创建服务层来处理业务逻辑:
public interface IProductService
{
Task<IEnumerable
Task
Task AddProductAsync(Product product);
Task UpdateProductAsync(Product product);
Task DeleteProductAsync(int id);
}
public class ProductService : IProductService
{
private readonly IRepository
private readonly IUnitOfWork _unitOfWork;
public ProductService(IRepository<Product> productRepository, IUnitOfWork unitOfWork)
{
_productRepository = productRepository;
_unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Product>> GetAllProductsAsync() => await _productRepository.GetAllAsync();
public async Task<Product> GetProductByIdAsync(int id) => await _productRepository.GetByIdAsync(id);
public async Task AddProductAsync(Product product)
{
await _productRepository.AddAsync(product);
await _unitOfWork.SaveChangesAsync();
}
public async Task UpdateProductAsync(Product product)
{
await _productRepository.UpdateAsync(product);
await _unitOfWork.SaveChangesAsync();
}
public async Task DeleteProductAsync(int id)
{
await _productRepository.DeleteAsync(id);
await _unitOfWork.SaveChangesAsync();
}
}
CopyInsert
6. 创建控制器
最后,创建一个控制器来处理 HTTP 请求:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IProductService _productService;
public ProductsController(IProductService productService)
{
_productService = productService;
}
[HttpGet]
public async Task<IActionResult> GetAll() => Ok(await _productService.GetAllProductsAsync());
[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id) => Ok(await _productService.GetProductByIdAsync(id));
[HttpPost]
public async Task<IActionResult> Create(Product product)
{
await _productService.AddProductAsync(product);
return CreatedAtAction(nameof(GetById), new { id = product.Id }, product);
}
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, Product product)
{
if (id != product.Id)
{
return BadRequest();
}
await _productService.UpdateProductAsync(product);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _productService.DeleteProductAsync(id);
return NoContent();
}
}
CopyInsert
7. 在 Startup.cs 中配置服务
最后,在 Startup.cs(或 Program.cs)中配置服务:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<IProductService, ProductService>();
services.AddControllers();
}
CopyInsert
通过以上步骤,你将可以在 ASP.NET Core 应用程序中实现一个基本的仓库和工作单元模式,以及服务层和控制器的分离。这样有助于保持代码的清晰和可维护性。