Caliburn.Micro + MEF + Bootstrapper + AddExportedValues delegate for all ViewModel implementations

Topics: Bootstrappers & IoC, Extensibility
Oct 18, 2012 at 11:49 PM

I've been brainstorming this one. I am using the Caliburn.Micro.HelloScreens as my starting point. In it there are 2 viewmodels (CustomerViewModel) and (OrderViewModel) implemented with corresponding IWorkspace exports for MEF.

What I am trying to do in the Bootstrapper (ScreenBootstrapper.cs) is set the

            batch.AddExportedValue<Func<CustomerViewModel>>(() => container.GetExportedValue<CustomerViewModel>());
            batch.AddExportedValue<Func<OrderViewModel>>(() => container.GetExportedValue<OrderViewModel>());

What I am trying to do in the Bootstrapper (ScreenBootstrapper.cs) is set the following instead:

            batch.AddExportedValue<Func<IModule>>(() => container.GetExportedValue<IModule>());:

But when I set the CustomerVewModel.cs and OrderViewModel.cs to export as IModule; they don't seem to load when the app runs.

I must admit I am new to MEF and Caliburn.Micro so I must be missing something.

Please note that if I create external assemblies for the 2 implementations <Customer> and <Order> and add the assemblies in my IEnumerable<Assembly> SelectAssemblies(); it works great but my main goal is not use these as external assemblies but integrated in the current project.

I'm just hoping they don't have to be spelled out batch.AddExportedValue<>() for each ViewModel used for business services.


Oct 19, 2012 at 4:58 AM

Is IModule attributed on the modules viewmodels are you trying to use?  Secondly using IModule in that fashion won't actually call them into the ViewModel as expected, you would need to [ImportMany] IEnumerable<IModule> Modules, in the constructor of the ShellViewModel to call the Modules into view.  As long as each ViewModel is decorated with IModule it will pulled into the collection.

Oct 19, 2012 at 9:03 PM

Thank you mvermef,

I later realized that the simple [ImportMany] decoration would work and cleaned up batch.AddExportedValue() in Bootstrapper. I was attempting to use the Helloscreens project as Wpf converting from Silverlight. I didn't realize many have attempted this and i used their guides to help solve the problem.

Oct 19, 2012 at 10:20 PM

glad to hear you are headed into the right direction.