ExportAttribute, ImportAttribute, CompositionContainer and MEF in ASP.NET MVC 3

本文介绍托管扩展框架(MEF)的基础知识及其在ASP.NET MVC3中的具体应用。通过实例展示了如何使用ExportAttribute和ImportAttribute来标记代码,并通过CompositionContainer进行组件间的依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

本文阐述了托管扩展框架Managed Extensibility Framework在ASP.NET MVC 3的应用。 本文 不处理 MEF ASP.NET  MVC 3 系统 的复杂性

Section 1 MEF Basics

以微软观点,MEF不是控制系统的一个反演。 然而 MEF 提供 控制系统的 反演 能力
MEF中三个基本结构 exportattribute   importattribute compositioncontainer
使用ExportAttribute特性标记以下的代码:
  • class
  • field
  • property
  • indexer
  • method
使用 ImportAttribute特性 标记 以下的 代码:
  • field
  • property
  • indexer
  • argument.
例1 — 使用 ExportAttribute标记类:
[ExportAttribute]
public class A
{
  public void ShowMessage()
  {
    Console.WriteLine("this is class A");
  }
}
例2  — 使用 ImportAttribute标记属性:
public class B
{
  [ImportAttribute]
  public A PropertyA { get; set; }
}
例3 — 使用 ExportAttribute标记方法:
public class C
{
  [ExportAttribute]
  public void DoSomething()
  { 
  }
}
另一个MEF 构造是CompositionContainer 类. 把 CompositionContainer应用在 标记了 ExportAttribute 或ImportAttribute特性的代码上. CompositionContainer 类尝试把 exports 匹配 imports.
例4演示了一个完整的程序,定义标记了exportattribute的A类或importattribute的B类。 compositioncontainer接收A和B类的实例 该compositioncontainer返回一个组成部分。 我选择用控制台应用程序实现。 不像一个ASP.NET MVC 3应用程序,控制台应用程序只需要一个文件。 这允许 你容易看到 MEF 结构的 作用
namespace MefExample4
{
  using System;
  using System.ComponentModel.Composition;
  using System.ComponentModel.Composition.Hosting;


  [ExportAttribute]
  public class A
  {
    public void ShowMessage()
    {
      Console.WriteLine("this is class A");
    }
  }

  [ExportAttribute]
  public class B
  {
    [ImportAttribute]
    public A PropertyA { get; set; }
  }

  class Program
  {
    static void Main(string[] args)
    {
      // Declare a composition container.
      CompositionContainer compositionContainer = new CompositionContainer();

      // Feed the container instances of A and B.
      compositionContainer.ComposeParts(new A(), new B());

      // Retrieve the composed part.
      B b = compositionContainer.GetExportedValueOrDefault<B>();

      // Use the imported construct of B.
      b.PropertyA.ShowMessage();
    }
  }
}

Section 2- MEF in ASP.NET MVC 3

1. You mark pieces of code that MEF should take care of with ExportAttributes and ImportAttributes.

Example 8  — MessageSource类  标记  ExportAttribute
namespace MefMvc01
{
  using System.ComponentModel.Composition;

  [ExportAttribute]
  public class MessageSource
  {
    public MessageSource()
    {
      this.Message = "this message is from MessageSource";
    }

    public string Message { get; private set; }
  }
}

Example 9  —   HomeController类标记   ExportAttribute
namespace MefMvc01.Controllers
{
  using System.ComponentModel.Composition;
  using System.Web.Mvc;

  [ExportAttribute]
  public class HomeController : Controller
  {
    [ImportAttribute]
    private MessageSource messageSource;

    public ActionResult Index()
    {
      return View(this.messageSource);
    }
  }
}

2. You create an instance of a CompositionContainer. You supply it with your marked code.

Example 11  — 定义 CompositionContainer
protected void Application_Start()
{
  AreaRegistration.RegisterAllAreas();

  RegisterGlobalFilters(GlobalFilters.Filters);
  RegisterRoutes(RouteTable.Routes);

  CompositionContainer compositionContainer = new CompositionContainer();
  compositionContainer.ComposeParts(new HomeController(),
                                    new MessageSource());
}

3. You implement the IDependencyResolver interface.

在例12, 在工程增加 实现了IDependencyResolver接口 MefDependencySolver  . IDependencyResolver  接口定义了两个方法 :  GetService  and  GetServices .
namespace MefMvc01
{
  using System;
  using System.Collections.Generic;
  using System.ComponentModel.Composition;
  using System.ComponentModel.Composition.Hosting;
  using System.Web.Mvc;

  public class MefDependencySolver : IDependencyResolver
  {
    public MefDependencySolver(CompositionContainer compositionContainer)
    {
      this.compositionContainer = compositionContainer;
    }

    private CompositionContainer compositionContainer;

    public object GetService(Type serviceType)
    {
      string name = AttributedModelServices.GetContractName(serviceType);
      return compositionContainer.GetExportedValueOrDefault<object>(name);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
      return this.compositionContainer
                 .GetExportedValues<object>(serviceType.FullName);
    }
  }
}

原地址


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值