服务层和控制器:谁需要什么照顾? [英] Service layer and controller: who takes care of what?

查看:87
本文介绍了服务层和控制器:谁需要什么照顾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在课堂上,我们现在正在学习如何建立一个Spring应用程序,即使弹簧没有直接参与,我们学会了如何使DAO和服务层对象的接口。

In class we're now learning how to build up a Spring application, even though spring isn't directly involved, we learned how to make the interfaces for DAO and service layer objects.

请纠正我,如果我错了:
DAO层是pretty摘要:它只是包含了CRUD操作,并进一步用于读取数据。(如:获取的所有对象,得到特定对象等)

Please correct me if I'm wrong: DAO layer is pretty abstract: it just contains the CRUD operations and is further used to read data.(ie: get all objects, get specific objects, etc)

服务层:包含服务创造的东西,删除的东西,这就是商业逻辑应该是。

Service layer: contains services to create things, and delete things, this is where business logic should be.

现在所有这一切都使得在服务层意识;除了更新的对象。你只是把更新功能,只是保存对象在数据库中?或者,你需要有定义的逻辑呢?这就是我的困惑是,我的理解是Spring对象仅仅POJO的。那么,现在谁对数据进行验证?

Now all of this makes sense in the service layer; except "updating" objects. Do you just put a "update" function that just saves the object in your database? Or do you need to define the logic there as well? This is where my confusion is as, my understanding is objects in Spring are just POJO's. Now then who validates the data?

比方说,我有一个对象子
它具有:名称性别照片生日
领域。
我将如何命名服务?或者,你只是让控制器照顾验证,它似乎并没有我的权利。在另一方面,它似乎并不正确要么委托每一个需要调用服务层的制定者。

Let's say I have an Object "child" it has:Name, SurName, Gender, Photo, Birthdate fields. how would I name the services? Or would you just let the controller take care of validation, which doesn't seem right to me. On the other hand it wouldn't seem right either to delegate every setter that needs to be called to the service layer.

所以基本上只是:帮我如何定义通过服务层保存对象

So just basically: help me with how to define saving your objects via the service layer.

推荐答案

如果您希望控制器能够坚持更改为儿童对象,那么传统上你会有名字类似 ChildService.update(儿童newChild对象)的服务的方法,这将处理调用正确的DAO坚持这个孩子的新版本。

If you wish to have controllers be able to persist changes to a Child object, then traditionally you would have a method in the service named something like ChildService.update(Child newchild), which will handle calling the correct DAOs to persist the new version of this Child.

控制器可以自由地要求服务对于孩子来说,改变周围的田野(可以想象基于某些用户输入) - 一个健全的设计将有控制器做与儿童POJO一些工作,然后要求服务坚持变化。该模型POJO竟然一无所知控制器,服务或DAO只是简单地保存数据按照你的建议 - 当然你不会希望的setName每次调用() setGender()来自动导致数据库更新。

Controllers are free to ask the service for a Child, change the fields around (conceivably based on some user input) - a sane design would have the Controller doing some work with the Child POJO and then asking the Service to persist the change. The model POJO should know nothing about a controller, service, or DAO but just simply hold data as you suggest - certainly you would not want every call to setName() or setGender() to automatically result in a database update.

相反,控制器和/或服务应取得儿童对象,做任何工作,它需要的对象,在它的工作单位,然后问服务(然后DAO)以保存更改。

Instead, the controller and/or service should acquire a Child object, do whatever work it needs to the object in it's unit of work, and then ask a Service (and then the DAO) to persist the changes.

验证可以发生在数层 - 控制器可能要验证从网络用户的输入,服务可能要验证它具有有效的儿童反对它坚持它。这尤其是很有意义的,在这两种情况下,验证层一定程度的您想以其他身份重新使用该服务 - 如暴露REST接口,不同的前端,等等

Validation can take place in several layers - the Controller might want to validate any input from the web user, and the Service may want to validate that it has a valid Child object before it persists it. It especially makes sense to have some level of validation in both layers in case you want to re-use this Service in other capacities - such as exposing a REST interface, a different front-end, etc.

这篇关于服务层和控制器:谁需要什么照顾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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