LifeTimeManager Issue

Jun 7, 2011 at 10:17 PM
Edited Jun 7, 2011 at 10:21 PM

We are using unity in MVVM for a ViewModelLocator.

We have situations where there maybe many Views that share a single instance of a ViewModel. What I would like to be able to do is create a singleton that would last until I destroy it. Then as the one of the Views is accessed again it will create a new ViewModel through the Resolve method. We have the locator as a global StaticResource in our application. SO we dont really have direct code access to the instance of the container.

I have of course seen ExternallyControlledLifetimeManager but having trouble finding an example that uses it in the way that I would like...

So basically I need a singleton class capability but the lifetime of the ViewModel will be shorter than the container life time but longer than multiple requests to the Resolve method... Is there a way to do that?




Jun 8, 2011 at 9:35 AM


I haven't personally worked with MVVM but base upon your requirements, I can say you can leverage ExternallyControlledLifetimeManager here. Just assign a null value to the variable that's holding the ViewModel instance then explicitly call the GC.Dispose() to "destroy" it so that the next call to resolve will give you a new instance. But of course, it would be nice if you can post a sample scenario of what you want to achieve.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.