日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NETMVC3讓依賴注入實現(xiàn)得更簡單

昨天,我寫了一篇文章(參見:ASP.NET MVC 依賴注入),這種實現(xiàn)方式我個人一直感覺不太順,在寫出來與大家一起分享的同時,

也是想讓大家提提自己的建議, 今天下載了微軟發(fā)布的***的ASP.NET MVC3 Beta版,同時也仔細(xì)閱讀了它的 Release Notes,

讓我感覺到驚喜的是,MVC3增加了對依賴注入的支持,增加了一個 IDependencyResolver 接口定義,真的是很不錯,比起我原來的實現(xiàn)要順暢很多,

還是老方法,上微軟牛人們的博客逛一圈看看有沒有已經(jīng)寫好的代碼,有就拿來用之,沒有就只能自己寫了,結(jié)果讓我很失望,也可能是我太笨,

我沒有找到一個完整的示例,只有一些代碼片斷,于是,我將其整理了一翻,也有一點點個人的心得,拿出來,與大家分享一下,

如遇高人請不吝賜教,下面是代碼片斷。

1、實現(xiàn) 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.         /// 構(gòu)造  
  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 中設(shè)置依賴注入解析器  DependencyResolver (這是一個全局靜態(tài)類,也是 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.             //設(shè)置依賴注入  
    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 版,將來正式版出來了,未必采用這種方式來實現(xiàn),畢竟對于依賴注入這塊,

    從 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讓依賴注入實現(xiàn)得更簡單
    標(biāo)題鏈接:http://www.5511xx.com/article/djcdheo.html