##// END OF EJS Templates
Working on container builder
Working on container builder

File last commit:

r5:83fa5814462d default
r5:83fa5814462d default
Show More
container.ts
69 lines | 1.6 KiB | video/mp2t | TypeScriptLexer
/* eslint max-classes-per-file: ["error", 20] */
import { describe, it } from "mocha";
import { Container } from "../Container";
import { ContainerServices } from "../interfaces";
import { fluent } from "../traits";
class Foo {
foo = "foo";
}
class Bar {
bar = "bar";
constructor(foo?: () => Foo) { }
}
interface Services {
foo: Foo;
bar?: Bar;
baz: Foo;
}
interface ServicesB {
// will give errors
// baz: Bar;
baz: Foo;
zoo?: Foo;
}
interface SharedServices {
foo: Foo;
bar?: Bar;
baz: Bar;
}
const config = fluent()
.declare<Services>()
.declare<ServicesB>()
.register({
bar: it => it
.lifetime("context") // тип активации, время жизни
.wants({
zoo: "zoo", // зависимость
zoo$: { name: "zoo", lazy: true } // отложенная активация,
//фабричный метод
})
.factory(({ zoo$ }) => // фабрика получает объект с именованными зависимостями
// удобно для деструктурирования
new Bar(zoo$) // создается экземпляр сервиса
),
foo: it => it.factory(() => new Foo()),
baz: it => it.value(new Foo())
})
.done({});
declare const container: Container<object>;
const c2 = config.configure(container);
c2.resolve("foo");
declare const m :ContainerServices<{foo: Foo}>["container"];
m.resolve("container").resolve("container");