Open veeramarni opened 7 months ago
This might be my workaround
const TYPES = {
RequestScopedDependencyFactory: Symbol.for('RequestScopedDependencyFactory'),
MyRequestScopedDependency: Symbol.for('MyRequestScopedDependency'),
MySingletonService: Symbol.for('MySingletonService'),
};
@injectable()
class MyRequestScopedDependency {
public value: number;
constructor() {
this.value = Math.random();
}
}
@injectable()
class MySingletonService {
private getRequestScopedDependency: () => MyRequestScopedDependency;
constructor(
@inject(TYPES.RequestScopedDependencyFactory)
getRequestScopedDependencyFactory: () => MyRequestScopedDependency,
) {
this.getRequestScopedDependency = getRequestScopedDependencyFactory;
}
getRequestScopedValue() {
return this.getRequestScopedDependency().value;
}
}
const container = new Container();
container
.bind<() => MyRequestScopedDependency>(TYPES.RequestScopedDependencyFactory)
.toFactory<MyRequestScopedDependency>(
(context: interfaces.Context) => () =>
context.container.get<MyRequestScopedDependency>(TYPES.MyRequestScopedDependency),
);
container
.bind<MyRequestScopedDependency>(TYPES.MyRequestScopedDependency)
.to(MyRequestScopedDependency)
.inRequestScope();
container.bind<MySingletonService>(TYPES.MySingletonService).to(MySingletonService).inSingletonScope();
request-scoped dependency is not working in a singleton class.
Expected Behavior
Writing a test case to validate the behavior of a singleton class with a request-scoped dependency involves simulating different request contexts and verifying that the singleton class behaves according to the request-scoped dependency. Here's a basic example using a hypothetical Node.js environment with Inversify for dependency injection and a testing framework like Jest.
Current Behavior
Request scope dependency is not refreshing with new value.
Steps to Reproduce (for bugs)
Singleton Service: MySingletonService is a singleton and should be the same instance in both contexts. Request-Scoped Dependency: MyRequestScopedDependency is request-scoped, so it should have different values in different contexts.
Your Environment