Reputation: 3641
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
{
[Import]
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.
batch.AddExportedValue<Func<PreferencesCategoriesFormViewModel>>(
() => 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 :)
Upvotes: 0
Views: 676
Reputation: 34349
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:
If you are going to use a view model factory then you could:
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 dataFor 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.
Upvotes: 2