2011-05-03 114 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