在 MVC 控制器中使用 构造函数时行依赖注入 (IoC)
2015-03-28 17:08
405 查看
在 Controller 中使用 构造函数进行依赖注入 (IoC)
1. Controller 代码:
接口 ICard , ICardCateogry 是一个普通的接口,没有继承或实现任何接口,而需要做的就是在 Gloable.cs 文件中解析依赖, 为了方便复用,将解析依赖封装成一个类,然后在 Gloable.cs 文件中注册,在这个示例中,使用的是 Unity 进行依赖注入,完整的代码如下:
View Code
然后在 App_Start 文件中添加一个配置类, UnityConfig.cs, 其中的代码如下:
最后在 Global.asax 文件中注册
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 { /// <summary> /// Dependency Injection Resolver Container /// </summary> public class UnityResolver : IDependencyResolver { protected IUnityContainer container; public UnityResolver(IUnityContainer container) { if (container == null) { throw new ArgumentNullException("container"); } this.container = 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 = container.CreateChildContainer(); // return new UnityResolver(child); //} //public void Dispose() //{ // container.Dispose(); //} //private const string HttpContextKey = "perRequestContainer"; //private readonly IUnityContainer container; //public UnityResolver(IUnityContainer container) //{ // this.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; //} } }
View Code
然后在 App_Start 文件中添加一个配置类, UnityConfig.cs, 其中的代码如下:
public static class UnityConfig { /// <summary> /// Dependency Injection /// </summary> public static void RegisterComponents() { var container = new UnityContainer(); // register all your components with the container here // it is NOT necessary to register your controllers // e.g. container.RegisterType<ITestService, TestService>(); container.RegisterType<IUser, UserService>( new HierarchicalLifetimeManager()); DependencyResolver.SetResolver(new UnityResolver(container)); } }
最后在 Global.asax 文件中注册
UnityConfig.RegisterComponents();
相关文章推荐
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- ASP.NET MVC中的依赖倒置IOC/依赖注入DI, (MircroSoft.Practices.Unity)与asp.net mvc 3的融合
- 扩展ASP.NET MVC三层框架且使用StructureMap实现依赖注入1-Model层
- C#使用AutoFac实现IOC依赖注入
- 在mvc中使用Ninject进行依赖注入
- 《Pro ASP.NET MVC 3 Framework》学习笔记之五【依赖注入及ninject工具使用】
- 在ASP.NET MVC中使用Unity进行依赖注入的三种方式
- android使用篇(四) 注解依赖注入IOC实现绑定控件
- 在ASP.NET MVC中使用Unity进行依赖注入的三种方式
- 使用XPath解析xml实现简单的Spring IOC完成bean的依赖注入
- spring Ioc 依赖注入的三种方式:构造函数注入、setter方法注入和接口注入
- 扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入1
- 2014-05-08android使用篇(四) 注解依赖注入IOC实现绑定控件
- MVC 2.0+Entity Framework在使用依赖注入时遇到的问题
- .NET平台下的依赖注入(IOC)和 面向切面(AOP)相结合简洁实用的使用方式
- 扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入1-Model层的实现
- 扩展ASP.NET MVC三层框架并使用StructureMap实现依赖注入2-Repository层的实现
- android使用篇(四) 注解依赖注入IOC实现绑定控件
- MVC使用StructureMap实现依赖注入Dependency Injection
- C# 对轻量级(IoC Container)依赖注入Unity的使用