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

Bringing toSelf() to the next level #649

Closed
osi-oswald opened this issue Oct 27, 2017 · 0 comments · Fixed by #651
Closed

Bringing toSelf() to the next level #649

osi-oswald opened this issue Oct 27, 2017 · 0 comments · Fixed by #651

Comments

@osi-oswald
Copy link
Contributor

osi-oswald commented Oct 27, 2017

I am aware of #500, #505 and inversify-binding-decorators. Nonetheless, I would love to have a container option fallbackToSelf at hand.

Motivation:

  • not having to type implicit bindings
  • same convenience as in angular: only @Injectable() and @Inject() are necessary (bindings are not mandatory)

What it does / How it behaves:

  • Whenever the container would otherwise throw "No matching bindings found..." for an @Injectable() class, it will use itself as the binding and therefore not throw at that point
  • This "fallback" behavior runs in the "planning" phase, meaning after any manual binding definitions
    • @Injectable() and container.bind will not be affected at all by this container option
    • container.get method will need changes (specifically the planning logic)
  • @provide decorator of inversify-binding-decorators does not do the job, because the container must be known at "decorating" phase (which I consider an antipattern btw)
  • If possible, fallbackToSelf will not add a binding at any point, but rather fall back to the "toSelf()" binding if no binding is defined for an @Injectable() class

Some examples:

@injectable() class A {}
@injectable() class B { constructor(a: A) {} }
@injectable() class AFake {}
class C {}

const container1 = new Container({fallbackToSelf: true});
container1.get(A); // does not throw, returns transient A
container1.get(B); // does not throw, returns transient B

const container2 = new Container({defaultScope: 'Singleton', fallbackToSelf: true});
container2.get(A); // does not throw, returns singleton A
container2.get(B); // does not throw, returns singleton B

const container3 = new Container({fallbackToSelf: true});
container3.bind(A).toSelf().inSingletonScope();
container3.get(A); // returns singleton A because of explicit binding
container3.get(B); // does not throw, returns transient B with singleton A

const container4 = new Container({fallbackToSelf: true});
container4.bind(A).to(AFake);
container4.get(A); // returns transient AFake because of explicit binding
container4.get(B); // does not throw, returns transient B with transient AFake

const container5 = new Container({fallbackToSelf: true});
container5.get(C): // still throws -> No matching bindings found

I tried to do this somehow with a middleware, but I think it is not possible.

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

Successfully merging a pull request may close this issue.

1 participant