Rama Arjun
Rama Arjun

Reputation: 303

Data flow between different MVC layers

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:

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

  2. 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?

  3. 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();

Upvotes: 3

Views: 1234

Answers (3)

Vasu
Vasu

Reputation: 22422

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

FormBeans are client/channel/endpoint specific, so the Controller layer should do the client specific validations (like min length, max length, etc..) and then convert FormBean's data to Entity Bean which will be passed it to the Service layer. In the 3 tier architecture, Service layer should be reusable (explained below) which should NOT be aware of FormBeans, therefore receives entity object and should be responsible for processing the business logic (performing business validations and core logic plus interacting with DAO/Repository classes).

(2) 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?

A single service can be reused/exposed to connect with multiple end points like a Controller or a different web service and each end point might require different formbeans, so Controller (end point) layer is preferred to handle end point specific validations and then create/pass the correct entity object to the service.

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

No problem. As entity objects are NOT singletons, you can create them inside your service like how you did. But if you are allowing Spring to manage them, you need to ensure that they are created one instance per each input request. This is because Spring bean's default scope is singleton, which needs to be changed to request scope.

Upvotes: 2

Branislav Lazic
Branislav Lazic

Reputation: 14806

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

Upvotes: 1

Abbas Kararawala
Abbas Kararawala

Reputation: 1254

In spring you let application context to manage your beans (i e. You don't initialize your class) and then you can autowire (include them in other classes) without explicitly initializing them.

1) Service layers are used as an intermediary between your controller and model. That is, you autowire your service into your rest controller.

2 and 3rd answer is explained above.

P.S.: here autowire means dependency injection - .http://www.javatpoint.com/dependency-injection-in-spring

Upvotes: 0

Related Questions