189 8069 5689

ASP.NETMVC3让依赖注入实现得更简单

昨天,我写了一篇文章(参见: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.         ///   
  15.         /// 依赖注入容器  
  16.         ///   
  17.         private UnityContainer _unityContainer;  
  18.  
  19.         ///   
  20.         /// 构造  
  21.         ///   
  22.         /// 依赖注入容器  
  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 GetServices( Type aServiceType )  
  42.         {  
  43.             try 
  44.             {  
  45.                 return _unityContainer.ResolveAll( aServiceType );  
  46.             }  
  47.             catch 
  48.             {  
  49.   /// 按微软的要求,此方法,在没有解析到任何对象的情况下,必须返回空集合,必须这么做!!!!  
  50.                 return new List( );  
  51.             }  
  52.         }  
  53.  
  54.         #endregion  
  55.     }  
  56.  
  57. 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( );  
    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 { get; set; }  
    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.NETMVC3让依赖注入实现得更简单
    当前URL:http://cdxtjz.cn/article/djcdheo.html

    联系我们

    您好HELLO!
    感谢您来到成都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
    • 电话:028- 86922220 18980695689
    • 商务合作邮箱:631063699@qq.com
    • 合作QQ: 532337155
    • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

    小谭建站工作室

    成都小谭网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,小谭建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

    小谭观点

    相对传统的成都网站建设公司而言,小谭是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
    我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。