MVC IOC 依赖注入


需要引用如下:
IDependencyResolver->System.Web.Mvc.dll
IUnityContainer->Microsoft.Practices.Unity.dll

Global.asax 追加如下方法


protected void
Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); Initialise(); } public void Initialise() { IUnityContainer container = BuildUnityContainer(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } private IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType<IOrderBiz, BookingGroupOrderBiz>("BookingGroupOrder"); container.RegisterType<IOrderBiz, GroupOrderBiz>("GroupOrder"); container.RegisterType<IPageDataBiz,GroupOrderPageDataBiz>(); return container; }

 

public class UnityDependencyResolver : IDependencyResolver
    {
        private const string HttpContextKey = "perRequestContainer";

        private readonly IUnityContainer _container;

        public UnityDependencyResolver(IUnityContainer container)
        {
            _container = container;
        }

        public object GetService(Type serviceType)
        {
            if (typeof(IController).IsAssignableFrom(serviceType))
            {
                return ChildContainer.Resolve(serviceType);
            }

            return IsRegistered(serviceType) ? ChildContainer.Resolve(serviceType) : null;
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (IsRegistered(serviceType))
            {
                yield return ChildContainer.Resolve(serviceType);
            }

            foreach (var service in ChildContainer.ResolveAll(serviceType))
            {
                yield return service;
            }
        }

        protected IUnityContainer ChildContainer
        {
            get
            {
                var childContainer = HttpContext.Current.Items[HttpContextKey] as IUnityContainer;

                if (childContainer == null)
                {
                    HttpContext.Current.Items[HttpContextKey] = childContainer = _container.CreateChildContainer();
                }

                return childContainer;
            }
        }

        public static void DisposeOfChildContainer()
        {
            var childContainer = HttpContext.Current.Items[HttpContextKey] as IUnityContainer;

            if (childContainer != null)
            {
                childContainer.Dispose();
            }
        }

        private bool IsRegistered(Type typeToCheck)
        {
            var isRegistered = true;

            if (typeToCheck.IsInterface || typeToCheck.IsAbstract)
            {
                isRegistered = ChildContainer.IsRegistered(typeToCheck);

                if (!isRegistered && typeToCheck.IsGenericType)
                {
                    var openGenericType = typeToCheck.GetGenericTypeDefinition();

                    isRegistered = ChildContainer.IsRegistered(openGenericType);
                }
            }

            return isRegistered;
        }
    }  


IOC 无需知道接口的实现,为BIZ业务类解耦。
可以在构造函数里面注入,也可以在属性里面注入,需要追加Dependency特性。
详细说明请参照 http://www.cnblogs.com/techborther/archive/2012/01/06/2313498.html

使用方法如下:


public class
GroupOrderController : BaseController { #region ///按照业务线划分 /// <summary> /// 非Booking酒店 类 /// </summary> [Dependency("GroupOrder")] public IOrderBiz COBiz { get; set; } /// <summary> /// Booking酒店 类 /// </summary> [Dependency("BookingGroupOrder")] public IOrderBiz BCOBiz { get; set; } /// <summary> /// 页面数据 /// </summary> [Dependency] public IPageDataBiz GroupPageData { get; set; }
  public ActionResult Index(int id)
  {
      return View();
  }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。