Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

controllers, entity classes or dao - what goes where?

With the introduction of Hibernate in my project, my code started getting really coupled, and boilerplate in many places (and it should be the other way round, right?)

I got pretty confused by a particular example. I've always considered DAO objects to be pretty generic in their nature (mostly encapsulating the basic CRUD oeprations as well as the backend storage implementation)

Unfortunately, as my entity classes started to get more complicated, I started offloading more and more logic to the DAO objects. I have a particular example:

my entity class User should have a relation called friends, which is essentially a collection of users. However, I have to map my class to a collection of UserFriendship objects instead, each of which contains a ref to the friend object, but also other specific friendship data (the date when the friendship occurred)

Now, it is easy to introduce a custom getter in the entity class, which will take the collection of UserFriendship objects and turn it into a collection of User objects instead. However, what if I need only a subset of my friends collection, say, like in paging. I cannot really do that in the entity object, because it doesn't have access to the session, right? This also applies to when I need to make a parametrized query on the relationship. The one that has the access to the session is the UserDAO. So I ended up with this

UserDAO => normal CRUD methods => getFriends(Integer offset, Integer limit); => a bunch of similar getters and setters responsible for managing the relationships within the User instance.

This is insane. But I cannot really do anything else. I am not aware if it is possible to declare computed properties within the entity classes, which could also be parametrized.

I could technically also wrap the DAO within the entity, and put the helper getters and setters back into the entity class, where they should be, but I am not sure whether if that is a good practice as well.

I know that the DAO should only be accessed by the controller object, and it should provide a more or less complete entity object or a set of entity objects.

I am deeply confused. More or less all of my DAO objects now couple logic that should be either in the Entity objects or in the controllers.

I am sorry if my question is a bit confusing. It is a bit hard to formulate it.

like image 566
user802232 Avatar asked Jul 13 '11 16:07

user802232


People also ask

Is DAO same as entity?

An entity is an object representation of data pulled from your DAO. It may or may not align exactly with your model, in which case a DTO could help translate from Entity to Model (or Model to serialized data for export). The "model" is essentially a code representation of your data store.

Which is used to pass the values between controller and DAO layer?

Answer: True The Model Map is used to pass the values between controller and DAO layer.

What is controller and DAO?

The Controller is a server side component (for web apps) that accepts requests from View clients, updates the Model appropriately, and sends results back to any and all View clients that need it. Service/DAO extends those layers to the server side.

What is Controller Service DAO layer?

The Service LayerThe DAO layer's main goal is to handle the details of the persistence mechanism, while the service layer stands on top of it to handle business requirements.


1 Answers

My general rules are:

  • in the entity classes, respect the law of Demeter: don't talk to strangers
  • the entity classes must not use the session
  • the controller/service classes must not use the session. They may navigate in the graph of entities and call DAO methods
  • DAO methods should be the ones using the session. Their work consists in getting, saving, merging entities and executing queries. If several queries or persistence-related actions should be executed for a single use-case, the controller/service should coordinate them, not the DAO.

This way, I can test the business logic relatively easily by mocking the DAOs, and I can test the DAOs relatively easily because they don't contain much logic. Most of the tests verify that the queries find what they're supposed to find, return them in the appropriate order, and initialize the associations that must be initialized (to avoid lazy loading exceptions in the presentation layer, where I'm using detached objects)

like image 135
JB Nizet Avatar answered Sep 26 '22 23:09

JB Nizet