Take the 2-minute tour ×

I have hit a wall. I have added a reference of another assembly that has Service classes. So instead of creating a new instance i though that MEF would help(but wont).. I do something similar to HelloScreens sample.The following viewmodel accepts a property too that holds the model.(Staff)

public class StaffFormViewModel : Screen
    public StaffService staffService { get; set; }

Obviously this doesnt work as the class isnt constructed by MEF. But even i wanted to construct it with mef i should create multiple instances so i should select NonShared.But people say its slow. Caliburn micro example adds something like this in order to create new instances. This requires 5 lines of code for each thing.

    () => container.GetExportedValue<PreferencesCategoriesFormViewModel>());

The problem is that although i can use this then if i want to provide a model class at constructor I cant because it is constructed by MEF. I should set the property by hand. All i wanted is to inject the needed service. Isnt this possible with mef? Should i have public static the container so i can reference it and call compose? Please help :)

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

The sample you've given is just creating a factory that returns new instances of PreferencesCategoriesViewModel. They are using the built in delegate type Func in order to save them creating a new factory interface, and an implementation of the interface which would require a reference to the container.

So if you wish to instantiate view models via MEF, then you can either:

  1. If you only need a single instance of the view model, then you could inject it via constructor injection to the parent view model, and leave MEF to instantiate it
  2. If you need control over the instantiation of the view model, or you need to instantiate multiple instances of the view model, then you could inject a view model factory type to the parent view model

If you are going to use a view model factory then you could:

  1. Use the built in Func delegate type as shown above, you can always use one of the other Func types that take one or more input parameter as your child view model requires data
  2. Create a specific factory interface and concrete implementation, and register that with the container
  3. Create a generic view model factory and inject that

For an example of option 3, see http://pglazkov.blogspot.co.uk/2011/04/mvvm-with-mef-viewmodelfactory.html.

You'll notice that his view model factory has a reference to the MEF container, which he actually resolves via MEF. Therefore your container will need to register itself.

share|improve this answer
thank you but i wanted to inject a service class within a class that isnt constructed by mef –  Parhs Nov 14 '12 at 10:21
The same principle applies, it's only your view model constructor that will change, or your factory method to take the data it requires. E.g. you could use a Func<Something, MyViewModel> or create an interface type with a method that takes your data type. The view model factory I've linked to also supports arguments. –  devdigital Nov 14 '12 at 10:47
3 seems great because cm solution was ugly to me. So factory seems tobe the solutoin 2 or 3. –  Parhs Nov 23 '12 at 18:12
add comment

Your Answer


