亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

擴展(替代)存儲庫設計模式?

擴展(替代)存儲庫設計模式?

C#
精慕HU 2022-12-24 14:15:29
我正在 ASP.NET MVC 中開發一個項目。我想在應用程序的數據訪問層和業務邏輯層之間創建一個抽象層。我一直在使用存儲庫和工作單元?;仡櫼幌?,在此模式中,創建了一個通用存儲庫和許多特定存儲庫。我在這個項目中遇到的問題是我需要另一個存儲庫中某個特定存儲庫的方法。例如,我有一個產品和子產品存儲庫。我想在 Product 方法中使用 Subproduct 方法,而不是每次都為 Subproduct 重寫 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    }}
查看完整描述

2 回答

?
慕森王

TA貢獻1777條經驗 獲得超3個贊

你已經在你的問題中說過你有業務邏輯層。那是管理這些東西的最佳場所。

因此,您不會在另一個存儲庫中調用一個存儲庫。相反,您可以通過 BLL 的一種方法調用兩個存儲庫來實現目標。希望您的 UoW 暴露于 BLL。這樣,在相同的 UoW 范圍內,您可以執行這兩個操作。

這不僅限于Get記錄。這可以進一步擴展到GetModify-Update或其他任何內容。

我不確定你是做什么的CheckSomeCondition。它只是一個 Predicate 就可以了。如果它是某些業務邏輯的一部分,更好的方法是如上所述將其轉移到 BLL。



查看完整回答
反對 回復 2022-12-24
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

最直接的方法是在其構造函數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

    }

}

如果你已經有一個ProductSubcategoryRepository你可以注入它的實例:


public ProductCategoryRepository(DbContext context, ProductSubcategoryRepository subRepo) : base(context)

{

    this.subRepo = subRepo;

}


查看完整回答
反對 回復 2022-12-24
  • 2 回答
  • 0 關注
  • 107 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號