1.1.1 • Published 1 year ago
di-injectable v1.1.1
DI-Injectable
A simple Dependency Injection (DI) library for TypeScript supporting Singleton and Transient service lifetimes.
Installation
First, install the package via npm or yarn:
npm install di-injectable
yarn add di-injectableUsage
Setting Up Services
- Define Services: Create your service classes and use the @Injectable decorator and use
ServiceLifetimeenum to register your services as Singleton or Transient.. - Resolve Services: Use the ServiceProvider to resolve instances of your services.
Example
Let's walk through a complete example.
- Define Services Create some simple services and use the @Injectable decorator.
// src/services/logger.ts
import { Injectable } from 'di-injectable';
@Injectable(ServiceLifetime.Singleton)
export class Logger {
log(message: string) {
console.log(`Logger: ${message}`);
}
}// src/services/userService.ts
import { Injectable, Inject } from 'di-injectable';
import { Logger } from './logger';
@Injectable()
export class UserService {
constructor(@Inject(Logger) private logger: Logger) {}
getUser() {
this.logger.log('Getting user...');
return { id: 1, name: 'John Doe' };
}
}- Resolve Services Use the ServiceProvider to resolve instances of your services.
// src/app.ts
import { ServiceProvider } from 'di-injectable';
import { UserService } from './services/userService';
const serviceProvider = new ServiceProvider();
const userService = serviceProvider.resolve<UserService>(UserService);
const user = userService.getUser();
console.log(user);Explanation
- Defining Services:
- The
Loggerservice is a simple logger class. - The
UserServiceclass depends on theLoggerservice. The@Injectdecorator is used to inject theLoggerservice - into the UserService constructor.
- The
- Registering Services:
- We register the
Loggerservice as a Singleton, meaning only one instance ofLoggerwill be created and shared. - We register the
UserServiceas a Transient by default, meaning a new instance ofUserServicewill be created every time it is resolved.
- We register the
- Resolving Services:
- We create a
ServiceProviderinstance. - We resolve an instance of
UserServiceusing theserviceProvider. TheUserServicewill have theLoggerinstance injected into it due to the@Injectdecorator.
- We create a
Service Lifetimes
- Singleton: Only one instance of the service is created and shared.
- Transient: A new instance of the service is created every time it is requested.
API Reference
- ServiceProvider:
resolve<T>(token: any): T: Resolves an instance of the service.Injectable: Decorator to mark a class as injectable as register it.Inject: Decorator to inject dependencies into the constructor.
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Steps to Use
- Create Your Services: Define your services using the
@Injectabledecorator and useServiceLifetimeenum to register your services as Singleton or Transient. - Resolve Services: Use
ServiceProviderto resolve instances of your services.