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让依赖注入实现得更简单
    标题路径:http://www.shufengxianlan.com/qtweb/news27/436327.html

    网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

    广告

    声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联