.NET平台下的依赖注入(IOC)和 面向切面(AOP)相结合简洁实用的使用方式
2013-02-20 16:18
706 查看
近日, 学习.NET平台下的依赖注入(IOC)和 面向切面(AOP)。
力求获得一种简洁实用的方法实现IOC和AOP相结合的使用方式。
查阅了多个技术资料。经过多次测试,基本达到目的。
IOC使用微软的 Microsoft.Practices.Unity,AOP 使用微软企业库的 Microsoft.Practices.EnterpriseLibrary.PolicyInjection实现。
第一步 首先在CONFIG文件中添加配置(全部代码见下载项目文件)
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
和
<unity configSource="unity.xml" />
然后CONFIG文件同级目录建立文件 unity.xml
在文件中注册依赖注入类型,例如
<register name="ASP.default_aspx.class" type="IClass" mapTo="MyClass" >
然后添加 面向切面 的扩展配置
<interceptor type="InterfaceInterceptor"/>
<policyInjection/>
一个完整的类型节点如下
<register name="ASP.default_aspx.class" type="IClass" mapTo="MyClass" >
<interceptor type="InterfaceInterceptor"/>
<policyInjection/>
</register>
其中IClass和 MyClass 均为别名。另外还需要sectionExtension配置,见项目代码。
第二步 编写AOP对象 ,例如 创建一个日志对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Collections.Specialized;
namespace WebApplication1
{
[ConfigurationElementType(typeof(CustomCallHandlerData))]
public class UserLogCallHandler : ICallHandler
{
/// <summary>
/// 构造函数,此构造函数是用于Attribute调用
/// </summary>
/// <param name="message">消息</param>
/// <param name="parameterName">参数名</param>
public UserLogCallHandler(string message, string parameterName)
{
this.Message = message;
this.ParameterName = parameterName.Trim();
}
/// <summary>
/// 构造函数,此处不可省略,否则会导致异常
/// </summary>
/// <param name="attributes">配置文件中所配置的参数</param>
public UserLogCallHandler(NameValueCollection attributes)
{
//从配置文件中获取key,如不存在则指定默认key
this.Message = String.IsNullOrEmpty(attributes["Message"]) ? "" : attributes["Message"];
this.ParameterName = String.IsNullOrEmpty(attributes["ParameterName"]) ? "" : attributes["ParameterName"];
}
/// <summary>
/// 实现ICallHandler.Invoke方法,用于对具体拦截方法做相应的处理
/// </summary>
/// <param name="input"></param>
/// <param name="getNext"></param>
/// <returns></returns>
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
//检查参数是否存在
if (input == null) throw new ArgumentNullException("input");
if (getNext == null) throw new ArgumentNullException("getNext");
//开始拦截,此处可以根据需求编写具体业务逻辑代码
if (input.Inputs.Count > 0 && input.Arguments.Count > 0)
{
var ss = input.Inputs[0];
var sss = input.Arguments[0]; //((Microsoft.Practices.Unity.InterceptionExtension.ParameterCollection)(input.Arguments)).arguments[0];
}
////写入数据库或文件 代码忽略,可以使用 依赖注入 日志类
//调用具体方法
var result = getNext()(input, getNext);
return result;
}
public string Message { get; set; }
public string ParameterName { get; set; }
private int _order = 0;
public int Order
{
get
{
return _order;
}
set
{
_order = value;
}
}
}
[AttributeUsage(AttributeTargets.Method)]
public class UserLogCallHandlerAttribute : HandlerAttribute
{
public UserLogCallHandlerAttribute(string message, string ParameterName)
{
this.Message = message;
this.ParameterName = ParameterName;
}
public string Message { get; set; }
public string ParameterName { get; set; }
public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
{
//创建具体Call Handler,并调用
UserLogCallHandler handler = new UserLogCallHandler(this.Message, this.ParameterName);
return handler;
}
}
}
还可以创建 数据验证列 ,异常处理类 ,等,
第三部 在方法前添加 写日志特性,这是 AOP的用法
[UserLogCallHandler("aaa", "bbb")]
public void ShowInfo1()
{
System.Web.HttpContext.Current.Response.Write(this.GetHashCode().ToString() + " ShowInfo1<br>");
}
第四部 使用UnityContainer实例化unity.xml中注册过的对象 (IOC)
var container = new UnityContainer();
container.LoadConfiguration();
var obj = container.Resolve<T>(name);
项目源码下载 http://download.csdn.net/detail/peiyu_peiyu/5075329
力求获得一种简洁实用的方法实现IOC和AOP相结合的使用方式。
查阅了多个技术资料。经过多次测试,基本达到目的。
IOC使用微软的 Microsoft.Practices.Unity,AOP 使用微软企业库的 Microsoft.Practices.EnterpriseLibrary.PolicyInjection实现。
第一步 首先在CONFIG文件中添加配置(全部代码见下载项目文件)
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
和
<unity configSource="unity.xml" />
然后CONFIG文件同级目录建立文件 unity.xml
在文件中注册依赖注入类型,例如
<register name="ASP.default_aspx.class" type="IClass" mapTo="MyClass" >
然后添加 面向切面 的扩展配置
<interceptor type="InterfaceInterceptor"/>
<policyInjection/>
一个完整的类型节点如下
<register name="ASP.default_aspx.class" type="IClass" mapTo="MyClass" >
<interceptor type="InterfaceInterceptor"/>
<policyInjection/>
</register>
其中IClass和 MyClass 均为别名。另外还需要sectionExtension配置,见项目代码。
第二步 编写AOP对象 ,例如 创建一个日志对象:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.Unity.InterceptionExtension;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Collections.Specialized;
namespace WebApplication1
{
[ConfigurationElementType(typeof(CustomCallHandlerData))]
public class UserLogCallHandler : ICallHandler
{
/// <summary>
/// 构造函数,此构造函数是用于Attribute调用
/// </summary>
/// <param name="message">消息</param>
/// <param name="parameterName">参数名</param>
public UserLogCallHandler(string message, string parameterName)
{
this.Message = message;
this.ParameterName = parameterName.Trim();
}
/// <summary>
/// 构造函数,此处不可省略,否则会导致异常
/// </summary>
/// <param name="attributes">配置文件中所配置的参数</param>
public UserLogCallHandler(NameValueCollection attributes)
{
//从配置文件中获取key,如不存在则指定默认key
this.Message = String.IsNullOrEmpty(attributes["Message"]) ? "" : attributes["Message"];
this.ParameterName = String.IsNullOrEmpty(attributes["ParameterName"]) ? "" : attributes["ParameterName"];
}
/// <summary>
/// 实现ICallHandler.Invoke方法,用于对具体拦截方法做相应的处理
/// </summary>
/// <param name="input"></param>
/// <param name="getNext"></param>
/// <returns></returns>
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
//检查参数是否存在
if (input == null) throw new ArgumentNullException("input");
if (getNext == null) throw new ArgumentNullException("getNext");
//开始拦截,此处可以根据需求编写具体业务逻辑代码
if (input.Inputs.Count > 0 && input.Arguments.Count > 0)
{
var ss = input.Inputs[0];
var sss = input.Arguments[0]; //((Microsoft.Practices.Unity.InterceptionExtension.ParameterCollection)(input.Arguments)).arguments[0];
}
////写入数据库或文件 代码忽略,可以使用 依赖注入 日志类
//调用具体方法
var result = getNext()(input, getNext);
return result;
}
public string Message { get; set; }
public string ParameterName { get; set; }
private int _order = 0;
public int Order
{
get
{
return _order;
}
set
{
_order = value;
}
}
}
[AttributeUsage(AttributeTargets.Method)]
public class UserLogCallHandlerAttribute : HandlerAttribute
{
public UserLogCallHandlerAttribute(string message, string ParameterName)
{
this.Message = message;
this.ParameterName = ParameterName;
}
public string Message { get; set; }
public string ParameterName { get; set; }
public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
{
//创建具体Call Handler,并调用
UserLogCallHandler handler = new UserLogCallHandler(this.Message, this.ParameterName);
return handler;
}
}
}
还可以创建 数据验证列 ,异常处理类 ,等,
第三部 在方法前添加 写日志特性,这是 AOP的用法
[UserLogCallHandler("aaa", "bbb")]
public void ShowInfo1()
{
System.Web.HttpContext.Current.Response.Write(this.GetHashCode().ToString() + " ShowInfo1<br>");
}
第四部 使用UnityContainer实例化unity.xml中注册过的对象 (IOC)
var container = new UnityContainer();
container.LoadConfiguration();
var obj = container.Resolve<T>(name);
项目源码下载 http://download.csdn.net/detail/peiyu_peiyu/5075329
相关文章推荐
- Spring学习,依赖注入(DI)、控制反转(IoC)和面向切面(AOP)
- Spring框架使用(控制反转,依赖注入,面向切面AOP)
- spring的依赖注入(DI)、控制反转(IOC)和面向切面(AOP)
- IOC和AOP使用扩展 多种方式实现依赖注入
- Spring 之 控制反转(IoC), 依赖注入(DI)和面向切面(AOP)
- 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现
- Spring框架运行机制(控制反转,依赖注入,面向切面AOP)
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- spring ioc容器注入的集中方式,以及属性注入的集中依赖,继承,使用SPEL表达式,ioc容器的生命周期,bean的几种工厂方法配置方式,bean的自动装配
- .NET平台依赖注入机制及IoC的设计与实现
- 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现[转]
- 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现
- 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- Spring(十一)使用Spring的注解方式实现面向切面技术AOP
- 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现
- 笔记——spring基本使用(控制反转IOC、面向切面编程AOP、声明式事务)
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- 基于.NET平台的分层架构实战(六)——依赖注入机制及IoC的设计与实现
- Spring——使用Spring的注解方式实现面向切面技术AOP