Giter Club home page Giter Club logo

diod's Introduction

Hi there πŸ‘‹

I'm Alberto Varela, a full stack developer based in Bilbao

  • πŸ’» I’m currently working at Paired as Senior Software Engineer to make couple relationships happier and healthier.
  • πŸ’Œ You can reach me via @artberri on Twitter or write me an email to [email protected]
  • πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘§ Father of two brave twins
  • ⚑ My website: albertovarela.net

diod's People

Contributors

artberri avatar benoitlahoz avatar witrin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

diod's Issues

Register same Service with different injections

Hi!

I have been looking for a way to define two implementations for the same service at the same time (For example, SendMailService) so that one of them injects the Logger implementation FakeLogger and another of them the implementation RealLogger) with the intention of being able to take one or the other depending on the needs. However, I think we cannot build the container with the same service in duplicate (The error is that the service identified as 'SendMailService' has already been registered and you would need to "unregister it before you can register it again"
The best solution I have found would be the following:

  // SendMailService.ts
  @Service()
  export class SendMailService {
   public constructor (private readonly logger: Logger) { }
  }

  // Service Register
  diodcontainer.registerFactoryAs(
    c => {
      return new SendMailService(
        env.realImplementation
          ? c.getDependency(RealLogger)
          : c.getDependency(FakeLogger),
      );
    },
    SendMailService,
    DependencyScope.Transient,
  );
  
  // SomeController.ts
  @Controller()
  export class SomeController {
  
    public async doStuffs(): Promise<void> {
      env.realImplementation = true; // or false
      const service = container.get(SendMailService);
      await service.send();
    }
  }

Do you think there could be a better solution or that the option of taking advantage of tags that allow a dual implementation to coexist could be introduced in the future?

Example:

  diodcontainer.registerFactoryAs(
    c => {
      return new SendMailService(c.getDependency(FakeLogger));
    },
    'SendMailServiceFakeLogger',
    DependencyScope.Transient,
  );

Do you think automatic registration could be implemented?

It would be great to be able to automatically register all services that have the decorator.

As far as I know, it is not possible to do this because the meta-data is not generated without importing the classes, but maybe there is a way and I don't know it.

If you know any way in which we can approach this, I would like to collaborate with it. Just give me some clue and I'll start investigating.

Thanks.

Unregister service / replace implementation

Hello and thank you very much for this library!

I'm currently working on a Vue.js plugin to integrate DIOD in Vue.

It's a work in progress and I'm learning to use DIOD, but you can find it here and a first documentation with examples there.

I was wondering if it would be possible to unregister dependencies for a 'cleaning' purpose , e.g. in case of crash or simply when quitting application (tab close, Electron quit, ...). Generally speaking I'm asking myself if the container(s) need to be teared down to be garbage collected, how it is done or how it could be done.

I guess the possibility to 'unregister' services or to replace implementations at runtime would provide a simple way to use feature toggles too...

I hope my question is clear... πŸ™ƒ

Thank you very much.

Unexpected error: TypeError: Cannot read property 'dependencies' of undefined if a direct dependency is missing other dependency

Describe the bug

When building the container if a service has a dependency registered with a missing dependency the Cannot read property 'dependencies' of undefined if a direct dependency is missing other dependency unexpected error is thrown.

To Reproduce

Run the test:

void tap.test(
  'throws error building a container with a registered service which has a dependency with unregistered dependencies',
  (t) => {
    // Arrange
    const builder = new ContainerBuilder()
    builder.register(Schedule).use(Schedule) // Schedule has Agenda as dependency
    builder.register(Agenda).use(Agenda) // Agenda has Clock as dependency but Clock is no registered

    // Assert
    t.throws(() => {
      // Act
      builder.build()
    }, new Error('Service not registered for the following dependencies of Agenda: Clock'))
    t.end()
  }
)

Expected behavior

Error Service not registered for the following dependencies of Agenda: Clock is thrown

Bug reported by @alemarcha

Register after build?

I have a feature to inject a class in container, call it A, and then use is to initiate another class, call it B. The built container doesn't let me inject class B, it has to go through builder.

// Main.ts
this.#builder.registerAndUser(ClassA);
this.#container = this.#builder.build();
// Child.ts
const instanceA = this.#container.get(ClassA);

// main module doesn't know about class B
const instanceB: ClassB = createFromA(instanceA);

// the feature that I requested.
this.#container.register(ClassB).useInstance(instanceB);
this.#container.rebuild();

I wonder if I need to rebuild or we should let user inject directly to Container?

Example outputs "Service not decorated" Error

Hi, I tried to follow an example from the documentation but got Error: Service not decorated: ServiceOne. I could not figure out the problem.

I turned on the decorators support in tsconfig.jscon:

{
  "emitDecoratorMetadata": true,
   "experimentalDecorators": true,
}
import 'reflect-metadata';
import { ContainerBuilder, Service } from 'diod';

@Service()
export class ServiceOne {
  constructor(
    private readonly dep1: ServiceTwo,
    private readonly dep2: ServiceThree
  ) {}
}

@Service()
export class ServiceTwo {
  constructor(private readonly dep1: ServiceFour) {}
}

@Service()
export class ServiceThree {}

@Service()
export class ServiceFour {}

const builder = new ContainerBuilder();

builder.registerAndUse(ServiceOne);
builder.registerAndUse(ServiceTwo);
builder.registerAndUse(ServiceThree);
builder.registerAndUse(ServiceFour);

const container = builder.build();
const serviceOne = container.get(ServiceOne);
const serviceTwo = container.get(ServiceTwo);
const serviceThree = container.get(ServiceThree);
const serviceFour = container.get(ServiceFour);

console.log({ serviceOne, serviceTwo, serviceThree, serviceFour });

The error is:

throw new Error(`Service not decorated: ${[...parents, target.name].join(" -> ")}`);

Error: Service not decorated: ServiceOne
    at getDependenciesFromDecoratedServiceOrThrow 
    ...

Update broken links from README.md

Broken Link 1

  • "create and use your own" link is not working:

1

  • The broken link is redirecting to a 404 - page not found:

2

Broken Link 2

  • "TypeScript needs types" link is not working:

3

  • The broken link is resulting in a "We can’t connect to the server at typescriptneedstypes.com." error:

4

Link to the PR

#14

Circular dependency detection expects unique class names

Given classes with the same name leads falsely to Error: Circular dependency detected: [...] during build:

To Reproduce

// module foo/bar
export class A {
}
// module foo/baz
export class A {
}
// module foo/index
export * as bar from './bar';
export * as baz from './baz';
// module qux
import foo from './foo';

export class B {

  constructor( x, y ) {
    // ...
  }
}
// module bootstrap
import '@abraham/reflection';
import { ContainerBuilder } from 'diod';
import foo from './foo';
import qux from './qux';
// [...]
const builder = new ContainerBuilder();

builder.register(foo.bar.A).useClass(foo.bar.A);
builder.register(foo.baz.A).useClass(foo.baz.A);
builder.use(qux.B).useClass(qux.B).withDependencies([foo.bar.A, foo.baz.A]);

builder.build();

Expected behavior
Duplicate class names do not lead to false circular dependency errors.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.