使用Managed Ext Framework(MEF)的工厂模式 [英] Factory pattern with Managed Ext Framework (MEF)

查看:62
本文介绍了使用Managed Ext Framework(MEF)的工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MEF实现工厂模式.

I am trying to implement Factory Pattern with MEF.

这是我的解决方法

核心项目

IClass
ObjectFactory static Class(This is where the problem is)

项目A

[Export(typeof(IClass))]
[ExportMetadata("Type", "TypeA")]
public classA : IClass
{}

ProjectB

[Export(typeof(IClass))]
[ExportMetadata("Type", "TypeB")]
public classB : IClass
{}

尝试动态创建对象时遇到问题

I am facing problem when I am trying to create object dynamically

这是工厂课程:

public static class ObjectFactory
{
    private static readonly CompositionContainer _container;

    [ImportMany]
    public static IEnumerable<Lazy<IClass, IMetaData>> objectTypes;
    static ObjectFactory()
    {
        AggregateCatalog catalog = new AggregateCatalog();

        catalog.Catalogs.Add(new DirectoryCatalog(Environment.CurrentDirectory));
        _container = new CompositionContainer(catalog);

        try
        {
            objectTypes = _container.GetExports<IClass, IMetaData>();
        }
        catch (CompositionException compositionException)
        {
            Console.WriteLine(compositionException.ToString());
            Console.ReadLine();
        }
    }

    public static IClass CreateObject(ObectType objectType)
    {
        IClass outProvider;

        Type typeToLoad = objectTypes.Where(x => x.Metadata.Type == objectType.ToString()).FirstOrDefault().GetType();
        outProvider = (IClass)Activator.CreateInstance(typeToLoad);

        return outProvider;
    }
}

推荐答案

如果您希望在每次调用CreateObject时都提供一个新的"NonShared"实例,那么我建议进行这种重构.

If you want a new "NonShared" instance provided on every call to CreateObject then I suggest this refactoring.

private static readonly CompositionContainer _container;

static ObjectFactory()
{       
    var directoryCatalog = new DirectoryCatalog(Environment.CurrentDirectory)
    _container = new CompositionContainer(directoryCatalog);        
}

public static IClass CreateObject(ObectType objectType)
{       
    var objectTypes objectTypes = new List<Lazy<IClass, IMetaData>>();
    try
    {
       objectTypes.AddRange(_container.GetExports<IClass, IMetaData>());
    }
    catch (CompositionException compositionException)
    {
        Console.WriteLine(compositionException.ToString());
        Console.ReadLine();
    }

    return objectTypes.FirstOrDefault(x => x.Metadata.Type == objectType.ToString());
}

您看到MEF每次组合类型时都会解析新实例(非共享实例),或者您调用GetExports(以及此函数的所有其他重载).另外,您也可以导出IClass工厂,然后将那里有一个提供程序的集合.

You see MEF will resolve new instances (non shared ones that is) every time it composes types or you you call GetExports (and all other overload of this function). Alternatively you can export IClass factories instead, then you will have a collection of there providers.

P.S.在您的示例中,objectTypes成员上的[ImportMany]是多余的,因为您没有在编写此类型(由于它是静态的,所以我什至都不相信),您只需从GetExports的输出中以编程方式对其进行设置

P.S. [ImportMany] on objectTypes member in your example is superfluous, since you are not composing this type (I don't believe you even can since its static), you are simply setting it programmatically from the output of GetExports

这篇关于使用Managed Ext Framework(MEF)的工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆