ASP.NET MVC 3让依赖注入实现得更简单

开发 后端
很多.NET程序员对ASP.NET MVC3 Beta还是挺感兴趣的,本文介绍的是通过MVC 3更简单的实现依赖注入。

昨天,我写了一篇文章(参见:ASP.NET MVC 依赖注入),这种实现方式我个人一直感觉不太顺,在写出来与大家一起分享的同时,

也是想让大家提提自己的建议, 今天下载了微软发布的***的ASP.NET MVC3 Beta版,同时也仔细阅读了它的 Release Notes,

让我感觉到惊喜的是,MVC3增加了对依赖注入的支持,增加了一个 IDependencyResolver 接口定义,真的是很不错,比起我原来的实现要顺畅很多,

还是老方法,上微软牛人们的博客逛一圈看看有没有已经写好的代码,有就拿来用之,没有就只能自己写了,结果让我很失望,也可能是我太笨,

我没有找到一个完整的示例,只有一些代码片断,于是,我将其整理了一翻,也有一点点个人的心得,拿出来,与大家分享一下,

如遇高人请不吝赐教,下面是代码片断。

1、实现 MVC3 Beta 中提供的依赖注入接口 IDependencyResolver ,MyDependencyResolver.cs 的代码: 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Microsoft.Practices.Unity;  
  7.  
  8. namespace Demo  
  9. {  
  10.     public class MyDependencyResolver : IDependencyResolver  
  11.     {  
  12.         #region IDependencyResolver 成员  
  13.  
  14.         /// <summary>  
  15.         /// 依赖注入容器  
  16.         /// </summary>  
  17.         private UnityContainer _unityContainer;  
  18.  
  19.         /// <summary>  
  20.         /// 构造  
  21.         /// </summary>  
  22.         /// <param name="aUnityContainer">依赖注入容器</param>  
  23.         public MyDependencyResolver( UnityContainer aUnityContainer )  
  24.         {  
  25.             _unityContainer = aUnityContainer;  
  26.         }  
  27.  
  28.         public object GetService( Type aServiceType )  
  29.         {  
  30.             try 
  31.             {  
  32.                 return _unityContainer.Resolve( aServiceType );  
  33.             }  
  34.             catch 
  35.             {  
  36.  /// 按微软的要求,此方法,在没有解析到任何对象的情况下,必须返回 null,必须这么做!!!!  
  37.                 return null;  
  38.             }  
  39.         }  
  40.  
  41.         public IEnumerable<object> GetServices( Type aServiceType )  
  42.         {  
  43.             try 
  44.             {  
  45.                 return _unityContainer.ResolveAll( aServiceType );  
  46.             }  
  47.             catch 
  48.             {  
  49.   /// 按微软的要求,此方法,在没有解析到任何对象的情况下,必须返回空集合,必须这么做!!!!  
  50.                 return new List<object>( );  
  51.             }  
  52.         }  
  53.  
  54.         #endregion  
  55.     }  
  56.  

2、在 Global.asax.cs 中设置依赖注入解析器  DependencyResolver (这是一个全局静态类,也是 MVC3 Beta 新增的):

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. using Microsoft.Practices.Unity;  
  8.  
  9. namespace Demo  
  10. {  
  11.     // Note: For instructions on enabling IIS6 or IIS7 classic mode,   
  12.     // visit http://go.microsoft.com/?LinkId=9394801  
  13.  
  14.     public class MvcApplication : System.Web.HttpApplication  
  15.     {  
  16.  public static void RegisterGlobalFilters( GlobalFilterCollection filters )  
  17.         {  
  18.             filters.Add( new HandleErrorAttribute( ) );  
  19.         }  
  20.  
  21.         public static void RegisterRoutes( RouteCollection routes )  
  22.         {  
  23.             routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );  
  24.  
  25.             routes.MapRoute(  
  26.                 "Default"// Route name  
  27.                 "{controller}/{action}/{id}"// URL with parameters  
  28. new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  29.             );  
  30.  
  31.         }  
  32.  
  33.         protected void Application_Start( )  
  34.         {  
  35.             AreaRegistration.RegisterAllAreas( );  
  36.  
  37.             RegisterGlobalFilters( GlobalFilters.Filters );  
  38.             RegisterRoutes( RouteTable.Routes );  
  39.             //设置依赖注入  
  40.             RegisterDependency( );  
  41.         }  
  42.  
  43.         private static UnityContainer _Container;  
  44.         public static UnityContainer Container  
  45.         {  
  46.             get 
  47.             {  
  48.                 if ( _Container == null )  
  49.                 {  
  50.                     _Container = new UnityContainer( );  
  51.                 }  
  52.                 return _Container;  
  53.             }  
  54.         }  
  55.  
  56.         protected void RegisterDependency( )  
  57.         {  
  58.             Container.RegisterType<ITest, Test>( );  
  59.  DependencyResolver.SetResolver( new MyDependencyResolver( Container ) );  
  60.         }  
  61.     }  

3、Controller的代码,HomeController.cs:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using Microsoft.Practices.Unity;  
  7.  
  8. namespace Demo.Controllers  
  9. {  
  10.  public class HomeController : Controller  
  11.     {  
  12.         [Dependency]  
  13.         public ITest Test { getset; }  
  14.           
  15.         public ActionResult Index( )  
  16.         {  
  17.    ViewModel.Message = Test.GetString( );  
  18.  
  19.             return View( );  
  20.         }  
  21.  
  22.         public ActionResult About( )  
  23.         {  
  24.             return View( );  
  25.         }  
  26.     }  

4、ITest.cs代码:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.  
  6. namespace Demo  
  7. {  
  8.     public interface ITest  
  9.     {  
  10.         string GetString( );  
  11.     }  

5、Test.cs代码: 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.  
  6. namespace Demo  
  7. {  
  8.     public class Test:ITest  
  9.     {  
  10.         #region ITest 成员  
  11.  
  12.         public string GetString( )  
  13.         {  
  14.             return "Run demo!";  
  15.         }  
  16.  
  17.         #endregion  
  18.     }  

***** 注意,这篇文章只适用于 ASP.NET MVC3 Beta 版,将来正式版出来了,未必采用这种方式来实现,毕竟对于依赖注入这块,

从 MVC1 -> MVC3 Preview1 -> MVC3 Beta 一直都在变化,微软牛人(Brad Wilson)在自己的博客中也多次提到:

Disclaimer
This blog post talks about ASP.NET MVC 3 Beta, which is a pre-release version. Specific technical details may change before the final release of MVC 3. 

This release is designed to elicit feedback on features with enough time to make meaningful changes before MVC 3 ships,

so please comment on this blog post or contact me if you have comments.

原文标题:ASP.NET MVC3 让依赖注入来的更简单(新补充了Ninject示例)

链接:http://www.cnblogs.com/cnmaxu/archive/2010/10/13/1849972.html

【编辑推荐】

  1. .NET平台下Web测试工具横向比较
  2. .Net平台下的分布式缓存设计
  3. .Net平台开源项目五年发展回顾
  4. .NET平台上Web开发的未来?
  5. .NET开发人员应该关注的七个开源项目
责任编辑:彭凡 来源: 博客园
相关推荐

2009-07-28 15:03:02

依赖性注入

2015-06-17 16:01:30

ASP.NET

2009-07-20 15:44:32

ASP.NET MVC

2009-07-31 12:43:59

ASP.NET MVC

2009-04-20 09:43:37

ASP.NET MVC基础开发

2009-07-24 13:20:44

MVC框架ASP.NET

2010-08-02 09:18:39

ASP.NET MVC

2009-07-28 14:47:18

ASP.NET MVC

2009-07-22 16:02:39

ASP.NET MVCPagedList

2010-10-12 09:52:02

ASP.NET MVC

2011-01-15 23:07:59

2009-09-09 09:09:17

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-06-01 10:23:31

asp.net mvcasp.net mvc.net mvc框架

2009-09-10 09:50:47

ASP.NET MVC

2009-07-29 11:18:21

ASP.NET连接My
点赞
收藏

51CTO技术栈公众号