tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

README.MD example uses old version of inversify-binding-decorators

Open pppcrf opened this issue 8 years ago • 7 comments

inversify-binding-decorators 4.0 does not any more have makeProvideDecorator and makeFluentProvideDecorator exports used in the example.

pppcrf avatar Aug 08 '18 11:08 pppcrf

Any chance you could update the README with how it's supposed to be used now?

endor avatar Aug 09 '18 09:08 endor

Do you know already how to refresh examples written in here https://github.com/lukeautry/tsoa#dependency-injection-or-ioc ? I would really love to make my controllers more independent of imports.

gitowiec avatar Jul 10 '19 15:07 gitowiec

I tried really hard :D but I failed. Here is my ioc.ts

import { Container, inject, interfaces } from 'inversify';
import {
  autoProvide,
  provide,
  fluentProvide,
} from 'inversify-binding-decorators';

let provideNamed = function (
  identifier: string | symbol | interfaces.Newable<any> | interfaces.Abstract<any>,
  name: string,
) {
  return fluentProvide(identifier)
    .whenTargetNamed(name)
    .done();
};

let provideSingleton = function (
  identifier: string | symbol | interfaces.Newable<any> | interfaces.Abstract<any>,
) {
  return fluentProvide(identifier)
    .inSingletonScope()
    .done();
};

export { autoProvide, provide, provideSingleton, provideNamed, inject };

Pay attention that I removed makeProvideDecorator, makeFluentProvideDecorator because "inversify-binding-decorators" package does not export them more. I had to remove also these lines

let iocContainer = new Container();

let provide = makeProvideDecorator(iocContainer);
let fluentProvider = makeFluentProvideDecorator(iocContainer);

Because I have another place to make Container instance, there I have it in container.ts

const utils = ...
const useCases = ...
const services = ...
const servicesClients = ...
const repositories = ...
const useCases = new ContainerModule((bind: Bind) => {
  bind<GetClientUseCase>(USE_CASE_IDENTIFIER.GetClientUseCase).to(GetClientUseCase);
  bind<AddClientUseCase>(USE_CASE_IDENTIFIER.AddClientUseCase).to(AddClientUseCase);
  bind<UpdateClientUseCase>(USE_CASE_IDENTIFIER.UpdateClientUseCase).to(UpdateClientUseCase);
  bind<DeleteClientUseCase>(USE_CASE_IDENTIFIER.DeleteClientUseCase).to(DeleteClientUseCase);
  bind<GetClientsUseCase>(USE_CASE_IDENTIFIER.GetClientsUseCase).to(GetClientsUseCase);
});
export const loadAll = (): Container => {
  const container = new Container();
  container.load(buildProviderModule());
  container.load(utils, useCases, services, servicesClients, repositories);
  return container;
};

Then I tried to use this configuration to inject useCases into my controllers but it didn't help. tsoa generate worked flawlessly but I got runtime errors - TSOA couldn't find my controllers. So I decided to add to container.ts

const controllers  = new ContainerModule((bind: Bind) => {
  bind<GetClientController>(GetClientController).to(GetClientController);
});

and load it into container

export const loadAll = (): Container => {
  const container = new Container();
  container.load(buildProviderModule());
  container.load(utils, useCases, services, servicesClients, repositories, controllers);
  return container;
};

And what the result was - it made runtime finding the controller but throwing more exceptional stuff at me :( As I am like a child in a mist, I don't really grasp very specific details of IoC and Inversify. But I leave it here, maybe someone would pick it up from here.

gitowiec avatar Jul 10 '19 17:07 gitowiec

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

github-actions[bot] avatar Aug 30 '19 00:08 github-actions[bot]

use an older version --> "inversify-binding-decorators": "^3.2.0"

sratemo avatar Sep 29 '19 21:09 sratemo

As at the time of wriiting this, I'm using inversify-binding-decorators version ^4.0.0 (which was the latest)

Update the file like so...

./inversify/ioc.ts

import {
    autoProvide,
    fluentProvide,
    provide
} from "inversify-binding-decorators";
import { Container, interfaces, inject } from "inversify";

const iocContainer = new Container();

const provideNamed = (
    identifier:
        | string
        | symbol
        | interfaces.Newable<any>
        | interfaces.Abstract<any>,
    name: string
) =>
    fluentProvide(identifier)
        .whenTargetNamed(name)
        .done();

const provideSingleton = (
    identifier:
        | string
        | symbol
        | interfaces.Newable<any>
        | interfaces.Abstract<any>
) =>
    fluentProvide(identifier)
        .inSingletonScope()
        .done();

export {
    iocContainer,
    autoProvide,
    provide,
    provideSingleton,
    provideNamed,
    inject
};

ofuochi avatar Oct 10 '19 18:10 ofuochi

Updated DI docs

WoH avatar May 03 '20 12:05 WoH