Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Daniil
    @reflash
    hm, I just use constructor injection, so I don't need to use any container when testing specific class

    anyway, for integration tests I do something like this

    const classMock = mock<IClass>();
    container.rebind<IClass>(TYPES.Class).toConstantValue(classMock);

    Problem is you have to reset container after tests to not bring your mocks to other tests

    Daniil
    @reflash
    @ericmaicon if you want to do something similar to your approach that will work only if you set this interface to be in singleton scope. Otherwise you get different objects every time you get/resolve dependency from the container
    Berger92
    @Berger92
    Hi Guys, this might be stupid, but is this package still being maintained? Last publish was from 2 years ago
    Daniil
    @reflash
    @Berger92 I've noticed the same :( but I found no better alternative for js/ts
    R
    @rsheynin
    I would like to Integrate InversifyJS to NestJS
    Is it a good Idea?
    How I can do it?
    y-chen
    @y-chen
    @rsheynin NestJS has it's own dependency injection system but anyway I am also using inversify with it. I don't if it's good or bad but I get the container in the controller and use the get function to inject the service I need in that controller
    It works
    Shakeel Osmani
    @shakeelosmani

    Hi there, here is my repo

    As you can see in my CustomerServiceImpl I am not using constructor injection as I run into an error:

    private _customerRepository: CustomerRepository // = DIContainer.get(ServiceIdentifier.CUSTOMERREPO);
      /**
       *
       */
      public constructor(@inject(ServiceIdentifier.CUSTOMERREPO) customerRepo: CustomerRepository) {
        this._customerRepository = customerRepo;
      }

    __param(0, inversify_1.inject(DIContainer_1.ServiceIdentifier.CUSTOMERREPO)),
    [1] ^
    [1]
    [1] TypeError: Cannot read property 'CUSTOMERREPO' of undefined

    Any idea why this is happening I am new to inversify

    y-chen
    @y-chen

    Hi, I'm writing the tests for a class with a dependency but when I try to get the sut the TYPES constant is undefined:

    @injectable()
    export class DictionaryService implements IDictionaryService {
           @inject(TYPES.FilesService) private readonly files!: IFilesService;

    And this is how I'm trying to get the sut:

        beforeEach(() => {
            filesServiceMock = mock<IFilesService>();
            container.rebind<IFilesService>(TYPES.FilesService).toConstantValue(filesServiceMock);
    
            sut = container.get<IDictionaryService>(TYPES.DictionaryService);
        });

    When I run the code normally it works

    Narcis B.
    @LeonardSSH
    Hi!
    evoytenkoapps
    @evoytenkoapps
    image.png
    image.png
    image.png
    image.png
    Hey guys. Please help. I have a problem inside test. diContainer.get return undefiden. When run one test its ok, but when i run all test i have this problem. whats a reason? It seams that inSingletonScope crete twice. how to solve it?
    Dustin Lacewell
    @dustinlacewell
    Can you bind undefined?
    evoytenkoapps
    @evoytenkoapps
    Its seems some problem with RESTORE method. He denied all singletons dependecies
    image.png
    May i use ASYNC with middleware HANLDER? My test are broke when i add async.
    Leandro Acosta
    @leandro.acosta_gitlab

    Hi everyone, I'm learning express with typescript, inversify and typeorm, and I'm trying to implement Hexagonal architecture, separating controllers, entities, services, etc, as we do it in my work (with spring boot)

    BaseService

    decorate(injectable(), Repository);
    
    @injectable()
    class BaseService<T extends BaseEntity> {
    
      public _repository: Repository<T>;
    
      constructor(repository: Repository<T>) {
        this._repository = repository;
      }
    
      public getAll(): Promise<T[]> {
        return this._repository.find();
      }
    
    }

    UserService

    decorate(injectable(), Repository);
    
    @injectable()
    class BaseService<T extends BaseEntity> {
    
      public _repository: Repository<T>;
    
      constructor(repository: Repository<T>) {
        this._repository = repository;
      }
    
      public getAll(): Promise<T[]> {
        return this._repository.find();
      }
    
    }

    UserRepository

    @EntityRepository(User)
    @injectable()
    export default class UserRepository extends Repository<User> {
    
      findByName(firstName: string, lastName: string) {
        return this.findOne({ firstName, lastName });
      }
    
    }

    Container

    const container = new Container();
    container.bind<UserService>(TYPES.UserService).to(UserService);
    container.bind<UserRepository>(TYPES.UserRepository).to(UserRepository);
    And I'm having this error
    TypeError: Cannot read property 'find' of undefined
    image.png
    On line 22, in the inherited method
    (Sorry for my english)
    mayt-moonboy
    @mayt-moonboy
    Does anyone know how to bind container modules to other container modules?
    mayt-moonboy
    @mayt-moonboy
    @leandro.acosta_gitlab can you post the full error message and stack trace
    Kevin Mitchell
    @kevin-mitchell

    Hi all :) - thanks in advance for any help!

    I am new to Inversify, but have this use case I"m trying to figure out: I am using a somewhat clunky "old school" SDK for a product that I'd like to make a bit easier to work with in a "modern" way. The way this thing is used in documentation is basically window.theSdk = await sdkInitializer(); - then you use theSdk on the window object. :/

    I'd like to use inversify to make the singleton instance of the SDK available in any component (I'm using React, more specifically Preact), ideally without making the injected version be a promise (but rather just return the actual SDK object).

    I of course have read through the documentation and at first glance https://github.com/inversify/InversifyJS/blob/master/wiki/provider_injection.md Provider Injection seems like perhaps what I might want... but I don't really understand how this would help me vs injecting a factory https://github.com/inversify/InversifyJS/blob/master/wiki/factory_injection.md

    The provider injection also doesn't appear to be a singleton - this is going to seem really stupid (SORRY!) and possibly outside the scope of Inversify, but given my goal of allowing the users of the injector to be able to not worry about the promise, is there a better way to do this?

    Kevin Mitchell
    @kevin-mitchell

    OK, another one :) (unrelated):

    I for some reason am failing to get @inject(ExampleConverter) exampleConverter!: ExampleConverter; working. This is a typescript project.

    If I explicitly use the "container" to resolve something container.get(TYPE.whatever) this works. But the annotations don't appear to work in my project. I think I've double checked all of the configuration :/

    Here is a clearer example of what I mean:
    
    export default class OrderHandler implements Handler {
    
        //does NOT work
        @inject(TYPES.Example) exampleConverter: ExampleConverter;
    
        handle(req: RouterRequest<any>, res: RouterResponse<any>): RouterResponse<any> {            
            //works!
            let example: ExampleConverter = DIContainer.get(TYPES.Example);                
        }
    
    }
    Narcis B.
    @LeonardSSH

    Hello. Is there a library or something to integrate InversifyJS with Fastify? (like inversify-express-utils)

    hmm

    If not, I could create one, based on ideas from inversify-express-utils. ­čśŐ
    Narcis B.
    @LeonardSSH
    I created a special discord server for the community, join us too! https://discord.gg/jXcMagAPnm
    Geoffrey Huntley
    @ghuntley
    Howdy folks, Gitpod uses InversifyJS in https://gitpod.io. Would you consider setting your project up in GitHub sponsors so that we can buy you some beer/coffee/tea?
    1 reply
    Jeramy
    @xylesoft
    Hi, I have a RedisClient dynamic binding, which creates a connection as part of the container definition. Although I'm having the issue where the Node process will not shut down because the socket is still alive. The only solution i have been able to work out, is to explicitly get the binding and then trigger .quit() command. This is not particuarlly scalable and also my entry file does not require the binding. Is there a way in InversifyJS to manage the shutting down of all existing sockets?
    1 reply
    let redis = null;
    container
        .bind<RedisClient>('Redis')
        .toDynamicValue((context: interfaces.Context) => {
            console.log('Redis loaded.');
            const host = 'xxxx';
            const port = 12345;
            const redisClient = redis.createClient({ host, port });
            const logger = context.container.get<Logger>('Logger');
    
            redisClient.on('error', error => logger.error(error));
            redisClient.on('connected', () => logger.log(`Redis connected to ${host}:${port}`));
    
            return redisClient;
        })
        .inSingletonScope();
    Almog Laktivi
    @al942u
    Hi all,
    Do you guys have a way to consume dependencies on worker_threads functionality?
    Madis Abel
    @madisabel
    Hi, looks like the latest release of Inversify (5.1.0) is badly broken in NPM - it's missing the code (like lib/inversify.js) and is not usable..
    Robert Plummer
    @robertleeplummerjr
    Is there a way to reset certain properties that have been bound, to an override, but from the context of a method call?
    The use case is we are dealing with experimental AI, and want to have long lasting code, that is well tested, but also have code that tests hypotheses, that will overwrite certain classes that have been bound with inversify. The classes are selected based off of an algorithm like multi arm bandit, that persistently classify a user for a duration to an experiment of overrides. This way we can have short lived hypothesis code, and long lived codebase, that doesn't get muddied with edge case code all over the place.
    Once the user reaches a goal, we can score the classification of the user, and the more successful set of experiments will be used over time, indicating if the experiment was a success or not, against a control set.
    Robert Plummer
    @robertleeplummerjr
    The question comes to inversify, because naturally we use it to inject instances of classes, and this framework I'm speaking of would control or work with inversify. If it is possible, we'd need the solution to be performant as well.
    Robert Plummer
    @robertleeplummerjr
    After experimentation, I've arrived at the concept of injection just in time without calling a function is simply outside the scope of inversify, and that the seemingly better practice would be not to use inversify for this practice.
    Narcis B.
    @LeonardSSH
    :wave:
    We also have a discord server, if you are interested! click here
    Luiz Guilherme D'Abruzzo Pereira
    @luiz290788
    Hello everyone :wave: I've opened a inversify/InversifyJS#1345 to add a functionally that was inversify/InversifyJS#1110 by multiple people. I was wondering if one of the maintainers could have a look at my pr. Also, I'm open to be one of the maintainers of the project reviewing PRs, answering questions in issues and working on new functionality. The project I'm currently working relies heavily on inversify and we really don't want this project to die.
    Francesco Ferraioli
    @francescoferraioli

    :wave:

    Just wondering when the onDeactivation handler is planned to be release? Also, will the handler be invoked when the container is stopped?

    David Hart
    @hartbit
    Hello!
    I'm trying to depend on InversifyJS master from my project as I need some of the new features not available in 5.1.1. Is there an easy way to do it?
    icy0307
    @icy0307
    Hi all,
    Just wondering , is there way we can auto inject interface?
    'autoBindInjectable' should only work with concrete type, right?
    After all , how could we hold the reference to the interface implementation´╝č