存储库模式最佳实践
我很抱歉,因为这个问题很难用好话表达。
我有类似于这个的问题。
是否违反了存储库模式?使用为产品创建的存储库用于获取所有类别?
viewModel.Categories= productRepository.FindAll<Category>(c => c.Id > 0).ToList();//is it Correct with Pattern
虽然我也可以得到类似的结果,但
viewModel.Categories = categoryRepository.GetAll();// getting Categories by creating new instance of categoryRepository
在上面的示例中,我需要在产品视图上显示类别,仅显示类别,不会在类别上完成任何增删改查(工作单元)。 那么这里的最佳实践是什么?
谢谢,
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 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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如本文中提到的,您可能希望在两者之间有一个 ServiceLayer。服务层负责将元逻辑与控制器解耦。随着您的代码变得越来越复杂,存储库将需要彼此了解,这并不是一件好事。然而,对于服务来说,这是完全可以的。存储库非常轻量级,并且不理解业务逻辑。
然而,ServiceLayer 却有。例如,如果没有
Categories
集合/表,但类别嵌入在产品中,则不应存在冒充的 CategoryRepository。然而,ProductService
可以提供方法AllCategories()
。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. AProductService
could suplly a methodAllCategories()
, however.