MEF应用(4) 组合容器 目录

组合容器

  Castle有容器,Unity有容器,同样作为一个能够提供扩展机制,能够支持依赖注入的框架肯定也有容器。MEF的组合模型的核心是组合容器,该容器包含所有可用的部件并执行组合操作(即,将导入和导出配对) 。通常我们使用的组合容器是:CompositionContainer ,MEF还提供一个组合对象:CompositionBatch。

 

目录

  前面我们有谈到组合容器中包含所有可用部件,并对这些组件执行组合操作,我们可能会问容器怎么发现这些部件呢?答案就是:目录(Catalog)。

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();


 

View Code 
 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());
         }
     }


 

 

we create a catalog -- that tells MEF where to look for imports and exports

we create a Composition container-- this is effectively the soup that all the different parts will be wired up together.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值