Autofac MVVM - Lifetime
Are there any examples of using Autofac in a MVVM application? I'm not sure how one would control lifetimes and disposal of objects in a MVVM environment.
I understand I can create a lifetime and reso开发者_开发问答lve from beneath it, but that really seems more like a service locator pattern than an IoC pattern.
I don't have a public example, but I have done this in Silverlight applications.
I used the Silverlight navigation framework to organize the top level of content. When the frame navigated to a new page, I created a lifetime scope in which I resolved the page's root view model, which I associated with the page through an attribute:
[ViewModel(typeof(OrdersViewModel))]
public class OrdersView : Page
When the frame navigated to a different page, I disposed the lifetime scope before creating the next one.
The same pattern applies to opening dialogs. Each dialog gets its own lifetime scope and view model. When it closes, the lifetime scope gets disposed.
There are also situations which don't fall neatly along these boundaries. Sometimes you need more granularity and can go deeper into lifetime scopes using contextual scopes. These situations are usually one-offs and involve some glue to begin the lifetime scopes.
I find it helpful to think in terms of units of work: you commit changes at the end of each lifetime scope. This makes the extent of a set of data a natural lifetime scope.
精彩评论