MEF提供的基于特性的编程模型,可以动态的根据目录找出程序集里面的所有程序部件。 对于MEF的目录服务MEF分别为WPF和Silverlight提供了不同的目录机制。使用目录的主要功能就是方便实现程序部件的装载,以及动态的组合应用程序部件等功能,更可以非常方便的得到程序部件的程序集、导出部件等相关数据。
如下代码块演示了如何在Silverlight中获取到当前应用程序的目录信息,包括了程序集和程序部件等。
var catalog
=
new
AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
var assembly = catalog.Assembly;
var parts = catalog.Parts;
var assembly = catalog.Assembly;
var parts = catalog.Parts;
在WPF中可以使用DirectoryCatalog装配基于目录的程序部件,Silverlight中则使用DeploymentCatalog来实现目录的管理,包括程序集下载、装配等。下面以MEF的目录在Silverlight中的具体应用为例介绍目录比较通用的用法,既动态下载与装配。为了提供系统的灵活性,可以通过抽象接口以高层次的抽象而存在,那么可以通过如下接口来封装目录(DeploymentCatalog)。
public
interface
IDeploymentService
{
void AddXap( string relativeUri, Action < AsyncCompletedEventArgs > completedAction);
}
{
void AddXap( string relativeUri, Action < AsyncCompletedEventArgs > completedAction);
}
在具体实现中就可以对DeploymentCatalog进行深度封装,以达到灵活应用的目的。并且可以将具体的实现通过[Export]标记为可装配的程序部件,在需要的地方就可以通过接口式的导入[Import]使用了。
[Export(
typeof
(IDeploymentService))]
public class DeploymentCatalogService : IDeploymentService
{
private static AggregateCatalog _aggregateCatalog;
Dictionary < string , DeploymentCatalog > _catalogs;
public DeploymentCatalogService()
{
_catalogs = new Dictionary < string , DeploymentCatalog > ();
}
public static void Initialize()
{
_aggregateCatalog = new AggregateCatalog();
_aggregateCatalog.Catalogs.Add( new DeploymentCatalog());
CompositionHost.Initialize(_aggregateCatalog);
}
public void AddXap( string relativeUri, Action < AsyncCompletedEventArgs > completedAction)
{
DeploymentCatalog catalog;
if ( ! _catalogs.TryGetValue(relativeUri, out catalog))
{
catalog = new DeploymentCatalog(relativeUri);
if (completedAction != null )
catalog.DownloadCompleted += (s, e) => completedAction(e);
else
catalog.DownloadCompleted += DownloadCompleted;
catalog.DownloadAsync();
_catalogs[relativeUri] = catalog;
_aggregateCatalog.Catalogs.Add(catalog);
}
}
void DownloadCompleted( object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null )
{
throw new InvalidOperationException(e.Error.Message, e.Error);
}
}
}
public class DeploymentCatalogService : IDeploymentService
{
private static AggregateCatalog _aggregateCatalog;
Dictionary < string , DeploymentCatalog > _catalogs;
public DeploymentCatalogService()
{
_catalogs = new Dictionary < string , DeploymentCatalog > ();
}
public static void Initialize()
{
_aggregateCatalog = new AggregateCatalog();
_aggregateCatalog.Catalogs.Add( new DeploymentCatalog());
CompositionHost.Initialize(_aggregateCatalog);
}
public void AddXap( string relativeUri, Action < AsyncCompletedEventArgs > completedAction)
{
DeploymentCatalog catalog;
if ( ! _catalogs.TryGetValue(relativeUri, out catalog))
{
catalog = new DeploymentCatalog(relativeUri);
if (completedAction != null )
catalog.DownloadCompleted += (s, e) => completedAction(e);
else
catalog.DownloadCompleted += DownloadCompleted;
catalog.DownloadAsync();
_catalogs[relativeUri] = catalog;
_aggregateCatalog.Catalogs.Add(catalog);
}
}
void DownloadCompleted( object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null )
{
throw new InvalidOperationException(e.Error.Message, e.Error);
}
}
}
有了上面的封装,下面只需要一句代码就可以完成独立程序包(.xap)的下载和动态装配,下面为调用封装的目录服务接口示例:
private
void
button1_Click(
object
sender, System.Windows.RoutedEventArgs e)
{
this .Service.AddXap( " MEFTraining.MefCatalogs.Parts.xap " , null );
}
{
this .Service.AddXap( " MEFTraining.MefCatalogs.Parts.xap " , null );
}
对于具体如何使用和集中控制,可以通过契约接口和元数据等策略来实现,详细在后面的文章里介绍,本篇就介绍到此。
本文转自 beniao 51CTO博客,原文链接:http://blog.51cto.com/beniao/354672,如需转载请自行联系原作者