Feel like a geek and get yourself Ema Personal Wiki for Android and Windows

14 January 2009

How to remove objects from the unity container

Removing objects from a Unity container can be done by using the LifetimeManager. You can even use the built-in LifetimeManager without building your own:

Dim container As New UnityContainer()
Dim lifetimeManager As New ContainerControlledLifetimeManager()
Dim someThing as New SomeThing()

container.RegisterInstance(Of SomeThing)(someThing, lifetimeManager)

Assert.AreEqual(someThing, container.Resolve(Of SomeThing)())

lifetimeManager.RemoveValue()

'we get a new instance here
Assert.AreNotEqual(someThing, container.Resolve(Of Something)())