服务层和存储库

发布于 2024-07-10 00:58:41 字数 681 浏览 11 评论 0原文

我使用 MVC 框架已经有一段时间了,我真的很喜欢如何分离关注点。 我养成了让控制器做大量工作的坏习惯。 所以我真的在寻求一些建议。

当我第一次开始使用 MVC 时,我经常在数据库工作完成后让控制器对模型进行操作。 我知道这很糟糕,所以将这项工作转移到模型中。 然而我对此并不满意,因为我希望我的模型非常有学问。

我读了一些书,发现人们通过服务层来保持控制器和模型的精简,我喜欢它的外观。

我只是想了解服务层和存储库应该如何协同工作。 这是我的假设,您能否告诉我这是否是一种好的工作方式?

  1. 如果不需要对数据进行任何操作,控制器可以直接调用存储库,因此服务层不需要参与。
  2. 一旦需要对数据(业务逻辑)进行任何工作,那么这应该在服务中完成层和控制器将在需要时对服务层进行简单的调用
  3. 一旦服务完成其业务逻辑,它将根据需要使用存储库(如果需要持久保存数据)。
  4. 理想情况下,模型应保持精简,理想情况下仅充当 DTO。
  5. 数据验证将在模型内完成(使用 MonoRail 验证属性)。 我很欣赏甚至没有人喜欢用大量属性污染他们的模型,但这是一个不同的讨论。 我喜欢 MonoRail 的验证属性在 UI 中自动进行 jQuery 验证的好处。

我试图将我的所有代码转变为单一职责原则,因此试图理清我的编码实践。

谢谢

I've been using MVC frameworks for a short while now and I really like how the concerns are separated out. I've got into a bad habit of letting the controllers do quite a bit of work. So I'm really looking for some advice.

When I first started using MVC I quite often had the controller doing manipulation on the models after database work had been done. I knew this was bad so moved that work into the models. However I'm not happy with that as I want my models to be very learn.

I've done a bit of reading and I see that people are keeping their controllers and models lean by having a service layer, which I like the look of.

I'm just trying to understand how a service layer and repository should all work together. Here are my assumptions, can you please let me know if this is a good way of working?

  1. The controller can call the repository directly if no manipulation needs to be done on the data and as such a service layer does not need to get involved
  2. Once any work needs to be done to data (business logic) then this should be done in the service layer and the controller will make a simple call to the service layer as and when required
  3. Once a service has done it's business logic it will then use the repository as necessary (if data needs to be persisted).
  4. Models ideally should be kept lean, ideally actings as nothing more than DTOs
  5. Validation of data will be done within the models (using MonoRail validation attributes). I appreciate not even one likes polluting their models with lots of attributes, but that is a different discussion. I like the benefit of MonoRail's validation attributes for the automatic jQuery validation in the UI.

I'm trying to turn all my code around to the single responsibility principle, hence trying to sort out my coding practices.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

七堇年 2024-07-17 00:58:42

首先,没有一套适用于所有情况的规则。 如何对应用程序进行建模很大程度上取决于项目的类型和复杂性。 话虽如此,这里有一些想法:

  1. 从控制器调用存储库没有任何问题。 只需确保控制器不包含业务逻辑即可。
  2. 该服务负责(某些)业务逻辑并使用其他服务来执行此操作。 存储库是一种服务,从服务调用它没有任何问题。
  3. 模型应该包含业务逻辑,实际上您应该始终尝试首先将其放入模型中。 如果您需要外部数据来执行该业务逻辑(来自另一个模型或来自存储库),那么您应该创建一个服务。
  4. 模型中的验证没有任何问题。 使用或不使用属性是一个品味问题(如果你喜欢它那就很好)。 如果验证变得太复杂,则将验证移至模型之外(创建一组外部规则)。

最重要的是,做感觉正确的事情(这通常是正确的答案)。

First, there is no set of rules that's going to work in every situation. How you model you're application depends a lot on the type and complexity of the project. Having said that, here are some ideas:

  1. Nothing wrong with calling the repository from a controller. Just make sure the controller does not contain business logic.
  2. The service takes care of (some) business logic and uses other services to do so. The repository is a type of service, there's nothing wrong with calling it from a service.
  3. The model should contain business logic, actually you should always try to put it in the model first. If you need external data to perform that business logic (from another model or from the repository) then you should create a service.
  4. Nothing wrong with validation in the models. Using attributes or not is a question of taste (if you like it then it's good). Move the validation outside of the model if it gets too complex (create a external set of rules).

Most important, do what feels right (that's usually the right answer).

世态炎凉 2024-07-17 00:58:42

此视频深入介绍了如何组织您的 ASP.NET MVC 解决方案并解决关注点分离和更好的可测试性。 希望它也能帮助其他人。 我从中学到了一些好东西。

This video gives great insight into how to organize your asp.net MVC solution and addressing separation of concerns, and better testability. Hopefully it will help someone else also. I learned some good stuff from it.

抠脚大汉 2024-07-17 00:58:42

Ian Cooper 刚刚写了一篇名为 The Fat 的博客文章控制器 就在这个主题上。

Ian Cooper has just written a blog post called The Fat Controller on just this subject.

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