Assisted-injection in Guice

173 Views Asked by At

I'd like some advise on the use of assisted injection.

Most of the example that i saw about assisted injection, have the following characteristic:

The class being constructor has a parameter, that comes one or more parameter that only comes at runtime, that is, that can't be determined at construction time, and one or many parameters that are known at construction time. Hence, the create method of the factory only takes, the parameter provided at runtime.

In my case 90% of the time, i just have the runtime parameter. Meaning, I have a class A that needs a class B, but B takes parameters that only A can provide. So there is only @assister Parameter in the constructor of B.

I'm wondering if that is the proper usage of assisted injection.

One of my last example is the following. A needs to return a class B that is a dataStructure (sometimes called DTO, or value object), hence i pass to A the necessary factory, so that after its computation he can return B.Here B, is like a container class. I have few case like that

..

The other situation is when service A needs the B Service, but B service depends of a Parameter from A upon creation. Where the parameter from A his himself injected to A, however, A does some initialization on it, before creating B with it

.

Especially in the last case shall i aim to do the "internal initialization of A" outside of A.

Is the idea is to have specific creation modules in each package, that externalizes the "internal creation of the services" of that package?

I'm saying this because I always thought that, creating things somewhere that is unrelated to the class/service that is going to use it sounds odd to me. I do not mean that i do not adhere to separating usage from construction, but i would rather do it in an enclosing class something like that. Not in a place far away completely unrelated.

With my current understanding, each package should provide its creation module, that centralizes the creation of the object that it contains, especially if one use private constructors.

I have used Guice so far for a while, but it is just recently that i red something about separating module, and keeping module with their package. I use to do inheritance between module to chain the creation of object, and ended up having one module that creates everything from start. That is wrong right ?

So that is it. I would appreciate some general advice on the right approach in using Guice. I'm not sure that i'm using assisted injection the right way.

0

There are 0 best solutions below