存储库模式最佳实践

发布于 2024-10-04 08:14:11 字数 616 浏览 2 评论 0原文

我很抱歉,因为这个问题很难用好话表达。

我有类似于这个的问题。

是否违反了存储库模式?使用为产品创建的存储库用于获取所有类别?

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-10-11 08:14:11

正如本文中提到的,您可能希望在两者之间有一个 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. A ProductService could suplly a method AllCategories(), however.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文