Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency tree #535

Closed
gagle opened this issue Apr 16, 2017 · 3 comments
Closed

Dependency tree #535

gagle opened this issue Apr 16, 2017 · 3 comments

Comments

@gagle
Copy link

gagle commented Apr 16, 2017

How can I get the dependency tree of a chain of injectables A injects B injects C?

I'm trying to implement some kind of framework on top of inversify. I would like to do the following list of things:

  1. Automatic binding of components.
  2. Asynchronous initialization.

Point 1 is resolved, so this is not a problem but with point 2 I'm having some difficulties and I don't know if I could solve it without a change in inversify. We already know the problem with issue #418.

Acomponent is a singleton class wich implements some lifecycle functions such as init() and destroy(). Concrete components of this framework will inherit from the base Component class.

export interface ComponentLifecycle {
  init: () => Promise<void>;
  destroy: () => Promise<void>;
}

By automatic binding I mean that I don't want the user to use inversify directly. For now what I'm doing is a thin wrapper arround inject() and injectable(). Each time injectable() is called I store in the metadata of a ComponentContainer class the class Component that is going to be injected later:

export function injectable() {
  return (target: any) => {
    const components: Array<Newable<Component>>
      = Reflect.getMetadata(COMPONENTS_KEY, ComponentContainer) || [];
    components.push(target);
    Reflect.defineMetadata(COMPONENTS_KEY, components, App);

    inversifyInjectable()(target);
  };
}

Then later there's some point when I read all the injectables, create the container and bind all of them in the following way:

const componentCtors = Reflect.getMetadata(COMPONENTS_KEY, ComponentContainer) || [];
const container = new Container();
for (const ctor of componentCtors) {
  container.bind(ctor).toSelf().inSingletonScope();
}

I always know the main component so I can resolve the dependencies by using resolve() or get().

To try to solve the async init I could call to the init() function manually in the same order that injectables dependencies are resolved by inversify. If A injects B and B injects C, classes are instantiated in a predictable way: C, B, A.

So I think that here I have a chance to call init() in the same order that te components are instantiated.

@gagle gagle changed the title Resolved tree Dependency tree Apr 17, 2017
@gagle
Copy link
Author

gagle commented Apr 17, 2017

I just built the tree dependency tree by reading the metadata of each class, keys inversify:tagged and inversify:tagged_props

@gagle gagle closed this as completed Apr 17, 2017
@remojansen
Copy link
Member

Hi @gagle sorry for the late reply I was on holidays... checkout the metadata middleware feature I have a feeling that maybe could be useful for your use case.

@gagle
Copy link
Author

gagle commented Apr 18, 2017

Hi @remojansen , this definitely will work to get the metadata in a consistent way. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants