1.0.0 • Published 7 months ago

di-stateless v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

di-stateless

Transient services similar to ASP.Net Core. A service that is created every time it is requested from a service container

Usage

import { stateless } from 'di-stateless';

const TransientService = stateless(
  class {

    randomId = randomString();

    constructor() {
      console.log("TransientService CTOR");
    }

    methodFoo = () => this.randomId;
    methodBaz = () => this.randomId;
    methodBar = () => this.randomId;

    entry = () => console.log({
      foo: this.methodFoo(),
      bar: this.methodBar(),
      baz: this.methodBaz(),
    });

  }
);

const service = new TransientService();

service.entry(); // { foo: "ndjol", bar: "ndjol", baz: "ndjol" }

service.randomId = "not-random-id";

service.entry(); // { foo: "c2wiyf", bar: "c2wiyf", baz: "c2wiyf" }