前言
通过之前的文章,我们已经了解了MEF中的导入(Import)和导出(Export)。在本系列的第一篇文章中我们知道MEF其实还包括另外两个核心内容:组合容器(CompositionContainer)和目录(Catalog)。
组合容器
Castle有容器,Unity有容器,同样作为一个能够提供扩展机制,能够支持依赖注入的框架肯定也有容器。MEF的组合模型的核心是组合容器,该容器包含所有可用的部件并执行组合操作 (即,将导入和导出配对) 。通常我们使用的组合容器是:CompositionContainer ,MEF还提供一个组合对象:CompositionBatch。
目录
前面我们有谈到组合容器中包含所有可用部件,并对这些组件执行组合操作,我们可能会问容器怎么发现这些部件呢?答案就是:目录(Catalog)。
目录就是一个对象,通过它可从某些源发现可用部件。 MEF 提供了用于从提供的类型、程序集或目录发现部件的目录。 应用程序开发人员可以轻松地创建用于从其他源(如 Web 服务)发现部件的新目录。
MEF中提供的目录对象主要有:Assembly Catalog(程序集目录),Directory Catalog,Aggregate Catalog,Type Catalog,和仅使用在Silverlight中得目录Deployment Catalog( Silverlight only),Filtered Catalog.
1.Assembly Catalog
可以在给定的Assembly 发现所有的导出部件,使用类型AssemblyCatalog。
var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
2.Directory Catalog
它可以在给定的目录(路径,相对路径或绝对路径)中发现导出部件,使用类型DirectoryCatalog。如果你使用的是相对路径,则相对的是当前AppDoamin的基路径。DirectoryCatalog只会对给定目录进行一次性扫描,目录发生变化是容器不会主动刷新,如果需要刷新给定的目录需要调用方法:Refresh() ,当目录刷新时,容器也会重新组合部件。
var catalog = new DirectoryCatalog("Extensions");
catalog.Refresh();
3.Aggregate Catalog
聚集目录,有时候我们使用单一的Assembly Catalog和Directory Catalog并不能解决我们的需求,我们可能需要同时使用到他们,这时候我们便可使用Aggregate Catalog,我们可以将Assembly Catalog和Directory Catalog同时添加到目录中,这种添加可以通过构造函数实现,也可以通过目录集合的添加方法来实现:catalog.Catalogs.Add(...)。聚集目录使用类型AggregateCatalog。
var catalog = new AggregateCatalog(
new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()),
new DirectoryCatalog("Extensions"));
4.Type Catalog
通过Type Catalog我们可以发现指定类型中得导出部件。使用类型TypeCatalog
var catalog = new TypeCatalog(typeof(type1), typeof(type2), ...);
5.Deployment Catalog
Deployment Catalog,这种类型的目录仅支持Silverlight在后面的文章中我们会专门讲到如何在silverlight中使用MEF。
6.Filtered Catalog
已过滤的目录,通过FilteredCatalog可以筛选出特定的目录,特别是,您可以请求所有可加载的插件都有一个指示级别的元数据属性。
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
var parent = new CompositionContainer(catalog);
var filteredCat = new FilteredCatalog(catalog,
def => def.Metadata.ContainsKey(CompositionConstants.PartCreationPolicyMetadataName) &&
((CreationPolicy)def.Metadata[CompositionConstants.PartCreationPolicyMetadataName]) == CreationPolicy.NonShared);
var child = new CompositionContainer(filteredCat, parent);
var root = child.GetExportedObject<Root>();
child.Dispose();
示例来源于CodePlex
一个简单的例子
在介绍了这么多的基本知识后,MEF的基本概念也介绍的差不多了,这里给出一个简单完整一点的例子,供大家参考。
在应用程序中寄宿MEF:
class Program
{
//导入,使用默认的ContractType和ContractName
[Import]
public ILog log
{
get;
set;
}
static void Main(string[] args)
{
Program pro = new Program();
pro.Compose();
pro.log.AddLog(new Exception("My First MEF"));
Console.Read();
}
//组合方法
private void Compose()
{
//指定目录为当前执行的程序集
var catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
//使用AssemblyCatalog创建组合容器
var container = new CompositionContainer(catalog);
//调用组合部件方法
container.ComposeParts(this);
}
}
//定义日志接口
public interface ILog
{
void AddLog(Exception ex);
}
//导出部件,指定协定类型(ContractType)为ILog
[Export(typeof(ILog))]
public class MyLog : ILog
{
public void AddLog(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
注:本文参考文章:http://mef.codeplex.com/wikipage?title=Using%20Catalogs&referringTitle=Guide