服务层和存储库 [英] Service Layers and Repositories

查看:153
本文介绍了服务层和存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了很短的MVC框架,而现在,我真的很喜欢这个顾虑是如何分离出来。我钻进让控制器做相当多的工作的一个坏习惯。所以我真的找了一些建议。

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.

当我使用MVC刚开始我经常不得不控制器上的模型做操纵数据库的工作已经完成了。我知道这是坏的,所以感动的是工作纳入模型。不过我不乐意这么做,因为我希望我的模型非常了解。

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. 控制器可以直接当调用库无操纵需要的数据,并作为这样一个服务层完成之后的任何工作需要做到的数据(业务逻辑),则应该是这样的不需要介入

  2. 在服务层和控制器将会为服务层作为一个简单的通话,并要求

  3. 一旦服务已经做它的业务逻辑它就会根据需要使用存储库时(如果数据需要持久化)。

  4. 模型最好应保持精简,最好actings因为没有什么比DTO的更多

  5. 数据验证将被内完成模型(采用单轨验证属性)。我很欣赏,甚至人们不喜欢污染他们的模型有很多的属性,但是这是一个不同的讨论。我喜欢在UI自动jQuery验证单轨的验证的利益属性。

  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.

感谢

推荐答案

首先,有没有一套规则,是怎么回事在各种情况下工作。你怎么建模你的应用取决于项目的类型和复杂了很多。话虽如此,这里有一些想法:

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. 没有错,呼吁从控制器的存储库。只要确保控制器不包含业务逻辑。

  2. 服务负责(有些)业务逻辑,并使用其他服务来完成。该库是一种服务,有什么不对从服务调用它。

  3. 模型包含业务逻辑,其实你应该总是试图把它在模型中第一位。如果需要外部数据来执行业务逻辑(从另一个模型或从存储库),那么你应该创建一个服务。

  4. 没有错,在模型验证。使用属性与否是品味的问题(如果你喜欢它,然后它的好)。移动验证模型之外,如果它变得太复杂(创建一个外部设定的规则)。

  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).

这篇关于服务层和存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆