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

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

问题描述

在课堂上,我们现在正在学习如何构建 Spring 应用程序,即使不直接涉及 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 层非常抽象:它只包含 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?

假设我有一个对象孩子"它有:NameSurNameGenderPhotoBirthdate领域.我将如何命名这些服务?或者你会让控制器处理验证,这对我来说似乎不正确.另一方面,将需要调用的每个 setter 都委托给服务层似乎也不正确.

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.

推荐答案

如果您希望控制器能够将更改持久化到 Child 对象,那么传统上您将在服务中拥有一个方法命名类似于 ChildService.update(Child newchild),它将处理调用正确的 DAO 以保留此 Child 的新版本.

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.

相反,控制器和/或服务应该获取一个 Child 对象,对它的工作单元中的对象做它需要的任何工作,然后请求一个服务(然后是 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.

验证可以在多个层中进行 - 控制器可能想要验证来自网络用户的任何输入,而服务可能想要在它持久化之前验证它是否具有有效的 Child 对象.如果您想在其他功能中重用此服务(例如公开 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天全站免登陆