ExportAttribute, ImportAttribute, CompositionContainer and MEF in ASP.NET MVC 3
来源:互联网 发布:淘宝助理mac版本 编辑:程序博客网 时间:2024/05/20 12:46
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
- field
- property
- indexer
- argument.
[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.
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); } }}
原地址
0 0
- ExportAttribute, ImportAttribute, CompositionContainer and MEF in ASP.NET MVC 3
- 【ASP.NET】 ASP.NET MVC 3 & MEF 2.0
- 如何用MEF实现Asp.Net MVC框架
- ASP.NET WebForms and MVC together in one project
- Authorize attribute and jquery AJAX in asp.net MVC
- ASP.NET MVC 4 - Layout and Section in Razor
- Upload files in ASP.NET MVC with JavaScript and C#
- ASP.NET MVC - Redirect-and-Post-JSON-Object-in-ASP-NET-MVC
- Creating a Cascading Dropdown in ASP.net MVC 3 and jQuery (1)
- 创建联动的 DropdownList in ASP.net MVC 3 and jQuery (2)
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- ASP.NET WebForm and ASP.NET MVC
- AOP in Asp.net MVC
- AOP in Asp.net MVC
- CustomValidation in ASP.NET MVC
- Caching in ASP.NET MVC
- MVC multi-tenancy in ASP.NET MVC
- Code-First Migration and Extending Identity Accounts in ASP.NET MVC 5 and Visual Studio 2013
- 第七周实验室工作报告
- 汇编的跳转
- C#基础---委托的使用
- 用css实现GridView的固定表头和列
- 【转载】Java 反射机制浅析
- ExportAttribute, ImportAttribute, CompositionContainer and MEF in ASP.NET MVC 3
- 1004. Counting Leaves (30)
- PHP + Socket 发送http请求进而实现网站灌水
- 详解CSS的z-index属性(带图片解析)
- 通达OA PHP开发环境设置系统输出调试信息
- mysql创建表和删除表
- Java数据结构约瑟夫问题和腾讯面试题快慢指针找出中间节点
- C#设计模式之状态者模式(State Pattern)
- 数据结构与算法分析(JAVA版)Chapter2练习题