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. window.onload和$(docunment).ready的区别

    浏览器加载完DOM后,会通过javascript为DOM元素添加事件,在javascript中,通常使用window.onload()方法. 在jquery中,则使用$(document).ready ...

  2. jsp--提交表单→插入数据库→成功后返回提示信息

    <%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="u ...

  3. Net core 轮子

    .net core 使用的人渐渐多了起来,轮子也渐渐多了起来,为了避免重复造轮子,以下列举了一些造好的轮子 1. IP 请求频率限制 git: https://github.com/stefanpro ...

  4. MongDB之各种查询操作

    接口IMongDaoFind: package com.net.test.mongdb.dao; public interface IMongDaoFind { public void findUse ...

  5. scrapy框架简介和基础使用

    概念 为了爬取网站数据而编写的一款应用框架,出名,强大.所谓的框架其实就是一个集成了相应的功能且具有很强通用性的项目模板.(高性能的异步下载,解析,持久化……) 安装 linux mac os:pip ...

  6. 如何将emoji表情存放到mysql数据库中

    昨晚在爬取猫眼电影评论时在将评论信息插入到数据库中时出现问题,总是在插入一条数据时就会报错: 看着应该时字符编码的问题,比如新建的数据库新建的表,默认字符编码是:Latin1, 这种编码是无法插入中文 ...

  7. DFS:BZOJ1085-骑士精神

    题目: 1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1461  Solved: 796 [Submit][ ...

  8. 【高精度】模板 (C++)

    //n为长度 1.高精加 复杂度:O(n) #include<iostream> #include<cstring> #include<algorithm> usi ...

  9. 访问tomcat出现HTTP Status 500 - java.lang.IllegalStateException: No output folder

    问题:tomcat分为安装版和解压缩版,解压缩版如果解压到安装盘,在浏览器中访问http://localhost:8080,可能会出现500错误,错误提示如下:  localhost:8080 jav ...

  10. 6 json和ajax传递api数据

    1 2 3 4 https://swapi.co/ <h1>Hello Reqwest!</h1> <script> var a = {} reqwest({ ur ...