Enterprise Library Lifetime Managers
Update: fixed a bug in the attached code.
Enterprise Library comes with several lifetime manager, which you can use to control how your objects are stored and retrieved. The available lifetime managers are:
-
ExternallyControlledLifetimeManager: stores a weak reference to objects which are created elsewhere
-
PerThreadLifetimeManager: stores objects in a thread static dictionary)
-
ContainerControlledLifetimeManager (the default): stores objects in the Unity container
-
TransientLifetimeManager: does not store objects, create a new one every time it is requested
In web applications, we may need additional options, for example:
-
Store objects in the current session
-
Store objects in the current request
-
Store objects in the current application
So, I wrote three new lifetime managers, for each of these requirements: WebSessionLifetimeManager, WebRequestLifetimeManager and WebApplicationLifetimeManager. Another possibility would be a pooled lifetime manager, which will be the subject for a future post.