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

ASP.NET MVC 3讓依賴注入實(shí)現(xiàn)得更簡單

開發(fā) 后端
很多.NET程序員對ASP.NET MVC3 Beta還是挺感興趣的,本文介紹的是通過MVC 3更簡單的實(shí)現(xiàn)依賴注入。

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

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

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

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

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

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

1、實(shí)現(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.         /// <summary>  
  15.         /// 依賴注入容器  
  16.         /// </summary>  
  17.         private UnityContainer _unityContainer;  
  18.  
  19.         /// <summary>  
  20.         /// 構(gòu)造  
  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 中設(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<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 版,將來正式版出來了,未必采用這種方式來實(shí)現(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.

原文標(biāo)題:ASP.NET MVC3 讓依賴注入來的更簡單(新補(bǔ)充了Ninject示例)

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

【編輯推薦】

  1. .NET平臺下Web測試工具橫向比較
  2. .Net平臺下的分布式緩存設(shè)計(jì)
  3. .Net平臺開源項(xiàng)目五年發(fā)展回顧
  4. .NET平臺上Web開發(fā)的未來?
  5. .NET開發(fā)人員應(yīng)該關(guān)注的七個開源項(xiàng)目
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

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基礎(chǔ)開發(fā)

2009-07-22 16:02:39

ASP.NET MVCPagedList

2009-07-28 14:47:18

ASP.NET MVC

2010-10-12 09:52:02

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-22 10:09:59

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

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
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 亚洲一区精品在线 | 婷婷五月色综合香五月 | 亚洲精品自在在线观看 | 一区二区三区小视频 | 日韩精品成人免费观看视频 | 日韩欧美在线观看 | 久久av一区二区三区 | 日韩中文欧美 | 91在线网站 | 国产精品一区二区视频 | 日本成人福利 | 国产人成在线观看 | 草草草久久久 | 免费在线黄色av | 欧美国产日韩在线观看 | 亚洲精品乱 | 一级黄色在线 | 在线一区二区三区 | 日韩精品 电影一区 亚洲 | 日韩免费毛片视频 | 久草免费在线视频 | 日本 欧美 国产 | 国产99久久久国产精品 | 一级黄色绿像片 | 91原创视频| 国产精品视频久久 | 一级黄a| 日本在线视频中文字幕 | 国产人免费人成免费视频 | 亚洲国产成人精品女人久久久 | 欧美区日韩区 | 成年人在线视频 | 国产激情免费视频 | 国产一区二区三区高清 | 黄色一级电影免费观看 | 精品96久久久久久中文字幕无 | 一区二区成人 | 91精品久久久久久久久久小网站 | 午夜性色a√在线视频观看9 | 午夜精品久久 | 午夜精品久久久久久久久久久久 |