Order of container.get give different inject result, is it a intended behavior? #1568
TotooriaHyperion
started this conversation in
General
Replies: 1 comment
-
// types.ts
import { interfaces } from "inversify";
export interface ITestModel {
id: number;
}
export const ITestModel: interfaces.ServiceIdentifier<ITestModel> =
Symbol("ITestModel"); // test.ts
import { Container, injectable, multiInject } from "inversify";
import { ITestModel } from "./types";
@injectable()
class Aggregate {
@multiInject(ITestModel)
tests!: ITestModel[];
}
function getContainers() {
const container = new Container({ defaultScope: "Singleton" });
container.bind(ITestModel).toConstantValue({ id: 1 });
container.bind(ITestModel).toConstantValue({ id: 2 });
container.bind(Aggregate).toSelf();
const child = container.createChild();
child.bind(ITestModel).toConstantValue({ id: 3 });
child.bind(ITestModel).toConstantValue({ id: 4 });
// child.bind(Aggregate).toSelf();
return {
container,
child,
};
}
function test1() {
const { container, child } = getContainers();
const outer = container.get(Aggregate);
const inner = child.get(Aggregate);
console.log("outer", outer.tests); // [{"id":1},{"id":2}]
console.log("inner", inner.tests); // [{"id":1},{"id":2}]
}
function test2() {
const { container, child } = getContainers();
const inner = child.get(Aggregate);
const outer = container.get(Aggregate);
console.log("outer", outer.tests); // [{"id":3},{"id":4}]
console.log("inner", inner.tests); // [{"id":3},{"id":4}]
}
function run() {
test1();
test2();
}
run(); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Beta Was this translation helpful? Give feedback.
All reactions