Adding more goodies in MEFedMVVM

Thanks to some good feedback from the community and also some really awesome help from Glenn Block and Sasha Barber, I added some new stuff to MEFedMVVM.

The core 3 additions are

– Reloading of design time data in Blend as you compile in Visual Studio (Patch by Chris Szabo)

      Before this patch you had to close and re open Blend everytime you did a change in your ViewModel for Blend to pick up this change. Chris Szabo sent me a code snippet showing me how I could overcome this issue. Very cool Chris!!!!!

– Added a new method in IComposer so that you can specify a list of custom ExportProviders.

      A user of Cinch suggested this feature since he had some ExportProvider he wanted to use in MEFedMVVM for versioning of objects. Now the IComposer has a new method that looks like this

image

You can return a list of ExportProviders or if you do not want to just return null. MEFedMVVM will attach its own ExportProvider even if you return null. (MEFedMVVM uses the ExportProvider for IContextAware services such as IVisualStateManager so that it can inject the View that requested the ViewModel)

– Added 2 new attached properties to make things more clear.

By default Exports in MEF are treated as Shared. This implies that if before you did an [ExportViewModel(“MyVM”] without specifying a [PartCreationPolicy(CreationPolicy.NonShared)] the ViewModel would be exported as a shared item so all imports get the same instance of the ViewModel. Also this implies that the ViewModel would never be garbage collected since MEF will keep its instance alive. The workaround for this would be to specify [PartCreationPolicy(CreationPolicy.NonShared)] where you put the ExportViewModel attribute. Once you know this its all good but if you are new to MEF maybe it is not that obvious that exports are by default Shared. So in order to make this crystal clear I added 2 new attached properties SharedViewModel and NonSharedViewModel, which will impose the CreationPolicy on the ViewModel you want to export (this is like doing the CreationPolicy on the Import if you were doing standard MEF). Glenn Block came up with this idea, kudos to Glenn!!!!

 

Besides these changes I also did some bug fixing here and there and some minor changes as per request …

– Export for ViewModel had an incorrect ImportCardinality. now the ImportCardinality is set to ExactlyOne.
– DataContextAware ViewModels used to call the DesignTimeInitialization 2 times, this is now fixed.
– Exposed the MEFedMVVM CompositionContainer, you can now access the CompositionContainer directly by doing this ViewModelRepoitory.Instance.Resolver.Container. This is useful if you want to get some Exported object from the CompositionContainer that MEFedMVVM is using.
– Fixed issue with ImportMany. This was a problem in the ExportProvider of MEFedMVVM.
– Added propertyObserver. you can see more about this here
– Fixed issue with DelegateCommand (was not hocking automatically to the CanExecute of the Command Manager in WPF)

Hope you enjoy MEFedMVVM and as always keep feedback coming !

8 thoughts on “Adding more goodies in MEFedMVVM

  1. I am trying to add one dll containing exports after my app has been running for some time. If I could access AggregateCatalog I would do something like:
    catalog.Catalogs.Add(new AssemblyCatalog(“My.dll”)).
    I know this works at startup when IComposer’s InitializeContainer is invoked. But how to do it later?

    What exactly am I trying to do here? I want to load the view that is in a separate dll which is not in the catalog when my app starts.

  2. Please ignore the post above. It is not possible to add a new assembly to the catalog after the container gets initialized with that catalog, unless it is a directory catalog.

  3. Hey Marlon,

    Please forgive this entirely superficial comment which probably misses a point somewhere.

    Regarding GetCustomExportProviders(), I wouldn’t usually recommend that an IEnumerable ever returns null – and should instead return Enumerable.Empty. Would what you’re trying to achieve here be satisfied by returning Enumerable.Empty? I’m presuming so.

    Thanks, Lee

  4. >> By default Exports in MEF are treated as Shared.

    This is a blatantly false statement. The default creation policy of exports is Any.

  5. Hi Marlon,

    how can I catch an Exception that throws by an MEFed ViewModel-ctor? Actually, in the debug output is an error message (from System.ComponentModel.Composition).

    (greets from Sacha!)

    -christoph

  6. Hi, I do believe this is a great website. I stumbledupon
    it 😉 I’m going to come back once again since i have bookmarked it. Money and freedom is the best way to change, may you be rich and continue to help other people.

Leave a comment