热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在MVC控制器中使用构造函数时行依赖注入(IoC)

在Controller中使用构造函数进行依赖注入(IoC)1.Controller代码:ICardcard;ICardCategorycardCategory;pub

 在 Controller 中使用 构造函数进行依赖注入 (IoC)

1. Controller 代码:

 

ICard card;ICardCategory cardCategory;public CardController(ICard card, ICardCategory cardCategory){this.card = card;this.cardCategory = cardCategory;}

 接口 ICard , ICardCateogry 是一个普通的接口,没有继承或实现任何接口,而需要做的就是在 Gloable.cs 文件中解析依赖, 为了方便复用,将解析依赖封装成一个类,然后在 Gloable.cs 文件中注册,在这个示例中,使用的是 Unity 进行依赖注入,完整的代码如下:

using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
//using System.Web.Http.Dependencies;
using System.Web;
using System.Web.Mvc;namespace SnsManage.Resolver
{
///

/// Dependency Injection Resolver Container /// public class UnityResolver : IDependencyResolver{protected IUnityContainer container;public UnityResolver(IUnityContainer container){if (container &#61;&#61; null){throw new ArgumentNullException("container");}this.container &#61; container;}public object GetService(Type serviceType){try{return container.Resolve(serviceType);}catch (ResolutionFailedException){return null;}}public IEnumerable<object> GetServices(Type serviceType){try{return container.ResolveAll(serviceType);}catch (ResolutionFailedException){return new List<object>();}}//public IDependencyScope BeginScope()//{// var child &#61; container.CreateChildContainer();// return new UnityResolver(child);//}//public void Dispose()//{// container.Dispose();//}//private const string HttpContextKey &#61; "perRequestContainer";//private readonly IUnityContainer container;//public UnityResolver(IUnityContainer container)//{// this.container &#61; container;//}//public object GetService(Type serviceType)//{// if (typeof(IController).IsAssignableFrom(serviceType))// {// return ChildContainer.Resolve(serviceType);// }// return IsRegistered(serviceType) ? ChildContainer.Resolve(serviceType) : null; //}//public IEnumerable 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 &#61; HttpContext.Current.Items[HttpContextKey] as IUnityContainer;// if (childContainer &#61;&#61; null)// {// HttpContext.Current.Items[HttpContextKey] &#61; childContainer &#61; container.CreateChildContainer();// }// return childContainer;// }//} //public static void DisposeOfChildContainer()//{// var childContainer &#61; HttpContext.Current.Items[HttpContextKey] as IUnityContainer;// if (childContainer !&#61; null)// {// childContainer.Dispose();// }//}//private bool IsRegistered(Type typeToCheck)//{// var isRegistered &#61; true;// if (typeToCheck.IsInterface || typeToCheck.IsAbstract)// {// isRegistered &#61; ChildContainer.IsRegistered(typeToCheck);// if (!isRegistered && typeToCheck.IsGenericType)// {// var openGenericType &#61; typeToCheck.GetGenericTypeDefinition();// isRegistered &#61; ChildContainer.IsRegistered(openGenericType);// }// }// return isRegistered;//}
}
}
View Code

然后在 App_Start 文件中添加一个配置类&#xff0c; UnityConfig.cs, 其中的代码如下&#xff1a;

public static class UnityConfig{///

/// Dependency Injection /// public static void RegisterComponents(){var container &#61; new UnityContainer();// register all your components with the container here// it is NOT necessary to register your controllers// e.g. container.RegisterType();
container.RegisterType( new HierarchicalLifetimeManager());DependencyResolver.SetResolver(new UnityResolver(container));}}

最后在 Global.asax 文件中注册

UnityConfig.RegisterComponents();

 


转载于:https://www.cnblogs.com/wisdo/p/4374475.html


推荐阅读
author-avatar
手机用户2502858307
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有