Sum Ergo Mea Culpa Est

Dependency Injection Requests

This is the first musing of maybe many on the fundamentals of (Java-based) dependency injection (DI) systems.

In a DI system, the system has to issue requests to acquire object references denoted by (usually) annotations.

It seems to me there are two kinds of these requests.

One kind is: some "mother" Java object is in the process of being built for some reason. To build it, its references (dependencies, collaborators) need to be acquired. Once they are successfully acquired, and the object itself is properly initialized with them, its build is complete. If or when the object is destroyed, any dependent references that were acquired are also automatically destroyed (see CDI's CreationalContext). Obviously this cascades "through" the object's collaborators. The reference acquisition that goes on in here is of the first kind.

Another kind is: no "mother" Java object is being built. But some kind of "managed" method is being called for some reason, and the references that constitute its arguments need to be acquired. Once they are successfully acquired, the method is invoked. After the invocation, any arguments that were in dependent scope need to be immediately destroyed, but cannot be so destroyed automatically, so must be destroyed manually (see CDI's Instance#destroy(Object)). The reference acquisition (and manually-initiated destruction) that goes on in here is of the second kind.

The second kind of request depends on the first kind of request. That is, building a reference that will be supplied to a method as an argument involves requests of the first kind. So requests of the second kind are primordial in a certain way.

Another way in which these two kinds of requests differ is that requests of the first kind form subtrees of objects, whereas requests of the second kind form trees of objects. That is, requests of the second kind have no parent node; requests of the first kind always have a parent node.

So requests of the first kind take place during constructions. Requests of the second kind take place during invocations.

Or: all constructions are the result of invocations. Invocations beget constructions.

Invocations require manual destruction. Constructions do not, because they are automatically destroyed (as necessary) when the invocation is destroyed.

I'll stop there for now.