C# 扩展(替代)存储库设计模式?

C# 扩展(替代)存储库设计模式?,c#,asp.net-mvc,design-patterns,repository,repository-pattern,C#,Asp.net Mvc,Design Patterns,Repository,Repository Pattern,我正在ASP.NETMVC中从事一个项目。我想在应用程序的数据访问层和业务逻辑层之间创建一个抽象层。我一直在使用存储库和工作单元。回顾一下,在这种模式中,将创建一个通用存储库和一些特定存储库。我在这个项目中遇到的问题是,我需要在另一个存储库中使用某个特定存储库的方法。例如,我有一个产品和子产品存储库。我希望在产品方法中使用子管道方法,而不是每次都重写子管道的LINQ查询。有没有办法扩展存储库设计模式的功能,或者我必须使用另一种设计模式 public class ProductSubcategor

我正在ASP.NETMVC中从事一个项目。我想在应用程序的数据访问层和业务逻辑层之间创建一个抽象层。我一直在使用存储库和工作单元。回顾一下,在这种模式中,将创建一个通用存储库和一些特定存储库。我在这个项目中遇到的问题是,我需要在另一个存储库中使用某个特定存储库的方法。例如,我有一个产品和子产品存储库。我希望在产品方法中使用子管道方法,而不是每次都重写子管道的LINQ查询。有没有办法扩展存储库设计模式的功能,或者我必须使用另一种设计模式

public class ProductSubcategoryRepository : Repository<ProductSubcategory>, IProductSubcategoryRepository
{
    public ProductSubcategoryRepository(DbContext context) : base(context)
    {
    }

    public IEnumerable<ProductSubcategory> CheckSomeCondition()
    {
        // LINQ to check some condition based on product subcategory
    }
}

public class ProductCategoryRepository : Repository<ProductCategory>, IProductCategoryRepository
{
    public ProductCategoryRepository(DbContext context) : base(context)
    {

    }

    public IEnumerable<ProductCategory> GetProductCategoriesBeforeDate()
    {
        // Repeated LINQ to check some condition based on product subcategory 
        // (I am looking for a way to call the same method of ProductSubCategory calss)

        // LINQ To return List of product category if the previous query is true
    }
}
公共类ProductSubcategory存储库:存储库,IPProductSubcategory存储库
{
public ProductSubCategory存储库(DbContext上下文):基本(上下文)
{
}
公共IEnumerable CheckSomeCondition()
{
//LINQ根据产品子类别检查某些条件
}
}
公共类ProductCategoryRepository:存储库,IPProductCategoryRepository
{
public ProductCategoryRepository(DbContext上下文):基础(上下文)
{
}
public IEnumerable GetProductCategoriesBeforeDate()之前
{
//重复LINQ检查基于产品子类别的某些条件
//(我正在寻找调用ProductSubCategory Cals的相同方法的方法)
//如果上一个查询为真,LINQ将返回产品类别列表
}
}

最简单的方法是使
ProductCategoryRepository
在其构造函数内创建
ProductSubcategoryRepository
的实例:

public class ProductCategoryRepository : Repository<ProductCategory>, IProductCategoryRepository
{
    private ProductSubcategoryRepository subRepo;
    public ProductCategoryRepository(DbContext context) : base(context)
    {
        subRepo = new ProductSubcategoryRepository(context);
    }

    public IEnumerable<ProductCategory> GetProductCategoriesBeforeDate()
    {
        // call subRepo
    }
}

最直接的方法是使
ProductCategoryRepository
在其构造函数中创建
ProductSubcategoryRepository
的实例:

public class ProductCategoryRepository : Repository<ProductCategory>, IProductCategoryRepository
{
    private ProductSubcategoryRepository subRepo;
    public ProductCategoryRepository(DbContext context) : base(context)
    {
        subRepo = new ProductSubcategoryRepository(context);
    }

    public IEnumerable<ProductCategory> GetProductCategoriesBeforeDate()
    {
        // call subRepo
    }
}

您在问题中已经说过,您已经准备好了业务逻辑层。那是管理这些东西的最好地方

因此,您不需要在另一个存储库中调用一个存储库。相反,您可以在一个方法中从BLL调用两个存储库来实现目标。希望您的UoW暴露于BLL。这样,在UoW的相同范围下,您可以执行这两个操作

这不仅限于
获取记录。这可以进一步扩展到
获取
-
修改
-
更新
或其他任何内容


我不确定您的
CheckSomeCondition
做了什么。这只是一个谓词,那就好了。如果它是某些业务逻辑的一部分,更好的方法是将其转换为BLL,正如我前面所述。

您在问题中已经说过,您已经准备好了业务逻辑层。那是管理这些东西的最好地方

因此,您不需要在另一个存储库中调用一个存储库。相反,您可以在一个方法中从BLL调用两个存储库来实现目标。希望您的UoW暴露于BLL。这样,在UoW的相同范围下,您可以执行这两个操作

这不仅限于
获取记录。这可以进一步扩展到
获取
-
修改
-
更新
或其他任何内容


我不确定您的
CheckSomeCondition
做了什么。这只是一个谓词,那就好了。如果它是某些业务逻辑的一部分,更好的方法是将其转换为BLL,正如我前面所述。

谢谢您的考虑。然而,我正在寻找一个通用的方法。在这个例子中,我有一个类,但是如果我有很多类呢。嗯,但毕竟你必须明确指定哪些类需要重用哪些类中的代码,所以我认为这里没有通用的解决方案。但我也希望看到这样的解决方案。谢谢您的考虑。然而,我正在寻找一个通用的方法。在这个例子中,我有一个类,但是如果我有很多类呢。嗯,但毕竟你必须明确指定哪些类需要重用哪些类中的代码,所以我认为这里没有通用的解决方案。但我也希望看到这样的解决方案。