Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/dependency-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ class InternalDependencyContainer implements DependencyContainer {

this.executePreResolutionInterceptor<T>(token, "Single");

// Using a container scoped registration within a singleton is not allowed
if(registration && registration.options.lifecycle === Lifecycle.ContainerScoped && context.isSingleton) {
throw new Error(
`Attempted to use a container scoped registration "${token.toString()}" within a singleton`
);
}

if (registration) {
const result = this.resolveRegistration(registration, context) as T;
this.executePostResolutionInterceptor(token, result, "Single");
Expand Down Expand Up @@ -313,6 +320,7 @@ class InternalDependencyContainer implements DependencyContainer {
const isContainerScoped =
registration.options.lifecycle === Lifecycle.ContainerScoped;

context.isSingleton = isSingleton;
const returnInstance = isSingleton || isContainerScoped;

let resolved: T;
Expand Down
1 change: 1 addition & 0 deletions src/resolution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import {Registration} from "./dependency-container";

export default class ResolutionContext {
scopedResolutions: Map<Registration, any> = new Map();
isSingleton: boolean = false;
}