Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Latest commit

 

History

History
84 lines (54 loc) · 2.19 KB

component-declaration.md

File metadata and controls

84 lines (54 loc) · 2.19 KB

Component Declaration

Introduction

Declaring a component allows its lifecycle to be managed by es-injection.

The decorators necessary for component declaration can be imported from the @es-injection/decorators module.

Basic Declaration

Declaring a component is as simple as adding a @Component decorator to a class.

import {Component} from '@es-injection/decorators';

@Component
class MyDependency {

}

Once declared a component, the dependency can be injected into other components.

Component Stereotypes

The @Component decorator is used to declare a generic component.

As an alternative, it is also possible to use one of three more specific stereotypes:

  • @Controller, dedicated to the presentation layer
  • @Repository, dedicated to the persistence layer
  • @Service, dedicated to the service layer

All four decorators exhibit the same behavior, but the annotated code is usually more readable when using the proper stereotype.

It also makes it possible to filter components based on their stereotype.

Named Components

The aforementioned decorators also make it possible to give a name to a component.

import {Component} from '@es-injection/decorators';

@Component('my-dependency-name')
class MyDependency {

}

Components with a name can be queried by name, either programmatically through the application context or using the @Named decorator.

Abstract Class vs. Interface

Currently, interfaces are not concrete types and do not get compiled into an actual object.

Due to this, it is not possible to add decorators to an interface and then retrieve components through this interface.

It, however, possible to achieve something quite similar by creating an abstract class with nothing but abstract methods.

The following interface:

interface MyComponent {
    myMethod(): void;
}

Thus becomes:

abstract class MyComponent {
    abstract myMethod(): void;
}