Software design with rich classes and CDI

Asked

Viewed 44 times

0

Good night. I’ve read a lot about software architecture using rich classes, dependency injection and such. According to what I have read, the ideal is to receive in the constructor the information it needs and receive dependencies by injection of dependency. But there lies my doubt.

If I instantiate a class manually to pass the values to the object this class is not instantiated by CDI then it cannot resolve the dependencies.

If I leave the class to be instantiated by the CDI can’t pass the values to the class by constructor so I have to fill my classes with setters and end the encapsulation.

I thought of creating a class that inserts the dependencies by Reflection in the constructor and make all my Beans inherit this class but honestly I don’t find the solution very good...

Someone has a suggestion for me?

  • Hello, Jonhatan. Welcome to [en.so]! The idea is that all "values" are declared via CDI and you don’t need to pass anything manually. It would be nice if you included in the question an example of what value you want to pass on so someone can give a more concrete answer.

  • Okay, I think you’re confusing things. An entity like a Usuario should not instantiated by CDI, because it is an entity and not a component. There basically you create a CDI component, for example UsuarioRepository, which has a method capable of inserting a user into the bank, for example incluir(Usuario u). Then at the time of calling the method you inject the UsuarioRepository in the class that is running, creates a Usuario defining its attributes using the constructor or through setters and calls the method incluir passing the user. That would be your question?

  • It would be that, but to have a class that receives a User and performs the recording of that in the database would not expose the behavior of the User class? I was reading a book by SOLID (from the code house. Very good by the way) that considers this technique as structured programming, since the behavior is defined outside the domain of the User class. I get a little confused about how to implement this.

  • A book about SOLID (where S = Single Responsibility Principle) says that the class that maintains the state (set of attributes or values of an entity) is the same responsible for interface with the bank? Either you got it wrong or this book has a serious problem. Note that you may have one Usuario as a system component or agent. The problem is that you do not persist the component or agent but a set of attributes. That’s not exposing behavior.

  • Similarly, encapsulating this set of attributes while you end up with SQL code or some framework spread throughout the code does not seem to me a very balanced exchange. But of course I’m making assumptions here and all of this could just be a mess of terminology. To better understand the problem, I suggest you edit your question and put the relevant snippets of code you have so far. Hug.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.