偶得--Unity在asp.net mvc上的基本应用
最近在研究Oxite,發(fā)現(xiàn)在這個程序中,大量采用了Unity這個微軟的IOC框架。過去我對于IOC的了解,還只停留在“城堡”階段。所以最近對Unity這個框架進行了下小小的補課,現(xiàn)在就將一個簡單的應(yīng)用拿出來跟大家分享。
首先,我有個主頁HOME
?
public class HomeController : Controller
在它下面有個Index,在這個頁面上有個主頁信息,需要從數(shù)據(jù)庫上獲取,我們首先先創(chuàng)建一個獲取這個信息的接口
??? public interface IGetHomeInfo
??? {
??????? string GetHomeTitle();
??? }
然后,實現(xiàn)它的一種方式
?
??? public class GetHome : IGetHomeInfo
??? {
??????? #region IGetHomeInfo 成員
??????? public string GetHomeTitle()
??????? {
??????????? DataService ds = new DataService();
??????????? return ds.GetHomeTitle();
??????? }
??????? #endregion
??? }
然后,我們先在index頁面的Controller中,加入這個接口
?
??? public class HomeController : Controller
??? {
??????? [Dependency]
??????? public IGetHomeInfo getHomeTitle { get; set; }
?
??????? [LoggerFilter()]
??????? [ExceptionFilter()]
??????? public ActionResult Index(int? id)
??????? {
??????????? DataService ds = new DataService();
??????????? ViewData["HomeTitle"] = getHomeTitle.GetHomeTitle();
??????????? ViewData["Id"] = id.ToString();
??????????? return View();
??????? }
好了,現(xiàn)在我們在Index(int? id)中放入了一個IGetHomeInfo 類型的注入接口,下面我們就把GetHome 注入到這個位置
??? public interface IContainerAccessor
??? {
??????? IUnityContainer Container { get; }
??? }
?
?
public class MvcApplication : System.Web.HttpApplication, IContainerAccessor
??? {
??????? private static UnityContainer _container;
??????? public static IUnityContainer Container
??????? {
??????????? get { return _container; }
??????? }
?
??????? IUnityContainer IContainerAccessor.Container
??????? {
??????????? get { return Container; }
??????? }
??????? public static void RegisterRoutes(RouteCollection routes)
??????? {
??????????? routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
??????????? routes.MapRoute(
??????????????? "Default",????????????????????????????????????????????? // Route name
??????????????? "{controller}/{action}/{id}",?????????????????????????? // URL with parameters
??????????????? new { controller = "Test", action = "Index", id = "" }? // Parameter defaults
??????????? );
????????
??????? }
??????? protected void Application_Start()
??????? {
??????????? AreaRegistration.RegisterAllAreas();
??????????? RegisterRoutes(RouteTable.Routes);
??????????? if (_container == null)
??????????????? _container = new UnityContainer();
??????????? IControllerFactory controllerFactory =
??????????????? new UnityControllerFactory(_container);
??????????? ControllerBuilder.Current.SetControllerFactory(controllerFactory);
??????????? _container.RegisterType<IGetHomeInfo, GetHome>????//在這里完成了具體的接口-類型綁定
??????????????? (new ContainerControlledLifetimeManager());
_container.RegisterType<IActionInvoker, BYSJControllerActionInvoker>();
??????? }
??? }
最后,我們還需要完成UnityControllerFactory它繼承自DefaultControllerFactory,代碼如下:
?
public class UnityControllerFactory : DefaultControllerFactory
??? {
??????? IUnityContainer _container;
??????? public UnityControllerFactory(IUnityContainer container)
??????? {
??????????? _container = container;
??????? }
??????? protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext,Type controllerType)
??????? {
??????????? if (controllerType == null)
??????????????? throw new ArgumentNullException("controllerType");
??????????? if (!typeof(IController).IsAssignableFrom(controllerType))
??????????????? throw new ArgumentException(string.Format(
??????????????????? "Type requested is not a controller: {0}", controllerType.Name),
??????????????????? "controllerType");
??????????? IController ic? = _container.Resolve(controllerType) as IController;
??????????? if (typeof(Controller).IsAssignableFrom(controllerType))
??????????? {
??????????????? Controller controller = ic as Controller;
??????????????? if (controller != null)
??????????????????? controller.ActionInvoker = _container.Resolve<IActionInvoker>();
??????????????? return ic;
??????????? }
??????????? return ic;
??????? }
??? }
?
這樣,一個簡單的IOC注入就完成了,至于程序中的IActionInvoker是怎么回事,我們留到下回再說
?
轉(zhuǎn)載于:https://www.cnblogs.com/yriyr/archive/2010/01/31/1660638.html
總結(jié)
以上是生活随笔為你收集整理的偶得--Unity在asp.net mvc上的基本应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: you think you know j
- 下一篇: NND年年回家这么难买火车票