.NET 8 中通过C#实现 GenericRepository

作者:微信公众号:【架构师老卢】
8-10 17:13
25

概述:在软件开发中,效率、可维护性和可扩展性至关重要。广泛采用的设计模式通过抽象化常见的数据访问操作、减少代码冗余和简化维护来解决这些问题。随着 .NET 8 的出现,利用此模式变得更加无缝和强大。在本文中,我们将深入探讨 C# 的概念、其优点及其在 .NET 8 中的实现。什么是通用存储库?A 是一种设计模式,用于创建可以处理任何类型实体操作的存储库。它抽象化了常见的 CRUD(创建、读取、更新、删除)操作,提供了一个通用接口来与数据源进行交互。此模式通过减少样板代码和集中数据访问逻辑来促进 DRY(不要重复自己)原则。使用通用存储库的好处代码可重用性:通过封装通用数据访问逻辑,允许在不同类型的实

在软件开发中,效率、可维护性和可扩展性至关重要。广泛采用的设计模式通过抽象化常见的数据访问操作、减少代码冗余和简化维护来解决这些问题。随着 .NET 8 的出现,利用此模式变得更加无缝和强大。在本文中,我们将深入探讨 C# 的概念、其优点及其在 .NET 8 中的实现。

什么是通用存储库?

A 是一种设计模式,用于创建可以处理任何类型实体操作的存储库。它抽象化了常见的 CRUD(创建、读取、更新、删除)操作,提供了一个通用接口来与数据源进行交互。此模式通过减少样板代码和集中数据访问逻辑来促进 DRY(不要重复自己)原则。

使用通用存储库的好处

  1. 代码可重用性:通过封装通用数据访问逻辑,允许在不同类型的实体之间重用代码。
  2. 可维护性:由于数据访问逻辑集中化,因此可以在一个地方进行所需的任何更改,从而简化维护。
  3. 可测试性:存储库提供的抽象使单元测试更容易,因为您可以在不处理实际数据源的情况下模拟存储库方法。
  4. 一致性:确保在整个应用程序中采用一致的数据访问方法。

在 .NET 8 中实现通用存储库

设置项目

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();
    }
}

配置 DbContext

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();
    }
}
阅读排行