服务和信息库层的职责 [英] Responsibilities of Service and Repository layers

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

问题描述

只是想节省的目的是我的持久性存储时,让我的头一轮服务层和存储库层的责任。

Just trying to get my head round the responsibilities of the service layer and repository layer when saving an object to my persistence store.

我站在下目前是这样的:

My current under standing is this:

在我的控制器我创建的(来自表单)用户提交的数据的注的对象。然后,用户调用保存上的NoteService(这是有通过依赖注入)。

In my controller I have created a "Note" object from the data submitted by the user (from the form). The user then calls "Save" on the "NoteService" (which is there via dependency injection).

在对NoteService中的保存方法我实现我的业务逻辑验证,然后通过备注对象的NoteRepository中的保存方法。

Within the "Save" method on the "NoteService" I carry out my business logic validation and then pass the "Note" object to the "Save" method of the "NoteRepository".

在NoteRepository的保存方法,然后进行检查,看是否有此对象上现有的主键,这样的话得到了从数据库中,它是用注对象通过传递的属性更新对象和它然后保存回数据库。
如果不存在主键则该对象被简单地保存到数据库和然后返回到与它的服务的新创建的主键。

The "Save" method of the "NoteRepository" then checks to see if there is an existing primary key on this object and if so then get's that object from the db and updates it's properties using the "Note" object passed through and it's then saved back to the db. If there is no primary key then the object is simply saved to the db and the then returned to the service with it's newly created primary key.

推荐答案

您的关注点分离的声音pretty对我好。我们遵循相同的模式,但往往会添加更多的层对的存储库层。我们把它叫做领域层,并在那里完成我们所有的业务逻辑。我们的服务层只是一个传递到我们的情况下,域名,我们需要在未来发布任何我们的服务到ESB。

Your separation of concerns sounds pretty good to me. We follow the same pattern, but tend to add one more layer right about the repository layer. We call it the domain layer and perform all our business logic in there. Our service layer is just a pass through to our domain in case we need to publish any of our services to an ESB in the future.

你所做的是不是塞满所有的业务和DB逻辑控制器里面很多人往往做的最大好处。你总是希望你的控制器是越轻越好,如果你真的想跟着MVC。

The biggest benefit of what you are doing is not cluttering all the business and DB logic in the controller which a lot of people tend to do. You always want your controllers to be as light as possible if you really want to follow MVC.

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

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