不同MVC层之间的数据流(Data flow between different MVC layers)

编程入门 行业动态 更新时间:2024-10-27 08:33:30
不同MVC层之间的数据流(Data flow between different MVC layers)

下面我介绍从使用表单到持久层的数据流。 但是对于哪些对象应该在MVC的哪一层以及如何在不同的MVC层之间传输数据有疑问。 我正在使用Spring,因此下面发布的代码是Spring框架的代码。

在这里,我们有一个DTO(数据传输对象) PatientForm ,它保存用户输入的表单数据。

public class Patient { private int id; private String name; private String medicineOne; private String medicineTwo; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMedicineOne() { return medicineOne; } public void setMedicineOne(String medicineOne) { this.medicineOne = medicineOne; } public String getMedicineTwo() { return medicineTwo; } public void setMedicineTwo(String medicineTwo) { this.medicineTwo = medicineTwo; } }

PatientForm传递给控制器PatientController ,不传输数据,但将表单传递给服务层PatientService 。

@PostMapping("/patient/addpatient") public ModelAndView addPatient(@ModelAttribute("patientform") PatientForm patient){ patientService.addPatient(patient); return new ModelAndView("redirect:/"); }

在服务层PatientService实际将数据从DTO转移到Pesistent Entity Patient 。

public void addPatient(com.hp.view.form.PatientForm patientForm){ String medicineOneName = patientForm.getMedicineOne(); Medicine medicineOne = medicineService.findByName(medicineOneName); if(medicineOne == null){ medicineService.save(new Medicine(medicineOneName)); medicineOne = medicineService.findByName(medicineOneName); } String medicineTwoName = patientForm.getMedicineTwo(); Medicine medicineTwo = medicineService.findByName(medicineTwoName); if(medicineTwo == null){ medicineService.save(new Medicine(medicineTwoName)); medicineTwo = medicineService.findByName(medicineTwoName); } List<Medicine> medicines = new ArrayList<>(); medicines.add(medicineOne); medicines.add(medicineTwo); Patient patient = new Patient(); patient.setName(patientForm.getName()); patient.setMedicine(medicines); patientRepository.save(patient); }

根据以上流程,我的问题如下:

Controller layer或Service layer是否应该将数据从DTO传输到持久实体?

如果在控制器中完成数据传输,则模型实体将在控制器层中声明。 如果数据传输在服务层完成,则意味着DTO将在服务层中声明。 哪两个更喜欢?

在我的服务层中,我实例化了我的实体对象Patient实例。 这会产生问题,我应该让Spring contianer管理我的实体bean吗?

Patient patient = new Patient();

Below I present flow of data from a use form to persistence layer. But have doubts about which objects should be available in which layer of MVC and how data should be transfered between different layers of MVC. I am working with Spring so the code posted below is that of Spring framework.

Here we go, I have a DTO(Data transfer object) PatientForm, which holds form data entered by user.

public class Patient { private int id; private String name; private String medicineOne; private String medicineTwo; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMedicineOne() { return medicineOne; } public void setMedicineOne(String medicineOne) { this.medicineOne = medicineOne; } public String getMedicineTwo() { return medicineTwo; } public void setMedicineTwo(String medicineTwo) { this.medicineTwo = medicineTwo; } }

PatientForm is passed on to a controller PatientController, does not transfer data but passes the form to the service layer PatientService.

@PostMapping("/patient/addpatient") public ModelAndView addPatient(@ModelAttribute("patientform") PatientForm patient){ patientService.addPatient(patient); return new ModelAndView("redirect:/"); }

In service layer PatientService actual transfer of data from DTO to Pesistent Entity Patient takes place.

public void addPatient(com.hp.view.form.PatientForm patientForm){ String medicineOneName = patientForm.getMedicineOne(); Medicine medicineOne = medicineService.findByName(medicineOneName); if(medicineOne == null){ medicineService.save(new Medicine(medicineOneName)); medicineOne = medicineService.findByName(medicineOneName); } String medicineTwoName = patientForm.getMedicineTwo(); Medicine medicineTwo = medicineService.findByName(medicineTwoName); if(medicineTwo == null){ medicineService.save(new Medicine(medicineTwoName)); medicineTwo = medicineService.findByName(medicineTwoName); } List<Medicine> medicines = new ArrayList<>(); medicines.add(medicineOne); medicines.add(medicineTwo); Patient patient = new Patient(); patient.setName(patientForm.getName()); patient.setMedicine(medicines); patientRepository.save(patient); }

Here are my questions as per the flow above:

Should Controller layer or Service layer transfer data from DTO to Persistent Entity?

If data transfer is done in controller means model entity will be declared in controller layer. And if data transfer is done in service layer means DTO will be declared in service layer. Which of the two is prefered?

In my service layer I have instantiated instance of my entity object Patient. Will this create problem and I should let Spring contianer manage my entity beans?

Patient patient = new Patient();

最满意答案

实际上,我会采用完全不同的方法。 DTO可能会受到特定Web应用程序框架的约束。 这会降低服务的可重用性。 相反,您可以创建类似“DTO到实体转换器”的内容。 一个简单的界面,看起来像这样:

public interface DtoToEntityConverter<T, R> { R getEntity(T t); }

然后你可以定义具体的类(或者甚至在更简单的情况下使用lambdas):

@Component public class PatientFormConverter implements DtoToEntityConverter<PatientForm, Patient> { public Patient getEntity(PatientForm form) { // Conversion rules and stuff... } }

然后,只需将该组件注入控制器并在添加患者时调用getEntity :

addPatient(patientFormConverter.getEntity(patientForm));

Actually, I would go with totally different approach. DTO's could be potentially bound for specific web application framework. That would decrease reusability of services. Instead, you can create something like "DTO to entity converter". A simple interface that could look like this:

public interface DtoToEntityConverter<T, R> { R getEntity(T t); }

And then you could define concrete class like (or even use lambdas in simpler cases):

@Component public class PatientFormConverter implements DtoToEntityConverter<PatientForm, Patient> { public Patient getEntity(PatientForm form) { // Conversion rules and stuff... } }

Then, just inject that component to controller and invoke getEntity upon adding of patient:

addPatient(patientFormConverter.getEntity(patientForm));

更多推荐

本文发布于:2023-08-04 12:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416135.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据流   MVC   Data   layers   flow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!