Discover di-lite An Efficient Lightweight Dependency Injection Library

Introduction to di-lite

di-lite is a powerful and lightweight dependency injection (DI) library for JavaScript/TypeScript applications. It simplifies the management of dependencies in your application, enabling more modular, maintainable, and testable code.

Setting Up di-lite

Getting started with di-lite is simple. First, install the di-lite package through npm:

  npm install di-lite

Registering Dependencies

Registering dependencies allows you to define how dependencies should be resolved.

  import { Container } from 'di-lite';

  const container = new Container();

  container.register('service', () => new Service());

Resolving Dependencies

Once you have registered services in the container, you can easily resolve them.

  const service = container.resolve('service');

Scoped and Singleton Registrations

You can register services as singletons or with scoped lifetimes.

  // Singleton Example
  container.registerSingleton('singletonService', () => new SingletonService());

  // Scoped Example
  container.registerScoped('scopedService', () => new ScopedService());

Auto-Wiring Dependencies

di-lite can automatically inject dependencies into constructors.

  class Consumer {
      constructor(service) {
          this.service = service;
      }
  }

  container.register('consumer', ['service'], (service) => new Consumer(service));

Handling Asynchronous Dependencies

di-lite supports asynchronous dependency resolution.

  container.registerAsync('asyncService', async () => {
      const service = await someAsyncInitialization();
      return service;
  });

  const asyncService = await container.resolveAsync('asyncService');

Mocking Dependencies for Testing

Mock dependencies for more effective testing.

  container.register('service', () => mockService);

Practical Application Example

Here’s a practical example of using di-lite in an application.

  // services.js
  export class Logger {
      log(message) {
          console.log(message);
      }
  }

  export class UserService {
      constructor(logger) {
          this.logger = logger;
      }

      getUser(id) {
          this.logger.log(`Fetching user with id: ${id}`);
          return { id, name: 'User' };
      }
  }

  // app.js
  import { Container } from 'di-lite';
  import { Logger, UserService } from './services';

  const container = new Container();

  container.register('logger', () => new Logger());
  container.register('userService', ['logger'], (logger) => new UserService(logger));

  const userService = container.resolve('userService');
  const user = userService.getUser(1);
  console.log(user);

By employing di-lite in your application, you achieve decoupled, maintainable, and testable code with ease. Whether you are building a small project or a large scalable application, di-lite proves to be an invaluable tool.

Hash: 91952376f50e82726cf2a06a87cc84e9498ce4b672dabea983aab457363ccac7

Leave a Reply

Your email address will not be published. Required fields are marked *