服务层和存储库
我使用 MVC 框架已经有一段时间了,我真的很喜欢如何分离关注点。 我养成了让控制器做大量工作的坏习惯。 所以我真的在寻求一些建议。
当我第一次开始使用 MVC 时,我经常在数据库工作完成后让控制器对模型进行操作。 我知道这很糟糕,所以将这项工作转移到模型中。 然而我对此并不满意,因为我希望我的模型非常有学问。
我读了一些书,发现人们通过服务层来保持控制器和模型的精简,我喜欢它的外观。
我只是想了解服务层和存储库应该如何协同工作。 这是我的假设,您能否告诉我这是否是一种好的工作方式?
- 如果不需要对数据进行任何操作,控制器可以直接调用存储库,因此服务层不需要参与。
- 一旦需要对数据(业务逻辑)进行任何工作,那么这应该在服务中完成层和控制器将在需要时对服务层进行简单的调用
- 一旦服务完成其业务逻辑,它将根据需要使用存储库(如果需要持久保存数据)。
- 理想情况下,模型应保持精简,理想情况下仅充当 DTO。
- 数据验证将在模型内完成(使用 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?
- 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
- 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
- Once a service has done it's business logic it will then use the repository as necessary (if data needs to be persisted).
- Models ideally should be kept lean, ideally actings as nothing more than DTOs
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,没有一套适用于所有情况的规则。 如何对应用程序进行建模很大程度上取决于项目的类型和复杂性。 话虽如此,这里有一些想法:
最重要的是,做感觉正确的事情(这通常是正确的答案)。
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:
Most important, do what feels right (that's usually the right answer).
此视频深入介绍了如何组织您的 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.
Ian Cooper 刚刚写了一篇名为 The Fat 的博客文章控制器 就在这个主题上。
Ian Cooper has just written a blog post called The Fat Controller on just this subject.