在实践中应用仓储模式时,如何处理复杂查询?请提供一个实际案例,展示在避免将这些复杂查询泄露到领域模型中的同时,仍然能够高效地满足业务需求。

在领域驱动设计中,尤其是在CQRS(命令查询职责分离)模式的支持下,仓储模式可以更高效地处理复杂查询,同时避免将这些复杂性引入到领域模型中。具体而言,可以通过以下步骤来实现这一点:

  1. 定义领域模型:首先,根据业务需求定义领域模型。确保领域模型干净、专注于领域逻辑,不涉及具体的数据访问细节。
  2. 创建只读模型:为了支持复杂的查询需求,可以创建一个只读的领域模型,该模型与领域模型中的实体可能不完全相同,但仅用于查询目的。这些只读模型可以更加灵活,直接映射到数据库中的视图或特定的索引。
  3. 设计仓储接口:为领域模型和只读模型设计仓储接口。领域模型的仓储接口主要用于执行创建、更新、删除操作,而只读模型的仓储接口则专注于处理复杂的查询逻辑。
  4. 实现仓储:实现仓储时,可以根据具体的数据库技术(如SQL Server、MongoDB等)来编写高效的查询。对于复杂的查询,可以直接在仓储实现中使用SQL语句或者其他数据库特定的功能,以确保查询的性能。

实际案例

假设我们正在开发一个电子商务系统,需要处理一个复杂的查询需求——“列出过去30天内销售额最高的前10位产品,同时显示这些产品的销售趋势和平均评价”。

1. 领域模型

首先定义产品、订单和评价的领域模型:

public class Product : Entity
{
    public Guid Id { get; private set; }
    public string Name { get; private set; }
    public decimal Price { get; private set; }
    // 其他属性和方法
}

public class Order : Entity
{
    public Guid Id { get; private set; }
    public List<OrderLine> Lines { get; private set; } = new();n    // 其他属性和方法
}

public class OrderLine : Entity
{
    public Guid ProductId { get; private set; }
    public int Quantity { get; private set; }
    public decimal TotalPrice { get; private set; }
}

public class Review : Entity
{
    public Guid ProductId { get; private set; }
    public int Rating { get; private set; }
    public string Comment { get; private set; }
    // 其他属性和方法
}

2. 只读模型

为了处理复杂的查询,定义一个只读模型,该模型专门用于聚集和展示数据:

public class ProductSalesReport
{
    public Guid ProductId { get; set; }
    public string ProductName { get; set; }
    public decimal TotalSales { get; set; }
    public List<SalesTrend> SalesTrends { get; set; } = new();
    public decimal AverageRating { get; set; }
}

public class SalesTrend
{
    public DateTime Date { get; set; }
    public decimal Sales { get; set; }
}

3. 仓储接口

为领域模型和只读模型定义仓储接口:

public interface IProductRepository
{
    Product GetById(Guid id);
    void Add(Product product);
    void Update(Product product);
    void Remove(Product product);
}

public interface IProductSalesReportRepository
{
    List<ProductSalesReport> GetTopSellingProducts(int topCount, DateTime startDate, DateTime endDate);
}

4. 仓储实现

在仓储实现中,编写高效的查询逻辑,直接访问数据库:

public class ProductSalesReportRepository : IProductSalesReportRepository
{
    private readonly DbContext _dbContext;

    public ProductSalesReportRepository(DbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public List<ProductSalesReport> GetTopSellingProducts(int topCount, DateTime startDate, DateTime endDate)
    {
        var query = from p in _dbContext.Products
                     join ol in _dbContext.OrderLines on p.Id equals ol.ProductId
                     join o in _dbContext.Orders on ol.OrderId equals o.Id
                     where o.OrderDate >= startDate && o.OrderDate <= endDate
                     group new { p, ol, o } by p.Id into g
                     select new
                     {
                         ProductId = g.Key,
                         ProductName = g.First().p.Name,
                         TotalSales = g.Sum(x => x.ol.TotalPrice),
                         SalesTrends = g.GroupBy(x => x.o.OrderDate.Date)
                                        .Select(t => new SalesTrend
                                        {
                                            Date = t.Key,
                                            Sales = t.Sum(x => x.ol.TotalPrice)
                                        }).ToList(),
                         AverageRating = _dbContext.Reviews
                                               .Where(r => r.ProductId == g.Key)
                                               .Average(r => r.Rating)
                     };

        var reports = query.OrderByDescending(x => x.TotalSales)
                           .Take(topCount)
                           .Select(x => new ProductSalesReport
                           {
                               ProductId = x.ProductId,
                               ProductName = x.ProductName,
                               TotalSales = x.TotalSales,
                               SalesTrends = x.SalesTrends,
                               AverageRating = x.AverageRating
                           }).ToList();

        return reports;
    }
}

通过这种方式,我们可以高效地处理复杂的查询需求,同时保持领域模型的简洁和专注。