Home > Software design >  In my MVC project, can I have a single simplified generic "DAO"?
In my MVC project, can I have a single simplified generic "DAO"?

Time:04-10

It's a modern architecture question based on Java spring/springboot MVC.

Whenever I build a DAO for each classDao it is very heavy to have N interfaces, N classes, N DTOs... and when I have to make a DAO for a class that has many queries, it becomes too long.

My question is based on whether it is correct to simplify and make it generic or with objects.

For example.

In MVC, can I have a single "DAO" in my project with a "single crud method" and have the controller handle the response of this DAO method? instead of having N methods with N DAOS?

like:

1 - View -> Call Controller

2 - Controller -> sends the query through the services

3 - Services layer -> points to a single DAO

4 - DAO -> receives the query and that there is only one method that returns objects, "?"(generic) or resultset and that these are treated in the controller)

I mention it because they would all point to a single "method" where it would be managed through the controller.

So every project would only have 1 single DAO and everyone would point to it instead of having 1 DAO for each Class.

I understand that architecture issues depend on the programmer, there are programmers who say:

  • if it works, then it's fine
  • everyone does it their way
  • each project is a world.

Sometimes we have found that development is tedious, so why not make it a little more agile. We talk about best practices and simplifying the DAO without trying to break the MVC model.

CodePudding user response:

In MVC, can I have a single "DAO" in my project with a "single crud method" and have the controller handle the response of this DAO method? instead of having N methods with N DAOS?

Yes, you can create a DAO class that will do the job for you, but you will violate one of the most important rules of the SOLID principles that is de Single Responsability. The truth is that, there is already a kind of unique DAO in tools such as ORMs (Hibernate, EclipseLink and Spring Data which build on top of Hibernate).

The principle behind ORMs is to take your Model, scan the fields throught reflection, and generate the appropriate query for you. Many details are hide and it helps avoid many boilerplates related to Data Acces Layer design.

In your case, if you still want to use your own DAO, you will have to use generics by defining a single interface or a single abstract class, that all your classDAO will implement.

Just choose the right tool, for the right situation.

  • Related