microsoft / microsoft/tsyringe

Could you add a little more sophisticated examples? (Also: Help me please!)

Open
#112 0 comments 5 reactions 1 assignee View on GitHub

@Xapphire13 is already working on this.

Since Jul 9, 2020.

awaiting triage enhancement
Dominant language
TypeScript
Stars
6k
Forks
184
Avg merge
3m
Merged PRs (30d)
1

Description

I am new to tsyringe (and DI) and have a really hard time to wrap my head around it. So I guess it would be of great help to have some more sophisticated examples in the readme?

E.g. my system looks like this, and I am stuck as of how to accomplish this in tsyringe:

A contains an array of B which contains C and D.

  class C {
    constructor(public parent:B){};
  }
  
  class D {
    constructor(public myC:C){};
  }
  
  class B{
    myC = new C(this);
    myD = new D(this.myC)
  }
  
  class A {
    myBs : B[]= [];
    addB() {
      this.myBs.push(new B())
    }
  }

A will need to pass some factory to A to add Bs, right?

And how do I prevent the container to instantiate a new C for D (but use the C from B)?

Same with C: how to I pass the reference to it's parent B (instead of instantiating a new B)?

Thank you for your time.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.