发布于 2026-01-06 0 阅读
0

理解和实现 .NET DEV 中的存储库模式 Mux 呈现的全球展示挑战赛:展示你的项目!

理解和实现 .NET 中的存储库模式

由 Mux 赞助的 DEV 全球展示挑战赛:展示你的项目!

在现代软件开发中,编写简洁易维护的代码至关重要。仓储模式正是实现这一目标的设计模式之一,它将数据访问层与应用程序的其他部分抽象出来,使代码更模块化、更易于测试和维护。

什么是仓储模式?

存储库模式是一种设计模式,它通过封装访问数据源所需的逻辑,在领域层和数据映射层(例如实体框架 (EF))之间进行协调。它提供了一个集中执行数据操作的位置,从而简化了数据访问代码的管理和测试。

主要优势:

  • 关注点分离:将业务逻辑与数据访问逻辑分离。
  • 可测试性:使数据访问层更容易进行模拟和单元测试。
  • 集中式数据访问:为整个应用程序提供一致的方式来访问数据。

在 .NET 中实现存储库模式

为了演示存储库模式的实际应用,我们将创建一个简单的 .NET 控制台应用程序,该应用程序对Product实体执行基本的 CRUD(创建、读取、更新、删除)操作。

步骤 1:设置控制台应用程序

  1. 创建一个新的.NET控制台应用程序:
   dotnet new console -n RepositoryPatternDemo
   cd RepositoryPatternDemo
Enter fullscreen mode Exit fullscreen mode
  1. 添加必要的软件包:
   dotnet add package Microsoft.EntityFrameworkCore
   dotnet add package Microsoft.EntityFrameworkCore.SqlServer
Enter fullscreen mode Exit fullscreen mode

步骤 2:创建实体和数据库上下文

首先,定义Product实体和AppDbContext

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace RepositoryPatternDemo
{
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

    public class AppDbContext : DbContext
    {
        public DbSet<Product> Products { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=ProductDb;Trusted_Connection=True;");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

步骤 3:创建存储库接口和实现

定义IRepository接口并在类中实现它Repository

namespace RepositoryPatternDemo
{
    public interface IRepository<T> where T : class
    {
        Task<T> GetByIdAsync(int id);
        Task<IEnumerable<T>> GetAllAsync();
        Task AddAsync(T entity);
        void Update(T entity);
        void Delete(T entity);
    }

    public class Repository<T> : IRepository<T> where T : class
    {
        protected readonly AppDbContext _context;

        public Repository(AppDbContext context)
        {
            _context = context;
        }

        public async Task<T> GetByIdAsync(int id)
        {
            return await _context.Set<T>().FindAsync(id);
        }

        public async Task<IEnumerable<T>> GetAllAsync()
        {
            return await _context.Set<T>().ToListAsync();
        }

        public async Task AddAsync(T entity)
        {
            await _context.Set<T>().AddAsync(entity);
            await _context.SaveChangesAsync();
        }

        public void Update(T entity)
        {
            _context.Set<T>().Update(entity);
            _context.SaveChanges();
        }

        public void Delete(T entity)
        {
            _context.Set<T>().Remove(entity);
            _context.SaveChanges();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

步骤 4:创建服务层

创建一个ProductService使用存储库与Product实体交互的类:

namespace RepositoryPatternDemo
{
    public class ProductService
    {
        private readonly IRepository<Product> _productRepository;

        public ProductService(IRepository<Product> productRepository)
        {
            _productRepository = productRepository;
        }

        public async Task<Product> GetProductById(int id)
        {
            return await _productRepository.GetByIdAsync(id);
        }

        public async Task<IEnumerable<Product>> GetAllProducts()
        {
            return await _productRepository.GetAllAsync();
        }

        public async Task AddProduct(Product product)
        {
            await _productRepository.AddAsync(product);
        }

        public void UpdateProduct(Product product)
        {
            _productRepository.Update(product);
        }

        public void DeleteProduct(Product product)
        {
            _productRepository.Delete(product);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

步骤 5:实现主方法

最后,实现主程序逻辑,演示如何使用该程序ProductService添加、检索、更新和删除Product实体:

using System;
using System.Threading.Tasks;

namespace RepositoryPatternDemo
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using var context = new AppDbContext();
            var productRepository = new Repository<Product>(context);
            var productService = new ProductService(productRepository);

            // Add a new product
            var newProduct = new Product { Name = "Laptop", Price = 1200.00m };
            await productService.AddProduct(newProduct);
            Console.WriteLine("Product added.");

            // Get all products
            var products = await productService.GetAllProducts();
            Console.WriteLine("Products in the database:");
            foreach (var product in products)
            {
                Console.WriteLine($"- {product.Name} (${product.Price})");
            }

            // Update a product
            newProduct.Price = 1000.00m;
            productService.UpdateProduct(newProduct);
            Console.WriteLine("Product updated.");

            // Get product by ID
            var fetchedProduct = await productService.GetProductById(newProduct.Id);
            Console.WriteLine($"Fetched product: {fetchedProduct.Name} (${fetchedProduct.Price})");

            // Delete a product
            productService.DeleteProduct(fetchedProduct);
            Console.WriteLine("Product deleted.");

            // Check the remaining products
            products = await productService.GetAllProducts();
            Console.WriteLine("Remaining products in the database:");
            foreach (var product in products)
            {
                Console.WriteLine($"- {product.Name} (${product.Price})");
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

结论

存储库模式是 .NET 开发人员的强大工具,它提供了一种清晰且易于维护的数据访问处理方式。通过抽象数据操作,它促进了代码架构的简洁性,使应用程序更易于管理、扩展和测试。本示例演示了如何在简单的 .NET 控制台应用程序中实现存储库模式,展示了解耦且可测试的数据访问层的优势。

文章来源:https://dev.to/moh_moh701/understanding-and-implementing-the-repository-pattern-in-net-3ikj