How to use MEF as a ViewModel Repository RRS feed

  • Question

  • I am just beginning to learn about MEF. I have an MVVM WPF application which has a dynamic set of ViewModels (think of customer orders.) Each CustomerOrder ViewModel is constructed from a unique ID which it uses to hydrate data for that order.

    Each CustomerOrder ViewModel has several child ViewModels constructed from the same ID, and those in turn may have their own child ViewModels. Additionally, those child ViewModels may depend on ancestor ViewModels created higher in the chain.

    For example, a CustomerOrder ViewModel has a child CustomerInformation ViewModel and CompanyInformation ViewModel. The CustomerInformation ViewModel depends on the CompanyInformation ViewModel as well.

    Currently, I am using constructor injection to manage dependencies. This is painful particularly when a child ViewModel deep in the object graph depends on an ancestor at the top (those dependencies have to get passed through each constructor.)

    MEF is interesting because it manages shared dependency injection and releasing resources, but I am not aware of a way to access a particular instance of a ViewModel by specifying a key.

    For example, from my CustomerInformation ViewModel, I'd like to access the CompanyInformation ViewModel from a container by specifying the order ID:

    public class CustomerInformation : IDisposable
        public CompanyInformation { get; set; }
        public CustomerInformation()
            container.SatisfyImportsOnce( this, OrderID );
            //release dependencies
            container.Release( this, OrderID );

    Any insight into what features of MEF I should investigate to support this type of pattern would be greatly appreciated.

    • Edited by blenddev Friday, August 31, 2012 4:31 PM
    Friday, August 31, 2012 4:26 PM