Вы можете сделать следующее.Вы можете создать свою дочернюю модель представления, используя обычный конструктор, а затем вызвать ComposeParts
в экземпляре, чтобы внедрить все:
var patientViewModel = new PatientViewModel(patient);
_container.ComposeParts(patientViewModel);
Но делать это каждый раз не очень хорошо по разным причинам.Для решения этого сценария и для инкапсуляции использования MEF я создал вспомогательный сервис для создания моделей представлений.Она называется IViewModelFactory
, и вот как это выглядит:
[Export(typeof(IViewModelFactory))]
[PartCreationPolicy(CreationPolicy.Shared)]
internal class ViewModelFactory : IViewModelFactory
{
[ImportingConstructor]
public ViewModelFactory(CompositionContainer container) {
Contract.Requires(container != null);
Container = container;
}
protected CompositionContainer Container { get; private set; }
public T Create<T>(params object[] args) where T : class {
T result;
try {
bool populateDependencies = false;
if (args == null || args.Length == 0) {
// There are no parameters for contructor, so
// try to create an instance by asking the container.
result = Container.GetExportedValueOrDefault<T>();
if (result == null) {
// The view model is not exported. Just create an instance using reflection
// and then populate all the dependencied using the container.
result = Activator.CreateInstance<T>();
populateDependencies = true;
}
}
else {
// There are constructor parameters. Create an instance using those parameters
// and then populate all the dependencied using the container.
result = (T)Activator.CreateInstance(typeof(T), args);
populateDependencies = true;
}
// Populate dependencies if needed
if (populateDependencies) {
Container.ComposeParts(result);
}
// Initialize the object if applicable
var initializable = result as IInitializable;
if (initializable != null) {
initializable.Initialize();
}
}
catch (Exception ex) {
throw new ViewModelCreationException(
string.Format(
"Unable to create and configure an instance of view model of type {0}. An error occured. See inner exception for details.",
typeof (T)), ex);
}
return result;
}
}
Используя эту фабрику, вы можете создавать модели дочернего вида, такие как:
var patientViewModel = ViewModelFactory.Create<PatientViewModel>(patient);
Недостатком является то, что, когдавы используете параметры конструктора, вы теряете проверку во время компиляции для типов параметров, количества, порядка и т. д.