maxlego
maxlego

Reputation: 4914

object with lifestyle PerWebRequest in Singleton

I did not find any reference in the doc for that if object with lifestyle PerWebRequest in Singleton is created in each request or not.

For me it seems that when PerWebRequest object is resolved in Singleton it will be stored locally to Singleton object and it does not act as PerWebRequest object any more.

Is it bug or a feature?

Upvotes: 2

Views: 760

Answers (2)

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27374

Most likely it is a bug. To be precise, this is a bug in the application code, not in the IoC container.

Long lived objects should not attempt to keep short lived objects as part of their state.

The solution is to either remove the dependency altogether, or remove the dependency from being part of the object's state.

Upvotes: 2

Stuart Lange
Stuart Lange

Reputation: 4089

Could you please clarify on what you mean by "object with lifestyle PerWebRequest in Singleton"? If you mean that you have an object with Singleton lifestyle that depends on an object with PerWebRequest lifestyle, then the Singleton object will have the same instance of the PerWebRequest object for the entire lifetime of the container. Castle will not "modify" the dependency "on the fly" (that wouldn't make much sense). Generally, it is not a good idea for an object to have a dependency with a "shorter" intended lifestyle (Singletons should not depend on Transients or PerWebRequests).

Upvotes: 3

Related Questions