The Managed Extensibility Framework (MEF) is a built-in set of elements that allows you to “export” and “import” objects across projects that allows you to not rely on hard dependencies.

From Microsoft:

The Managed Extensibility Framework or MEF is a library for creating lightweight, extensible applications. It allows application developers to discover and use extensions with no configuration required. It also lets extension developers easily encapsulate code and avoid fragile hard dependencies. MEF not only allows extensions to be reused within applications, but across applications as well.

To use MEF you need to include the following reference in your project:

System.ComponentModel.Composition

We need to define a container. This is where all the exported values will be stored. One simple way to do that is to create a class such as this one:

public static class Mef
{
private static CompositionContainer container; public static CompositionContainer Container
{
get
{
if (container == null)
{
var catalog =
new DirectoryCatalog(".", "MyProjectNamespace.*"); container = new CompositionContainer(catalog);
} return container;
}
}
}
This will grab all the exported values from all the assemblies in the same directory starting with “MyProjectNamespace”. And then I can annotate the classes I want to export as in the following:
[Export]
public class Logger
{
} [Export]
public class GameService
{
[Import]
private Logger log;
}
Whenever I need a GameService I can request it from the container as in the following:
GameService gameService = Mef.Container.GetExportedValue<GameService>();
Notice that on GameService class I have a field with an [Import] attribute. This means MEF will resolve the value for me while it is retrieving the exported value for GameService. I can also export a class identified by an interface:
[Export(typeof(IGameService))]
public class GameService
{
[Import]
private Logger log;
}
And I could use it as:
GameService gameService = Mef.Container.GetExportedValue<IGameService>();
MEF will get whatever it has in its container for IGameService. If you have more than one export for IGameService and you attempt to resolve it with GetExportedValue you will get an exception. You can have multiple exports, but the way you need to handle it is different. For example, I can have a screen with several tabs that are exported as in the following:
public interface ITab { } [Export(typeof(ITab))]
public class HomeTab : ITab { } [Export(typeof(ITab))]
public class GamesTab : ITab { } [Export(typeof(ITab))]
public class WishlistTab : ITab { }
And I can import them using the ImportMany attribute as in the following:
[Export]
public class Home
{
[ImportMany]
private List<ITab> tabs;
}
ImportMany And there is also the ImportingConstructor attribute that allows me to import objects while I am creating the instance.
[Export]
public class GameService
{
private Logger log; [ImportingConstructor]
public GameService(Logger log)
{
this.log = log;
}
} [Export]
public class Logger { }
ImportingConstructor

Using MEF to Set Up Dependency Injection的更多相关文章

  1. MVC Controller Dependency Injection for Beginners【翻译】

    在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...

  2. Ninject学习(一) - Dependency Injection By Hand

    大体上是把官网上的翻译下而已. http://www.ninject.90iogjkdcrorg/wiki.html Dependency Injection By Hand So what's Ni ...

  3. 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)

    控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...

  4. [转载][翻译] IoC 容器和 Dependency Injection 模式

    原文地址:Inversion of Control Containers and the Dependency Injection pattern 中文翻译版本是网上的PDF文档,发布在这里仅为方便查 ...

  5. Dependency Injection

    Inversion of Control - Dependency Injection - Dependency Lookup loose coupling/maintainability/ late ...

  6. Inversion of Control Containers and the Dependency Injection pattern(转)

    In the Java community there's been a rush of lightweight containers that help to assemble components ...

  7. Scala 深入浅出实战经典 第57讲:Scala中Dependency Injection实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  8. 【译】Dependency Injection with Autofac

    先说下为什么翻译这篇文章,既定的方向是架构,然后为了学习架构就去学习一些架构模式.设计思想. 突然有一天发现依赖注入这种技能.为了使得架构可测试.易维护.可扩展,需要架构设计为松耦合类型,简单的说也就 ...

  9. 依赖注入 | Dependency Injection

    原文链接: Angular Dependency Injection翻译人员: 铁锚翻译时间: 2014年02月10日说明: 译者认为,本文中所有名词性的"依赖" 都可以理解为 & ...

随机推荐

  1. neo4j 安装

    查看 http://ip:7474/browser/

  2. linux三剑客之sed深度实践

    参数: -a:追加文本到指定行后 -i:插入文本到指定行前 1.单行增加 [root@redhat~]#   sed  ' 2a  6,f '  linux.tet 1,a 2,b 6,f 3,c 4 ...

  3. JZOJ 4735. 最小圈

    Description 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点,那么一个圈的平均值为圈上k条边权的和除以k,现要求其中的最小值 Input 第一行2个正整数,分别为 ...

  4. Android系统中标准Intent的使用

    Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...

  5. win10桌面显示我的电脑

    1.桌面单击右键菜单栏,选中单击个性化 2.选择主题->桌面图标设置 3.勾选需要显示或不显示的图标

  6. HDOJ 2120 Ice_cream's world I

    Ice_cream's world I ice_cream's world is a rich country, it has many fertile lands. Today, the queen ...

  7. C#小知识点积累

    1.sealed 修饰符 概念: C#提出了一个密封类(sealed class)的概念,帮助开发人员来解决这一问题. 密封类在声明中使用sealed 修饰符,这样就可以防止该类被其它类继承.如果试图 ...

  8. 3 View - Request对象

    1.HttpReqeust对象 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象 视图函数的第一个参数是HttpRequest对象 在django.http模块中定义了Htt ...

  9. mybatis是如何防止sql注入?

    sql注入发生的时间,sql注入发生的阶段在sql预编译阶段,当编译完成的sql不会产生sql注入 采用jdbc操作数据时候 String sql = "update ft_proposal ...

  10. Android学习笔记之-----讯飞语音识别实例化RecognizerDialog参数出现错误的解决方法

    本人也是个小菜鸟,在做语音识别时也遇到了这个问题,空指针一直报错,app程序停止运行. 在网上搜了半天在这个帖子里找到了解决方法:http://bbs.xfyun.cn/forum.php?mo .. ...