2011-05-03 133 views
1

我ProductRepository Repository模式有一個方法,讓說:混合邏輯

GetAllCalculatedProducs(int categoryId) {} 

此方法使用類計算器來計算產品的許多值。它看起來就像這樣:

public IEnumerable<Product> GetAllCalculatedProducts(int categoryId) 
{ 
    var items = GetAllProductsByCategoryId(categoryId); 

    Calculator c = new Calculator(items); 
    c.Calculate(); 

    Filter f = new Filter(c.Items); 
    f.Filter(); 

    Sorter s = new Sorter(f.Items); 
    s.Sort(); 

    return s.Items; 
} 

計算器使用另一個存儲庫的工作。

[DB] < - > [存儲庫] < - 商業邏輯| - > [Calculator]

我認爲這是錯誤的,因爲Repository使用屬於Logic的類。我甚至認爲這種方法應該可以在其他地方使用ProductService?但我不確定。

並且Filter和Order可以在倉庫中使用嗎?

回答

0

我只將方法添加到直接或間接針對數據庫的存儲庫中。你的方法不這樣做,它調用GetAllProductsByCategoryId它與數據庫一起工作。因此它應該是服務的一部分。

如果它已經生成了自己的查詢並對它進行了一些過濾和排序,那麼我就沒有任何問題了。

+0

感謝您的回覆,所以解決方案可能是這樣的:http://pastie.org/1859961,對吧? – reizal 2011-05-03 10:54:40

+0

是的。儘管我會爲'Sorter','Filter'和'Calculator'使用更具體的名稱。也許'SortOnLastName','FilterOutOldEntries','CalculateAveragePrice'。 – jgauffin 2011-05-03 11:04:07

+0

很好,謝謝你的建議 – reizal 2011-05-03 13:20:12