thiagobustamante / typescript-ioc

A Lightweight annotation-based dependency injection container for typescript.
MIT License
526 stars 64 forks source link

Export Snapshot in index #57

Closed Kampfmoehre closed 4 years ago

Kampfmoehre commented 4 years ago

Hello,

we use your library in our backends and we like it very much. I just updated to the new 3.0 version and bumped across your Snapshot change. For our unit tests, it would be handy if the Snapshot type would directly be exported by your library.

Right now we must write our tests like this:

import { Container } from "typescript-ioc";
import { Snapshot } from "typescript-ioc/dist/model";

describe("SomeController", () => {
  let controller: TestController;
  let dbService: DatabaseService;
  let dbServiceSnapshot: Snapshot;

  beforeEach(() => {
    dbServiceSnapshot = Container.snapshot(DatabaseService);
    Container.bind(DatabaseService).factory(() => dbService);
    controller = new TestController();
  });

  afterEach(() => {
    dbServiceSnapshot.restore();
  });
});

For convenience it would be cool, if we don't have to import the Snapshot from the dist directory.

Edit: For bonus points it would be very awesome if the ObjectFactory type would be generic. Our factories look like this:

const configServiceFactory: ObjectFactory = () => {
  const configurationDirs = [path.resolve("config"), path.resolve("config_local")];
  return new ConfigurationService(configurationDirs, true);
};

If we could specify it as const configServiceFactory: ObjectFactory<ConfigurationService> the compiler would tell us, if we return a wrong class for example.

thiagobustamante commented 4 years ago

Hi @Kampfmoehre ,

Both of your feedbacks makes sense. The Snapshot was Fixed in 3.0.4 version

Kampfmoehre commented 4 years ago

@thiagobustamante Thank you very much, I will check it out next monday.