Fundamentals - Dependency Injection IV

Explicit Configuration:

- You may desire to change the behavior of individual classes for the specific needs of application. This is easy to do by either directly using the Container API or by decorating class with a Registration

The Container Registration API:

The usual way to configure a class's lifetime is to use the Container API directly. Typically, you will want to do this configuration up-front in your application's main configure method. The Aurelia instance that is provided during configuration has a container property which points to the root DI container for your application. Recall that any Resolver configured at the application root will apply unless a child container has explicitly overridden the behavior.

container.registerSingleton(key: any, fn?: Function): void - This method allows you to register a class as a singleton. This is the default, so there's rarely a reason to call this method. It is provided in the API for completeness. When calling, provide the key that will be used to look up the singleton & the class which should be used. It's common for the key & class to be the same. If they are the same, then only the key needs to be provided.
e.g. container.registerSingleton(History, BrowserHistory);
       container.registerSingleton(HttpClient);

container.registerTransient(key: any, fn?: Function): void - This method allows you to register a class as transient. This means that every time the container is asked for the key, it will return a brand new instance of the class. The key is required but the class is optional. If left off, the key will be treated as the class to be instantiated.
e.g. container.registerTransient(LinkHandler, DefaultLinkHandler);

container.registerInstance(key: any, instance?: any): void - If you already have an existing instance, you can add that to the container with this method. You just need to pick a key that the instance will be retrievable by. If no key is provided then the key becomes the instance.

container.registerHandler(key: any, handler: (container?: Container, key?: any, resolver?: Resolver) => any): void - In addition to simply declaring behaviors, you can also provide a custom function (a handler) that will respond any time the container is queried for the key. This custom handler has access to the container instance, the key and the internal resolver which stores the handler. This enables just about any sort of custom lifetime to be implemented by supplying a custom function. 
e.g. container.registerHandler('Foo', () => new Bar());

container.registerResolver(key: any, resolver: Resolver): void - You can also register a custom Resolver instance for the key. Under the hood, all previously discussed methods translate to using a built-in Resolver instance. However, you can always supply your own.

container.autoRegister(fn: any, key?: any): Resolver - If a container can't find a registration during its resolution stage, it will auto-register the requested type. That is done internally through the use of autoRegister. However, you can use it yourself to auto-register a type with a particular container instance. By default, this will result in a singleton registration, on the container this API is called on. However, if the type has registration decorators, that could provide an alternate registration. Whatever Resolver is established during auto-registration will be returned.

Registration Keys

All registration APIs take a key. This key is typically the class itself (for convenience). However, the key can be any type, including strings and objects. This is possible because Aurelia's DI implementation uses a Map object to correlate a key to a Resolver. When using class-oriented registration APIs, if the key is not a class, you must provide the class to be created as the second argument to the API call.

Registration Decorators:

As an alternative to explicitly registering types with the container, you can rely on auto-registration, but specify the auto-registration behavior you desire, overriding the default container-root-singleton behavior. To provide auto-registration behavior, you simply decorate your type with an auto-registration decorator. What follows is a basic explanation of built-in registration decorators

transient() - Simply decorate your class with transient() and when it's requested from the container, a new instance will be created for each request.
singleton(overrideChild?:boolean) - Normally, types are auto-registered as singletons in the root container. This decorator allows you to specify true as an argument to indicate that the singleton should be registered not in the root container, but in the immediate container to which the initial request was issued.
registration(registration: Registration) - In addition to the built-in singleton and transient registrations, you can create your own and associate it with a class.

Registration Decorator Usage

At present, the Decorators spec allows for decorators to use parens or not depending on whether or not the decorator requires arguments. This means that decorator invocation is dependent on how the decorator was implemented internally, which can be confusing from time to time. As a result of the way that the registration decorators are implemented, you must use them with parens.

Comments

Popular posts from this blog

Fundamentals - App Config & Startup - VIII

Fundamentals - Dependency Injection V

Style vs Css attribute