成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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-07-24 13:20:44

MVC框架ASP.NET

2010-08-02 09:18:39

ASP.NET MVC

2009-04-20 09:43:37

ASP.NET MVC基礎開發

2010-10-12 09:52:02

ASP.NET MVC

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-07-28 14:47:18

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2009-07-23 15:44:39

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2011-01-15 23:07:59

2009-06-01 10:23:31

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

2009-09-09 09:09:17

ASP.NET MVC

2009-07-29 11:18:21

ASP.NET連接My

2010-10-18 09:03:44

ASP.NET MVC
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美日韩精品一区 | www.99热这里只有精品 | 国产成人免费在线 | 久久精品色欧美aⅴ一区二区 | 欧美午夜视频 | 久久国产精品亚洲 | 亚洲不卡一 | 91电影在线播放 | 国产精品久久久久久久粉嫩 | 国产精品99久久久久久久久久久久 | 亚洲日韩中文字幕一区 | 成人一区二区三区在线观看 | 嫩草研究影院 | 久久久久亚洲精品 | 狠狠色网| 亚洲国产成人精品久久久国产成人一区 | 视频一区二区在线观看 | 国产精品视频免费观看 | 一级片子 | 久久中文字幕一区 | 99久久99 | 色综合久| 少妇一级淫片aaaaaaaaa | 一级看片 | 免费观看一级毛片 | 久久久久亚洲av毛片大全 | 亚洲精品国产综合区久久久久久久 | 精品福利在线 | 精品国产一级 | 国产精品久久国产精品 | 中文字幕一区二区三区四区 | 国产69精品久久99不卡免费版 | 99久久婷婷国产综合精品电影 | 粉嫩在线| 亚洲精品欧洲 | 五月综合激情在线 | 日日草夜夜草 | 中文字幕精品一区二区三区在线 | 国产亚洲久 | 久草资源| 成人在线精品视频 |