Managing view lifetime with Unity container

Topics: Prism v2 - WPF 3.5
May 8, 2009 at 4:06 PM
Edited May 8, 2009 at 4:07 PM

I'm missing something fundamental about the Unity container and controlling the lifetime of my views. What I am trying to do is add views to the container and then at a particular point that makes sense in our application, go and remove a set of views from the container. After struggling a bit, I think we are to a point where we have accomplished all of this with the Unity container but the removing of the view.

View created by something like the following:

viewInstance = _container.Resolve<CustomerView>(customerViewIdentifier);

I thought container.Teardown(viewInstance); would be the right way to remove the view, but it seems there is no implementation of Teardown. Is extending this method really what I need? This seems like such a basic need that I feel like I am missing something.

My background is SCSF and it seemed so easy to add, reference, and remove views from WorkItems.

Any help or guidance would be greatly appreciated. Thanks in advance.


May 8, 2009 at 9:00 PM



There is no out-of-the-box way to remove objects from the Unity container.


Usually, in Prism-v2, views don't need to be registered in the container. Instead the Resolve<T> method is used to get each view to go through Unity’s pipeline (this also is usually with the view, presenter or view model, depending on which gets built first). By doing this, a new object of a particular type is created and goes through the Unity pipeline, so that dependency injection is performed. However this object does not remain registered in the container. You can check this out in most of the quickstarts. For example:

viewInstance = _container.Resolve<CustomerView>();


Services are normally registered in the container. This is done because they are singletons and should be accessible anywhere in the application.


Is there any benefit why you want to keep view instances registered in the container?


On the subject of WorkItem vs Unity container

You are right, there is an important difference between the container and workitems. This is because SCSF Workitems are the place where everything needed to fulfill a use case is usually placed. This includes views, commands, services, etc.

However, Workitems are specific to CAB/SCSF while Unity can be used in any application without much modifications.


In Prism there is no need to keep your views in the container, you can resolve and place them in regions when you need to.


For more information about some of the topics I mentioned above you can check:

·         Unity Application Block Methods

·         Unity Application Block 1.2 - October 2008


If you have any other doubt which is related to Unity, you could check out the Unity forums. There a better answer might be provided to you:

·         patterns & practices – Unity Forum


Please let me know if this helps.

Damian Schenkelman

May 8, 2009 at 10:10 PM

Thank you, I think clears up some of it. So, you also would not typically register models or view models in the container? It seems my SCSF background has pointed me in the wrong direction, but it is beginning to make a whole lot more sense now. Thanks again.