开发者

Repository Pattern best Practices

My apology as this question is difficult to express with nice words.

I have question similar to this.

Is it violation of Repository Pattern? to use Reposiotry created for Product is used to get all Categories?

        viewModel.Categories= productRepository.FindAll<Category>(c => c.Id > 0).ToList();//is it Correct with Pattern

Though I can get similar result with this also,

        viewModel.Categories = categoryRepository.GetAll();// getting Categories by creating new instance of categoryRepository

In above 开发者_JS百科example I need to show categories on Product view, only diplay no crud(unit of work) will be done on Category. So what is best practice here?

Thnaks,


As mentioned in this post, you probably want to have a ServiceLayer in between. The service layer is responsible to decouple meta logic from your controller. As your code grows more complex, there will be a need for repositories knowing each other, which is not good. For a service, however, it's perfectly OK. Repositories then are very light-weight and have no understanding of the business logic.

The ServiceLayer, however, has. For example, if there is no Categories Collection/Table, but the Categories are embedded in the Products, there shouldn't be a CategoryRepository that pretends. A ProductService could suplly a method AllCategories(), however.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜