In order to create a [Container], the InjectableFunctions maintained by the PartialContainer must be memoized into Factories that can resolve their dependencies and return the correct Service.
In particular, this requires access to a "parent" Container to avoid infinite looping in cases where Service A depends on Service A – this is allowed (as long as the parent container provides Service A), but requires access to the parent Container to provide the parent implementation of Service A.
This also means that Services provided by a PartialContainer to a Container via this function will always be scoped to the Container. In other words, if a PartialContainer containing Service A is provided to both Container X and Container Y, when Service A is resolved by Container X the InjectableFunction used to create Service A will be invoked – and when Service A is resolved by Container Y, the InjectableFunction will be invoked again.
A [Container] which provides all the required Dependencies of this PartialContainer.
Create a new PartialContainer which provides a Service created by a pre-built InjectableFunction.
Tip: Prefer the inline forms provides('token', () => value) or
provides('token', ['dep'] as const, (dep) => value) instead.
An InjectableFunction, taking dependencies as arguments, which returns the Service.
Create a new PartialContainer which provides a Service created by a zero-argument factory function.
Create a new PartialContainer which provides a Service created by a factory function with dependencies. Dependencies that are not already provided by this PartialContainer will be tracked and must be fulfilled by the Container this PartialContainer is eventually provided to.
Merges services from another PartialContainer into this one. Dependencies from both containers are combined, with any dependencies satisfied by the other container's services removed.
The PartialContainer whose services will be merged.
Merges services from a Container into this PartialContainer. Since Container services are fully resolved, they add no new dependencies and may satisfy existing ones.
The Container whose services will be merged.
Create a new PartialContainer which provides the given class as a Service, all of the class's dependencies will be resolved by the parent Container.
Example:
class Foo {
static dependencies = ['bar'] as const;
constructor(public bar: string) {}
}
const partial = new PartialContainer({}).providesClass("foo", Foo);
const foo = Container.providesValue("bar", "bar value").provides(partial).get("foo");
console.log(foo.bar); // "bar value"
Create a new PartialContainer which provides the given value as a Service.
Example:
const partial = new PartialContainer({}).providesValue("value", 42);
const value = Container.provides(partial).get("value");
console.log(value); // 42
StaticfromCreates a new PartialContainer from a plain object containing service definitions. Each property of the object is registered as a value service with no dependencies.
A plain object where each property maps to a service value.
A new PartialContainer populated with the provided services and no dependencies.
Similar to [Container], with the exception that Services may be provided to a PartialContainer which does not contain all of that Services dependencies.
For this to remain safe, Services can not be resolved by PartialContainer – it has no
getmethod.Instead, the PartialContainer must be provided to a [Container] which does contain all the dependencies required by all the Service in the PartialContainer. The resulting [Container] can then resolve these Services.
PartialContainers are used to create a collection of Services which can then be provided via a simple one-line syntax to an existing Container (which fulfills the collection's dependencies). It is an organizational tool, allowing coherent groupings of Services to be defined in one place, then combined elsewhere to form a complete [Container].
Here's an example of PartialContainer usage: