• 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();
      }
    }
  • 相关阅读:
    缺陷与出路——一个游戏开发者的反思(转自《大众软件》)
    Arcengine 根据坐标串生成几何图形
    C#Arcengine通过坐标点生成面(环形)
    解析XML文件
    arcgis下载地址
    C#读写txt文件的两种方法介绍
    可伸缩性最佳实践:来自eBay的经验[精华强贴, 转之]
    请问怎样才能监视数据库表的变化?[转]
    VS2010中,在新建项目的时候,删除默认新建路径或曾经使用过的路径
    VS2010注册表垃圾清理
  • 原文地址:https://www.cnblogs.com/kfsmqoo/p/3995119.html
Copyright © 2020-2023  润新知