开发者

Repository Pattern with mixed Logic

My ProductRepository has a method, lets say:

GetAllCalculatedProducs(int categoryId) {}

This method uses class Calculator to compute many values of the product. It looks just like this:

public IEnumerable<Product> GetAllCalculatedProducts(int categoryId)
{
  var items = GetAllProductsByCatego开发者_如何学运维ryId(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;
}

Calculator does his work using another repositories.

[DB] <--> [Repository] <- |Business Logic| -> [Calculator]

I assume that is wrong due to the fact that Repository uses class which belongs to Logic. I even think that this method should be in other place maybe in ProductService? but I'm not sure.

And can Filter and Order be use in repository?


I only add methods to the repository that directly or indirectly work against the database. Your method do not do that, it calls GetAllProductsByCategoryId which works with the DB. Hence it should be part of the service instead.

If it had generated an own query and done some filtering and sorting on it, I would have not had any problems with that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜