简体   繁体   English

如何使用MEF实现此ViewModel工厂?

[英]How to implement this viewmodel factory using MEF?

I've copy a code from PRISM examples called MVVM RI, within this factory has a Dictionary<Type, Func<Question, QuestionViewModel>> for mapping. 我从PRISM示例中复制了一个称为MVVM RI的代码,该工厂内有一个Dictionary<Type, Func<Question, QuestionViewModel>>用于映射。 Here it is: 这里是:

    /// <summary>
    /// Factory class to create a question view model for a given question object.
    /// </summary>
    private static class QuestionViewModelFactory
    {
        private static Dictionary<Type, Func<Question, QuestionViewModel>> maps = new Dictionary<Type, Func<Question, QuestionViewModel>>()
        {
            { typeof(OpenQuestion), (q) => new OpenQuestionViewModel((OpenQuestion)q) },
            { typeof(MultipleSelectionQuestion), (q) => new MultipleSelectionQuestionViewModel((MultipleSelectionQuestion)q) },
            { typeof(NumericQuestion), (q) => new NumericQuestionViewModel((NumericQuestion)q) }
        };

        public static QuestionViewModel GetViewModelForQuestion(Question question)
        {
            Func<Question, QuestionViewModel> viewModelInstanceFactory = null;
            if (maps.TryGetValue(question.GetType(), out viewModelInstanceFactory))
            {
                return viewModelInstanceFactory(question);
            }
            else
            {
                throw new ArgumentOutOfRangeException("Could not locate a view model for question type");
            }
        }
    }

Note that each class derived QuestionViewModel needs a constructor parameter to be created. 请注意,每个派生的QuestionViewModel类都需要创建一个构造函数参数。

public abstract class QuestionViewModel : NotificationObject
{
    protected QuestionViewModel() { ... }
}

public abstract class QuestionViewModel<T> : QuestionViewModel
    where T : Question
{
    protected QuestionViewModel(T question) { ... }
}

But now, I need to have this factory through discovery (because I don't have a reference, these are in different modules). 但是现在,我需要通过发现来拥有这个工厂(因为我没有参考,所以它们在不同的模块中)。 I was thinking something like create a new custom attribute at the beggining, but then I said: how can I pass the parameter into the constructor? 我当时在想像在开始时创建一个新的自定义属性,但是然后我说:如何将参数传递给构造函数?

I'm going to create hundreds of questions of a same data type, that's the reason why [Import] won't work (I guess). 我将创建数百个相同数据类型的问题,这就是[Import]不起作用的原因(我想)。

You could look into using ExportFactory<T> . 您可以考虑使用ExportFactory<T> This is included in Silverlight and for WPF you can download a port of the Silverlight code from Glenn Block's Skydrive . 它包含在Silverlight中,对于WPF,您可以从Glenn Block的Skydrive中下载Silverlight代码的端口。

With an ExportFactory you can create as many objects for a given imported type as you like. 使用ExportFactory您可以根据需要为给定的导入类型创建尽可能多的对象。

[ImportMany]
public IEnumerable<ExportFactory<IModule, IModuleMetadata>> Modules { get; set; }

Then you can call the CreateExport() method of ExportFactory to export a fresh object. 然后,您可以调用ExportFactoryCreateExport()方法来导出新对象。 I recently learned about using ExportFactory so this post might help you get started. 我最近了解了有关使用ExportFactory因此本文可能会帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM