3
I have this doubt because if I am not mistaken I have read, seen, or even implemented something of this type but I am no longer remembering, it is the following:
I have here an EJB project with several entities, some of them have very similar ways of saving as for example the entity Contract and Financial Assistance, these two (and more) will implement a list of Competencies and save other basic attributes. The point is that so I don’t have to implement several Services and several Daos with the same save methods by changing a little thing here or there I think I could have something like a service or generic dao that picked up the object of any entity and the entity itself if in charge of saving himself.
We try to do with a Factory that creates the dao corresponding to the type of the passed object but I think that is not yet there... I don’t know for sure but maybe even using addiction injection would be a lot more elegant... Someone could give me a light?
You are using
JPA
? If yes theEntityManager
can be used to persist any entity. While for architectural reasons it is interesting to centralize access to bank, Daos are certainly not a requirement. If you want to adopt a standard recommend the Spring Data JPA, with it you will basically only declare extending interfacesCrudRepository
in the basic case, it does all the rest for you.– Anthony Accioly
Yes @Anthony Accioly, I am using JPA. I know that Entitymanager is capable of saving any entity but I will have to create the DAO of each entity, right? I’ll take a look at the Spring Data JPA to see if it covers the desired...
– Vinícius França
Vinicius, not by necessity. DAO is a standard like any other; you can get Entity Manager wherever you want (in your - rich - domain layer for example, or directly in the service layer if isolating access to the bank is not a priority). Personally I like to have a layer of Daos or Repositories to centralize access, but this is by no means a requirement.
– Anthony Accioly