Asp.Net Mvc3.0(MEF依赖注入实例)

简介: 前言 在http://www.cnblogs.com/aehyok/p/3386650.html前面一节主要是对MEF进行简单的介绍。本节主要来介绍如何在Asp.Net Mvc3.0中使用MEF。 准备工作  首先建立一个Asp.Net Mvc3.0的Web项目。

前言

http://www.cnblogs.com/aehyok/p/3386650.html前面一节主要是对MEF进行简单的介绍。本节主要来介绍如何在Asp.Net Mvc3.0中使用MEF。

准备工作

 首先建立一个Asp.Net Mvc3.0的Web项目。然后建立一个业务逻辑处理的类库项目。

在MEF.Repository类库项目中建立一个ITestRepository的接口

namespace MEF.Repository
{
    public interface ITestRepository
    {
        string GetTestString();
    }
}

以及它的一个实现类

    [Export(typeof(ITestRepository))]
    public class TestRepository:ITestRepository
    {
        public string GetTestString()
        {
            return "Hello World";
        }
    }

可以看到我们已经为其标记了Export的导出属性,它的类型为上面声明的接口ITestRepository。

记得还需要引用命名空间

using System.ComponentModel.Composition;

实现MEF的注入工作

 步骤大概分为四步

第一步:发现部件---这种方式是通过读取当前程序下的dll文件中的部件

第二步:为第一步中获取到的部件创建一个CompositionContainer实例

第三步:实现IDependencyResolver接口

第四步:注册到DependencyResolver

            //设置MEF依赖注入容器
            DirectoryCatalog catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
            MefDependencySolver solver = new MefDependencySolver(catalog);
            DependencyResolver.SetResolver(solver);

这是在Global.asax文件下的Application_Start方法执行的文件,里面有一个自定义的MefDependencySolver

public class MefDependencySolver : IDependencyResolver
    {
        private readonly ComposablePartCatalog _catalog;
        private const string MefContainerKey = "MefContainerKey";

        public MefDependencySolver(ComposablePartCatalog catalog)
        {
            _catalog = catalog;
        }

        public CompositionContainer Container
        {
            get
            {
                if (!HttpContext.Current.Items.Contains(MefContainerKey))
                {
                    HttpContext.Current.Items.Add(MefContainerKey, new CompositionContainer(_catalog));
                }
                CompositionContainer container = (CompositionContainer)HttpContext.Current.Items[MefContainerKey];
                HttpContext.Current.Application["Container"] = container;
                return container;
            }
        }

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

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

里面定义创建了CompositionContainer对象实例,并继承接口IDependencyResolver实现方法

并最终完成注册到DependencyResolver。

接下来就是需要实现调用了

    [Export]
    public class HomeController : Controller
    {
        [Import]
        public ITestRepository Repository { get; set; }
        
        public ActionResult Index()
        {
            Repository.GetTestString();
            ViewBag.Message = "Welcome to ASP.NET MVC!" + Repository.GetTestString();

            return View();
        }

就是为需要的部件进行属性的标注

通过依赖注入的实现的确找到了该调用的实例。并查看最终的运行效果。

总结

 自己感觉很不错,希望能应用到实际的开发项目中。

简单的实例代码下载地址 http://pan.baidu.com/share/link?shareid=224012114&uk=4244870074

 

 

 

 

目录
相关文章
|
1月前
|
开发框架 前端开发 JavaScript
ASP.NET MVC 教程
ASP.NET 是一个使用 HTML、CSS、JavaScript 和服务器脚本创建网页和网站的开发框架。
31 7
|
1月前
|
存储 开发框架 前端开发
ASP.NET MVC 迅速集成 SignalR
ASP.NET MVC 迅速集成 SignalR
41 0
|
2月前
|
开发框架 前端开发 .NET
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
ASP.NET MVC WebApi 接口返回 JOSN 日期格式化 date format
38 0
|
2月前
|
设计模式 开发框架 .NET
分享一个 .NET Core Console 项目使用依赖注入的详细例子
分享一个 .NET Core Console 项目使用依赖注入的详细例子
|
2月前
|
开发框架 前端开发 安全
ASP.NET MVC 如何使用 Form Authentication?
ASP.NET MVC 如何使用 Form Authentication?
|
2月前
|
开发框架 .NET API
服务注册自治,降低 ASP.NET Core Web API 依赖注入的耦合度和复杂度
服务注册自治,降低 ASP.NET Core Web API 依赖注入的耦合度和复杂度
|
2月前
|
开发框架 .NET API
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
一个简单的 ASP.NET Core 依赖注入例子,提高代码的可维护性和可扩展性
|
2月前
|
开发框架 .NET
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
Asp.Net Core 使用X.PagedList.Mvc.Core分页 & 搜索
96 0
|
5月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
165 0
|
5月前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
72 0