zoukankan      html  css  js  c++  java
  • MEF简介及简单的Demo

    MEF简介及简单的Demo

    文章开始之前,首先简单介绍一下什么是MEF,MEF,全称Managed Extensibility Framework(托管可扩展框架)。单从名字我们不难发现:MEF是专门致力于解决扩展性问题的框架,MSDN中对MEF有这样一段说明:

      Managed Extensibility Framework 或 MEF 是一个用于创建可扩展的轻型应用程序的库。 应用程序开发人员可利用该库发现并使用扩展,而无需进行配置。 扩展开发人员还可以利用该库轻松地封装代码,避免生成脆弱的硬依赖项。 通过 MEF,不仅可以在应用程序内重用扩展,还可以在应用程序之间重用扩展。

      废话不多说了,想了解更多关于MEF的内容,到百度上面查吧!

      MEF的使用范围广泛,在Winform、WPF、Win32、Silverlight中都可以使用,我们就从控制台说起,看看控制台下如何实现MEF,下面先新建一个win32控制台项目MEFDemo,添加一个IBookService接口,写一个简单的Demo:

    IBookService内容如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace MEFDemo
    {
       public interface IBookService
       {
          string BookName { get; set; }
          string GetBookName();
       }
    }
    复制代码

    下面添加对System.ComponentModel.Composition命名空间的引用,由于在控制台程序中没有引用这个DLL,所以要手动添加:

    点击OK,完成添加引用,新建一个Class,如:MusicBook继承IBookService接口,代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.Composition;
    
    namespace MEFDemo
    {
       [Export(typeof(IBookService))]
       public class MusicBook : IBookService
       {
          public string BookName { get; set; }
    
          public string GetBookName()
          {
             return "MusicBook";
          }
       }
    }
    复制代码

    Export(typeof(IBookService)) 这句话将类声明导出为IBookService接口类型。

    然后修改Porgram中的代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    
    namespace MEFDemo
    {
       class Program
       {
          [Import]
          public IBookService Service { get; set; }
    
          static void Main(string[] args)
          {
             Program pro = new Program();
             pro.Compose();
             if (pro.Service != null)
             {
                Console.WriteLine(pro.Service.GetBookName());
             }
             Console.Read();
          }
    
          private void Compose()
          {
             var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
             CompositionContainer container = new CompositionContainer(catalog);
             container.ComposeParts(this);
          }
       }
    }
    复制代码

    这里使用[Import]导入刚刚导出的MusicBook,下面的Compose方法,实例化CompositionContainer来实现组合,点击F5运行,结果如下:

    可以看到调用了MusicBook类的GetBookName方法,但是我们并没有实例化MusicBook类,是不是很神奇呢???

    这一就是实现了主程序和类之间的解耦,大大提高了代码的扩展性和易维护性!

     
    分类: ASP.NET
  • 相关阅读:
    suse12安装详解
    Centos7上部署openstack mitaka配置详解(将疑难点都进行划分)
    菜鸟帮你跳过openstack配置过程中的坑[文末新添加福利]
    openstack中dashboard页面RuntimeError: Unable to create a new session key. It is likely that the cache is unavailable.
    Multiple network matches found for name 'selfservice', use an ID to be more specific.报错
    查看 SELinux状态及关闭SELinux
    SELinux深入理解
    IP地址、子网掩码、网络号、主机号、网络地址、主机地址
    Oracle job procedure 存储过程定时任务
    POI文件导出至EXCEL,并弹出下载框
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3924430.html
Copyright © 2011-2022 走看看