Fundamentals - Dependency Injection

- When building applications, it's often necessary to take a "divide & conquer" approach by breaking down complex problems into a series of simpler problems.
- A dependency injection container is a tool that can simplify the process of decomposing such a system.

Injection:

Let's say we have a CustomerEditScreen that needs to load a Customer entity by ID from a web service. We wouldn't want to place all the details of our AJAX implementation inside our CustomerEditScreen class. Instead, we would want to factor that into a CustomerService class that our CustomerEditScreen, or any other class, can use when it needs to load a Customer. Aurelia's dependency injection container lets you accimplish this by declaring that the CustomerEditScreen needs to have a CustomerService injected at creation time.

import {CustomerService} from 'backend/customer-service';
import {inject} from 'aurelia-framework';

@inject(CustomerService)
export class CustomerEditScreen {
           constructor(customerService) {
                             this.customerService = customerService;
                             this.customer = null;
           }
           activate(params) {
                        return this.customerService.getCustomerById(params.customerId)
                                  .then(customer => this.customer = customer);
           }
}

inject decorator & the constructor signature matches the list of dependencies in the inject decorator.

- The DI that any time it wants to create an instance of CustomerEditScreen it must first obtain an instance of CustomerService which it can inject into the constructor of CustomerEditScreen during instantiation.
- We can have as many injected dependencies as you need. Simply ensure that the inject decorator & the constructor match one another.
e.g.
import {CustomerService} from 'backend/customer-service';
import {CommonDialogs} from 'resources/dialogs/common-dialogs';
import {EventAggregator} from 'aurelia-event-aggregator';
import {inject} from 'aurelia-framework';

@inject(CustomerService, CommonDialogs, EventAggregator)
export class CustomerEditScreen {
           constructor(customerService, dialogs, ea) {
                             this.customerService = customerService;
                             this.dialogs = dialogs;
                             this.ea = ea;
                             this.customer = null;
           }
           activate(params) {
                        return this.customerService.getCustomerById(params.customerId)
                                  .then(customer => this.customer = customer)
                                  .then(customer => this.ea.publish('edit:begin', customer));
           }
}

- To use decorators in Babel, you need the babel-plugin-transform-decorators-legacy plugin. To use them in Typescript, you need to add the "experimentalDecorators": true setting to the compilerOptions section of your tsconfig.json file. Aurelia projects typically come with these options pre-configured.

- If your are using TypeScript, you can take advantage of an experimental feature of the language to have the TypeScript transpiler automatically provide Type information to Aurelia's DI. You can do this by configuring the TypeScript compiler with the "emitDecoratorMetadata": true option in the compilerOptions section of your tsconfig.json file. If you do this, you don't need to duplicate the type information with inject, instead, as long as your constructor definition conntains its parameters'types, you can use Aurelia's autoinject decorator like this:

import {CustomerService} from 'backend/customer-service';
import {CommonDialogs} from 'resources/dialogs/common-dialogs';
import {EventAggregator} from 'aurelia-event-aggregator';
import {autoinject} from 'aurelia-framework';

@autoinject
export class CustomerEditScreen {
           constructor(private customerService: CustomerService, private dialogs: CommonDialogs, private ea: EventAggregator) {
                             this.customer = null;
           }
           activate(params) {
                        return this.customerService.getCustomerById(params.customerId)
                                  .then(customer => this.customer = customer)
                                  .then(customer => this.ea.publish('edit:begin', customer));
           }
}

- Interestingly, you don't need to use our autoinject decorator at all to get the above to work. The TypeScript compiler will emit the type metadata if any decorator is added to the class. Aurelia can read this metadata regardless od what decorator triggers TypeScript to add it. We simplyprovide the autoinject decorator for consistency & clarity.

- you can easily provide inject metadata using a simple static method or property on class

import {CustomerService} from 'backend/customer-service';
import {CommonDialogs} from 'resources/dialogs/common-dialogs';
import {EventAggregator} from 'aurelia-event-aggregator';

export class CustomerEditScreen {
           static inject ( ) { return [CustomerService, CommonDialogs, EventAggregator]; }

           constructor(customerService, dialogs, ea) {
                             this.customerService = customerService;
                             this.dialogs = dialogs;
                             this.ea = ea;
                             this.customer = null;
           }
           activate(params) {
                        return this.customerService.getCustomerById(params.customerId)
                                  .then(customer => this.customer = customer)
                                  .then(customer => this.ea.publish('edit:begin', customer));
           }
}

- In addition to a static inject method, a static inject property is also supported. In fact, the inject decorator simply sets the static property automatically.
- You could even create your own decorator.
- DI works in a recursive fashion

Comments

Popular posts from this blog

Fundamentals - App Config & Startup - VIII

Fundamentals - Dependency Injection V

Style vs Css attribute