TypeDI
Github
Current Release
Current Release
  • Old documentation
  • Getting Started
  • Usage Guide
    • Container API
    • @Service decorator
    • @Inject decorator
    • Service Tokens
    • Inheritance
    • Usage with TypeORM
  • Advanced Usage
    • Creating custom decorators
    • Using scoped container
    • Transient services
  • Usage without TypeScript
    • Getting Started
    • Usage
      • Old documentation
Powered by GitBook
On this page

Was this helpful?

  1. Advanced Usage

Creating custom decorators

NOTE: This page is a direct copy of the old documentation. It will be reworked.

You can create your own decorators which will inject your given values for your service dependencies. For example:

// Logger.ts
export function Logger() {
  return function (object: Object, propertyName: string, index?: number) {
    const logger = new ConsoleLogger();
    Container.registerHandler({ object, propertyName, index, value: containerInstance => logger });
  };
}

// LoggerInterface.ts
export interface LoggerInterface {
  log(message: string): void;
}

// ConsoleLogger.ts
import { LoggerInterface } from './LoggerInterface';

export class ConsoleLogger implements LoggerInterface {
  log(message: string) {
    console.log(message);
  }
}

// UserRepository.ts
@Service()
export class UserRepository {
  constructor(@Logger() private logger: LoggerInterface) {}

  save(user: User) {
    this.logger.log(`user ${user.firstName} ${user.secondName} has been saved.`);
  }
}
PreviousAdvanced UsageNextUsing scoped container

Last updated 3 years ago

Was this helpful?