ExportAttribute, ImportAttribute, CompositionContainer and MEF in ASP.NET MVC 3
2014-11-02 22:04
609 查看
Introduction
本文阐述了托管扩展框架(ManagedExtensibility 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 co
bbbb
ntainer 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接口定义了两个方法
:
GetServiceand
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); } } }
原地址
相关文章推荐
- Authorize attribute and jquery AJAX in asp.net MVC
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- ASP.NET MVC - Redirect-and-Post-JSON-Object-in-ASP-NET-MVC
- Plug-In Hybrids: ASP.NET WebForms and ASP.MVC and ASP.NET Dynamic Data Side By Side
- Displaying a Sorted, Paged, and Filtered Grid of Data in ASP.NET MVC
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- (全英文)How to install and run a simple Asp.Net 5 Application in a Docker Container
- [转]WCF RESTful service and WebGrid in ASP.NET MVC 5
- ASP.NET MVC 4 - Layout and Section in Razor
- http://venkatbaggu.com/file-upload-in-asp-net-mvc-using-dropzone-js-and-html5/
- Urls, Routing and Area in Asp.net MVC
- ASP.NET MVC and the Managed Extensibility Framework (MEF)
- [转]Paging, Searching and Sorting in ASP.Net MVC 5
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- Code-First Migration and Extending Identity Accounts in ASP.NET MVC 5 and Visual Studio 2013
- Active Directory Authentication in ASP.NET MVC 5 with Forms Authentication and Group-Based Authorization
- Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)
- Redirect and Post JSON object in ASP.NET MVC
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- ASP.NET WebForms and MVC together in one project