1、实现 MVC3 Beta 中提供的依赖注入接口 IDependencyResolver ,MyDependencyResolver.cs 的代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Microsoft.Practices.Unity;
- namespace Demo
- {
- public class MyDependencyResolver : IDependencyResolver
- {
- #region IDependencyResolver 成员
- /// <summary>
- /// 依赖注入容器
- /// </summary>
- private UnityContainer _unityContainer;
- /// <summary>
- /// 构造
- /// </summary>
- /// <param name="aUnityContainer">依赖注入容器</param>
- public MyDependencyResolver( UnityContainer aUnityContainer )
- {
- _unityContainer = aUnityContainer;
- }
- public object GetService( Type aServiceType )
- {
- try
- {
- return _unityContainer.Resolve( aServiceType );
- }
- catch
- {
- /// 按微软的要求,此方法,在没有解析到任何对象的情况下,必须返回 null,必须这么做!!!!
- return null;
- }
- }
- public IEnumerable<object> GetServices( Type aServiceType )
- {
- try
- {
- return _unityContainer.ResolveAll( aServiceType );
- }
- catch
- {
- /// 按微软的要求,此方法,在没有解析到任何对象的情况下,必须返回空集合,必须这么做!!!!
- return new List<object>( );
- }
- }
- #endregion
- }
- }
2、在 Global.asax.cs 中设置依赖注入解析器 DependencyResolver (这是一个全局静态类,也是 MVC3 Beta 新增的):
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using System.Web.Routing;
- using Microsoft.Practices.Unity;
- namespace Demo
- {
- // Note: For instructions on enabling IIS6 or IIS7 classic mode,
- // visit http://go.microsoft.com/?LinkId=9394801
- public class MvcApplication : System.Web.HttpApplication
- {
- public static void RegisterGlobalFilters( GlobalFilterCollection filters )
- {
- filters.Add( new HandleErrorAttribute( ) );
- }
- public static void RegisterRoutes( RouteCollection routes )
- {
- routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );
- routes.MapRoute(
- "Default", // Route name
- "{controller}/{action}/{id}", // URL with parameters
- new { controller = "Home", action = "Index", id = UrlParameter.Optional }
- );
- }
- protected void Application_Start( )
- {
- AreaRegistration.RegisterAllAreas( );
- RegisterGlobalFilters( GlobalFilters.Filters );
- RegisterRoutes( RouteTable.Routes );
- //设置依赖注入
- RegisterDependency( );
- }
- private static UnityContainer _Container;
- public static UnityContainer Container
- {
- get
- {
- if ( _Container == null )
- {
- _Container = new UnityContainer( );
- }
- return _Container;
- }
- }
- protected void RegisterDependency( )
- {
- Container.RegisterType<ITest, Test>( );
- DependencyResolver.SetResolver( new MyDependencyResolver( Container ) );
- }
- }
- }
3、Controller的代码,HomeController.cs:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Microsoft.Practices.Unity;
- namespace Demo.Controllers
- {
- public class HomeController : Controller
- {
- [Dependency]
- public ITest Test { get; set; }
- public ActionResult Index( )
- {
- ViewModel.Message = Test.GetString( );
- return View( );
- }
- public ActionResult About( )
- {
- return View( );
- }
- }
- }
4、ITest.cs代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Demo
- {
- public interface ITest
- {
- string GetString( );
- }
- }
5、Test.cs代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace Demo
- {
- public class Test:ITest
- {
- #region ITest 成员
- public string GetString( )
- {
- return "Run demo!";
- }
- #endregion
- }
- }
注:这篇文章只适用于 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.
文章来源:http://www.hulian.top,转载请注明!