1.2.0 • Published 2 years ago

@tkustov/dif v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

DI factories

DI factories is a simple, zero-dependencies toolkit designed to help organize your project's Composition Root. It provides useful helpers for using Pure DI, making it easier to define your project's dependency graph.

With DI factories, you can easily define and manage your project's dependencies in a clear and concise way. This allows you to focus on writing clean and maintainable code, without having to worry about the complexities of managing your project's dependencies.

Whether you're working on a small project or a large-scale application, DI factories can help you simplify your dependency management and ensure that your code remains organized and easy to maintain. So if you're looking for a lightweight, easy-to-use toolkit for managing your project's dependencies, give DI factories a try!

Install

npm install --save @tkustov/dif

Getting started

Desired way to use this toolkit in your project:

  1. Write code units in way they depends only on abstractions (types, interfaces)
  2. Using dif define code unit factories inside project's Composition Root.
  3. Pass these factories to other factories to define dependency graph.
  4. Instantiate main code unit(s) in project's entry point using factories created on previous step and use it.
// file: src/root/units.ts

import { dif } from '@tkustov/dif';
import { service1, service2 } from './services';
import { Unit1 } from '../app/Unit1';

function resolveParam2(value?: string) {
  return value ?? 'default';
}

const param2 = dif.factory((v?: string): string => v ?? 'default', dif.scopes.Singleton)
  .args(process.env.MY_VAR2)
  .end();

export unit1 = dif.ctor(Unit1, dif.scopes.Transient)
  .args(service1, dif.value(process.env.MY_VAR1))
  .setService2(service2)
  .prop('param2', param2)
  .end();
// file: src/app/Unit1.ts

export interface Service1 { }
export interface Service2 { }

export class Unit1 {
  private service2?: Service2;
  public param2?: Service3;

  constructor(
    private service1: Service1,
    private param1: string
  ) { }

  setService2(service2: Service2) {
    this.service2 = service2;
  }
}

Motivation

I need a tool that can help me define dependencies for code units in a TypeScript project. This tool should verify that the types of the passed dependencies match the required types of the code unit. The code units should be written in isolation, depending only on types/interfaces.

After reviewing many DI libraries, I found that they all have drawbacks that I cannot accept, such as:

  • Weak type checking or no type checking during the unit definition process
  • Most of them are actually Service Locators rather than DI containers
  • The DI libraries that do use tokens can be helpful, but they are not very effective

To address these issues, I am developing a new library that provides a better solution for managing dependencies with type checking. With this library, you can easily define and manage your dependencies with confidence, knowing that your code units will have the correct dependencies with the correct types.

Reference

Methods of creation (dif)

There are several methods available for creating units:

MethodParametersReturnsDescription
dif.ctor(\, scope: DIScope)ClassCtor - class constructorDIDepsBuilderCreates an instance of the passed class constructor.
dif.factory(\, scope: DIScope)FactoryFn - factory functionDIDepsBuilderUses the result returned by the factory function.
dif.value(\)Value - any valueDIFactoryCreates a DI factory that always returns the passed value.
dif.derive(\, \)Parent - factory, DeriveFn - function that maps Parent value to new oneDIFactoryCreates a DI factory that always returns the passed value.
dif.compose(\: DIFactory[], \)Same as .derive(), but for few sourcesDIFactoryCreates a DI factory that always returns the passed value.

Dependency definition (DIDepsBuilder)

MethodParametersReturnsDescription
.args(...\: DIFactory[])args - DI factories corresponding to constructor parameter typesDIDepsBuilderDefines values that will be passed during unit creation.
.method(\: string, ...\: DIFactory[])name - method name, args - method argumentsDIDepsBuilderDefines method injection.
.\(...\: DIFactory[])methodName - method name, args - method argumentsDIDepsBuilderAlternative method injection (ES Proxy used).
.prop(\: string, \: DIFactory)name - property name, dep - dependency DI factoryDIDepsBuilderDefines property injection.
.end() DIFactoryCompletes unit definition.

Scopes

  • Singleton: same instance for all consumers.
  • Transient: each consumer will receive their own instance.
1.2.0

2 years ago

1.1.1

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago