Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java type conversion between layers

It is common in Java applications to have :

  • an IHM layer
  • a service layer
  • a dao layer

It is common that each layer has it's own objects and relies on converters to transform objects from a layer to the other.

In my case, I use Sing MVC as the IHM layer, and MyBatis as the doa layer.

Since Spring MVC and MyBatis use only beans without any annotations on that beans :

Is it necessary to have differents objects on the 3 layers ? Is it a good practice to share the same object between the 3 layers ? Or why it is not recommended to do so ?

like image 700
fluminis Avatar asked Jan 25 '26 01:01

fluminis


1 Answers

In most cases it is a good practice to keep a full seperation between layers.

Lets say you have an object called UserDAO that holds user data, and after writing quite a lot of code on all layers you decide to update sureName to be lastName in the object, now you have to go through all the layers and change every reference to that object field.

On the other hand, if you kept this object on the DB layer and mapped it to other objects on the upper layers, all you need to change is the mapping:

somObj.setUserLastName(dao.getSureName());

to:

somObj.setUserLastName(dao.getLastName());

of course this is a silly example, but it is just for demonstration.

On the other hand, seperating every object on every layer might cause a lot of redundency and duplication and can turn into a maintenance horror, so you need to think of the pros and cons and decide where it is most suitable to seperate and where not.

like image 83
Tomer Avatar answered Jan 27 '26 13:01

Tomer