【问题标题】:Use Unity's Resolve for a with MEF filled Lazy List使用 Unity 的 Resolve 填充 MEF 的惰性列表
【发布时间】:2014-08-24 09:17:39
【问题描述】:

我真的喜欢 MEF 用于导出和导入,但 对于 DI 我更喜欢 Unity。这就是我目前在尝试使用 Unity 解决惰性列表时遇到的问题。

我导入 ViewModelBases 列表:

[ImportMany]
public IEnumerable<Lazy<ViewModelBase, ViewModelMetadata>> ViewModelsLazy { get; set;}

这很好用,但是当对象被初始化时,我想用 Unity 解析它们的构造函数参数:

ViewModelsLazy.Single(v => v.Metadata.Name.Equals(viewModel)).Value

类似:

unityContainer.Resolve<ViewModelsLazy.Single(v => v.Metadata.Name.Equals(viewModel)).Value>();

会很好。

我知道 MEF 提供带有 [ImportingConstructor] 的构造函数注入,但如前所述,我更喜欢 Unity 用于 DI,并且通常仅将 MEF 用于导出/导入。

【问题讨论】:

    标签: c# unity-container mef windows-phone-8.1


    【解决方案1】:

    您始终可以阅读目录以获取导出为给定类型的类型。

    此代码不会从 [ImportMany] 中读取,而是您可以分配 IEnumerable,而不是类上的 ComposeParts。

    此外,MEF 元数据是回避类型的,使用此元数据实际上需要实现接口。看起来你有一个名为 ViewModelMetadata 的类,它需要是一个接口。

    public static class MEFExtensions
    {
        public static IEnumerable<Lazy<T, M>> GetExportsResolved<T, M>(this CompositionContainer mefContainer,
                                                                       IUnityContainer unityContainer) 
            where T: class where M: class
        {
            // wrap the resolve around unity resolve then change type to T
            return mefContainer.GetExportTypesWithMetadata<T, M>()
                               .Select(kv => new Lazy<T, M>(() => unityContainer.Resolve(kv.Key) as T, kv.Value));
        }
    
        public static IEnumerable<KeyValuePair<Type, M>> GetExportTypesWithMetadata<T, M>(
            this CompositionContainer mefcontainer)
            where T : class where M : class
        {
    
            // need to examine each type to see if they have the correct export attribute and metadata
            foreach (var type in mefcontainer.GetExportTypes<T>())
            {
                // should just be one if more than one will throw exception
                // metadata or export attribute has to implement the interface
                var metadataAttribute =
                    type.GetCustomAttributes()
                        .SelectMany(
                            a =>
                            a.GetType()
                             .GetCustomAttributes()
                             .OfType<MetadataAttributeAttribute>()
                             .Concat<Attribute>(new[] { a }.OfType<ExportAttribute>()))
                        .OfType<M>().SingleOrDefault();
    
                // if we found the correct metadata
                if (metadataAttribute != null)
                {
                    // return the lazy factory
                    yield return new KeyValuePair<Type, M>(type, metadataAttribute);
                }
            }
        }
    
        //Idea from http://www.codewrecks.com/blog/index.php/2012/05/08/getting-the-list-of-type-associated-to-a-given-export-in-mef/
        public static IEnumerable<Type> GetExportTypes<T>(this CompositionContainer mefContainer)
            where T : class
        {
            // look in the mef catalog to grab out all the types that are of type T
            return mefContainer.Catalog.Parts.Where(part => part.ExportDefinitions
                                                                .Any(
                                                                    def =>
                                                                    def.Metadata.ContainsKey(
                                                                        "ExportTypeIdentity") &&
                                                                    def.Metadata["ExportTypeIdentity"]
                                                                        .Equals(
                                                                            typeof (T).FullName)))
                               .AsEnumerable()
                               .Select(part => ReflectionModelServices.GetPartType(part).Value);
        }
    }
    

    您似乎也在尝试将其用作统一容器,最好只使用单元容器。我们目前使用 MEF 来查找所有注册 - 这些是导出属性,其中包含应如何注册的元数据。然后执行与上面类似的操作来填充统一容器而不是属性。

    你会像这样使用它

    ViewModelsLazy = mefContainer.GetExportsResolved<ViewModelBase, IViewModelMetadata>(unityContainer);
    

    再次使用统一容器并执行类似的操作可能会更好

    var regs = mefContainer.GetExportTypesWithMetadata<ViewModelBase, IViewModelMetadata>();
    foreach (var reg in regs)
    {
        unityContainer.RegisterType(typeof (ViewModelBase), reg.Key, reg.Value.Name);
    }
    

    当你想要一个类解析时,你会去统一并做一个解析,将名称作为合同名称传递。

    【讨论】:

      猜你喜欢
      • 2010-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-04
      • 2018-05-06
      • 1970-01-01
      相关资源
      最近更新 更多