It is common in Java applications to have :
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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With