在软件开发中,效率、可维护性和可扩展性至关重要。广泛采用的设计模式通过抽象化常见的数据访问操作、减少代码冗余和简化维护来解决这些问题。随着 .NET 8 的出现,利用此模式变得更加无缝和强大。在本文中,我们将深入探讨 C# 的概念、其优点及其在 .NET 8 中的实现。
A 是一种设计模式,用于创建可以处理任何类型实体操作的存储库。它抽象化了常见的 CRUD(创建、读取、更新、删除)操作,提供了一个通用接口来与数据源进行交互。此模式通过减少样板代码和集中数据访问逻辑来促进 DRY(不要重复自己)原则。
dotnet new webapi -n GenericRepositoryDemo
public interface IEntity
{
int Id { get; set; }
}
public class Product : IEntity
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public interface IRepository<TEntity> where TEntity : class, IEntity
{
Task<IEnumerable<TEntity>> GetAllAsync();
Task<TEntity> GetByIdAsync(int id);
Task AddAsync(TEntity entity);
Task SaveAsync();
void Update(TEntity entity);
void Delete(TEntity entity);
}
public class Repository<TEntity> : IRepository<TEntity>
where TEntity : class, IEntity
{
private readonly DbContext _context;
private readonly DbSet<TEntity> _dbSet;
public Repository(DbContext context)
{
_context = context;
_dbSet = context.Set<TEntity>();
}
public async Task<IEnumerable<TEntity>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}
public async Task<TEntity> GetByIdAsync(int id)
{
return await _dbSet.FindAsync(id);
}
public async Task AddAsync(TEntity entity)
{
await _dbSet.AddAsync(entity);
}
public void Update(TEntity entity)
{
_dbSet.Update(entity);
}
public void Delete(TEntity entity)
{
_dbSet.Remove(entity);
}
public async Task SaveAsync()
{
await _context.SaveChangesAsync();
}
}
public class Context : DbContext
{
public Context(DbContextOptions<Context> options) : base(options) {}
public DbSet<Product> Products { get; set; }
}
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<Context>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IRepository<Product> _productRepository;
public ProductsController(IRepository<Product> productRepository)
{
_productRepository = productRepository;
}
[HttpGet]
public async Task<IActionResult> GetAllProducts()
{
var products = await _productRepository.GetAllAsync();
return Ok(products);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetProductById(int id)
{
var product = await _productRepository.GetByIdAsync(id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
[HttpPost]
public async Task<IActionResult> AddProduct(Product product)
{
await _productRepository.AddAsync(product);
await _productRepository.SaveAsync();
return CreatedAtAction(nameof(GetProductById), new { id = product.Id }, product);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateProduct(int id, Product product)
{
if (id != product.Id)
{
return BadRequest();
}
_productRepository.Update(product);
await _productRepository.SaveAsync();
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProduct(int id)
{
var product = await _productRepository.GetByIdAsync(id);
if (product == null)
{
return NotFound();
}
_productRepository.Delete(product);
await _productRepository.SaveAsync();
return NoContent();
}
}