Git Product home page Git Product logo

Comments (1)

TotooriaHyperion avatar TotooriaHyperion commented on June 12, 2024

same problem here:

import { Container, inject, injectable } from "inversify";
import "reflect-metadata";

@injectable()
class ModelA_inBoth {}

@injectable()
class ModelA2_inRoot {
  @inject(ModelA_inBoth)
  a1: ModelA_inBoth;
}

@injectable()
class ModelB_inRoot {
  @inject(ModelA_inBoth)
  a1: ModelA_inBoth;
}

@injectable()
class ModelC_inChild {
  @inject(ModelA_inBoth)
  a1: ModelA_inBoth;

  @inject(ModelA2_inRoot)
  a2: ModelA2_inRoot;
}

function test1() {
  const root = new Container({ defaultScope: "Singleton" });

  const child = root.createChild();

  root.bind(ModelA_inBoth).toSelf();
  root.bind(ModelA2_inRoot).toSelf(); // A2 -> A1
  root.bind(ModelB_inRoot).toSelf(); // B -> A1

  child.bind(ModelA_inBoth).toSelf();
  child.bind(ModelC_inChild).toSelf(); // C -> A2 -> A1

  const modelB = root.get(ModelB_inRoot); // root.B -> root.A1
  const modelC = child.get(ModelC_inChild); // child.C -> root.A2 -> child.A1

  // expect: child.C -> root.A2 -> root.A1
  // actual: child.C -> root.A2 -> child.A1
  // expect to be true, actual false
  console.log(
    "modelC.a2.a1 === modelB.a1, expect true",
    modelC.a2.a1 === modelB.a1
  );
  // expect: child.C -> root.A1 === root.B -> root.A1
  // actual: child.C -> child.A1 !== root.B -> root.A1
  // expect to be true, actual false
  console.log("modelC.a1 === modelB.a1, expect true", modelC.a1 === modelB.a1);
  // expect: child.C -> child.A1 !== child.C -> root.A2 -> root.A1
  // actual: child.C -> child.A1 === child.A -> root.A2 -> child.A1
  // expect to be false, actual true
  console.log(
    "modelC.a1 === modelC.a2.a1, expect false",
    modelC.a1 === modelC.a2.a1
  );
}

test1();

The problem is, root.A2 -> child.A1, which violates the hierarchical relations between containers.

from inversifyjs.

Related Issues (20)

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.