<commit_before># Enable @singleton and @transient decorators on class methods

[![CircleCI](https://circleci.com/gh/dryajov/aurelia-factory-methods/tree/master.svg?style=svg)](https://circleci.com/gh/dryajov/aurelia-factory-methods/tree/master)

This module allows using the `@transient` and `@singleton` decorators on class methods, which in turn allows using factories to create dependencies that decouple registration.

## What's the use case?

The main use case is creating configuration classes that allow registering dependencies implicitly through factory methods, rather than explicitly using the constructor.

Consider the following example:

```javascript
// app.ts

import 'reflect-metadata';

import {autoinject, Container} from 'aurelia-dependency-injection';
import {Logger, Config} from './config-class1';

@autoinject
export class App {
  constructor(private config: Config, public logger: Logger) {
  }
}

let container = new Container();
let app = container.get(App);

console.log(`Type of Logger is: ${app.logger.toString()}`);

// config.ts
import {autoinject, Container} from 'aurelia-dependency-injection';

export class Logger {
  public toString(): string {
   return 'Logger';
  }
}

@autoinject()
export class Config {
  constructor(container: Container) {
    container.registerSingleton(Logger);
  }
}
```

In the above snippet, we have a config class that registers a `Logger` in the constructor and an `App` that consumes the logger, however note the fact that we have to explicitly request the config class. This creates tight coupling between the consuming app and the configuration class. This isn't a big issue when your configuration is contained within an app, but it is, when you're trying to consume third party modules that provide their own configuration. The fact that you have to _know_ which class to request as opposed to the class running implicitly as part of the import process of a module creates this tight coupling and makes distributing modules that provide configuration through DI that much harder.

A better approach is having the configuration class provide dependencies through factory methods, this removes the need to _explicitly_ request the configuration class, and instead, running one of the decorators `@singleton` or `@transient` as part of the import process of the third party module, should be enough to register the required dependencies. E.g:

```javascript
// app.ts
import 'reflect-metadata';

import {autoinject, Container} from 'aurelia-dependency-injection';
import {Logger} from './config-class';

@autoinject
export class App {
  constructor(public logger: Logger) {
  }
}

let container = new Container();
let app = container.get(App);

console.log(`Type of Logger is: ${app.logger.toString()}`);

// config.ts
import {autoinject} from 'aurelia-dependency-injection';
import {singleton} from '../../lib/registration';

export class Logger {
  public toString(): string {
    return 'Logger';
  }
}<commit_msg>improving readme<commit_after># Enable @singleton and @transient decorators on class methods

[![CircleCI](https://circleci.com/gh/dryajov/aurelia-factory-methods/tree/master.svg?style=svg)](https://circleci.com/gh/dryajov/aurelia-factory-methods/tree/master)

This module allows using the `@transient` and `@singleton` decorators on class methods, which in turn allows using factories to create dependencies that decouple registration.

## What's the use case?

The main use case is creating configuration classes that allow registering dependencies implicitly through factory methods, rather than explicitly using the constructor.

Consider the following example:

```javascript
// app.ts

import 'reflect-metadata';

import {autoinject, Container} from 'aurelia-dependency-injection';
import {Logger, Config} from './config-class1';

@autoinject
export class App {
  constructor(private config: Config, public logger: Logger) {
  }
}

let container = new Container();
let app = container.get(App);

console.log(`Type of Logger is: ${app.logger.toString()}`);

// config.ts
import {autoinject, Container} from 'aurelia-dependency-injection';

export class Logger {
  public toString(): string {
   return 'Logger';
  }
}

@autoinject()
export class Config {
  constructor(container: Container) {
    container.registerSingleton(Logger);
  }
}
```

In the above snippet, we have a config class that registers a `Logger` in the constructor and an `App` that consumes the logger, however note the fact that we have to explicitly request the config class. This creates tight coupling between the consuming app and the configuration class. This isn't a big issue when your configuration is contained within an app, but it is, when you're trying to consume third party modules that provide their own configuration. The fact that you have to _know_ which class to request as opposed to the class running implicitly as part of the import process of a module creates this tight coupling and makes distributing modules that provide configuration through DI that much harder.

A better approach is having the configuration class provide dependencies through factory methods, this removes the need to _explicitly_ request the configuration class, and instead, running one of the decorators (`@singleton` or `@transient`) as part of the import process of the third party module, which should be enough to register the required dependencies. E.g:

```javascript
// app.ts
import 'reflect-metadata';

import {autoinject, Container} from 'aurelia-dependency-injection';
import {Logger} from './config-class';

@autoinject
export class App {
  constructor(public logger: Logger) {
  }
}

let container = new Container();
let app = container.get(App);

console.log(`Type of Logger is: ${app.logger.toString()}`);

// config.ts
import {autoinject} from 'aurelia-dependency-injection';
import {singleton} from '../../lib/registration';

export class Logger {
  public toString(): string {
    return 'Logger';
  }
}
{'fraction_non_alphanumeric': 0.09025572455289989, 'fraction_numerical': 0.00033428046130703663, 'mean_word_length': 4.978021978021978, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3536205', 'n_tokens_mistral': 1579, 'n_tokens_neox': 1515, 'n_words': 688}