Fundamentals - Dependency Injection II

Object Lifetime, Child Containers & Default Behavior:

Container Singleton: 

A singleton class, A, is instantiated when it is first needed by the DI container. The container then holds a reference to class A's instance so that even if no other objects reference it, the container will keep it in memory. When any other class needs to inject A, the container will return the exact same instance. thus, the instance of A has its lifetime connected to the container instance. It will not be garbage collected until the container itself is disposed & no other classes hold a reference to it.

Application singleton:

In Aurelia, It's possible to have child DI containers created from parent containers. Each of these child containers inherits the services of the parent, but can override them with their own registrations. Every application has a root DI container from which all classes & child containers are created. An application singleton is just like a container singleton,except that the instance is referenced by the root DI container in the application. This means that the root & all child containers will return the same singleton instance, provided that a child container doesn't explicitly override it with its own registration.

Transient:

Any DI container can create transient instances. These instances are created each time they are needed. The container holds no references to them & always creates a new instance for each request.

Any class can be registered in a container as singleton or transient(or custom). Most classes are auto-registered by Aurelia. That is, there is no upfront configuration, but when an instance of class A is first needed, it is registered automatically at that point in time & then immediately resolved to an instance.

Example 1 - Root Container Resolution

Imagine that we have a single instance of  Container called root. If a developer invokes root.get(A) to resolve an instance of A, the root will first check to see if it has a Resolver for A. If one is found, the Resolver is used to get instance, which is then returned to the developer. If one is not found, the container will auto-register a Resolver for A. This resolver is configured with a singleton lifetime behavior. Immediately after auto-registration, the Resolver is used to get the instance of A which is returned to the developer. Subsequent calls to root.get(A) will now immediately find a Resolver for A which will return the singleton instance.

Example 2 - Child Container Resolution

Imagine that we have a Container named root & we call root.createChild( ) to create a child container named child. Then, we invoke child.get(A) to resolve an instance of A. First, child checks for a Resolver for A. If none is found, then it calls get(A) on its parent which is the root container from which it was created. root then checks to see if it has a Resolver.  If not, it auto-registers A in root & then immediately calls the Resolver to get an instance of A.

The Container basically walks its hierarchy until it either finds a Resolver or reaches the root. If no Resolver is found in the root, it auto-registers the class as a singleton in the root. This means that all auto-registered classes are application-wide singletons, unless they are overridden by a child container 


Comments

Popular posts from this blog

Fundamentals - App Config & Startup - VIII

Fundamentals - Dependency Injection V

Style vs Css attribute