简单AOP
代码如下
//使用说明
//1,新加接口与类
//2,新加类并实现ICallHandler类: ExecuteHandler
//3,新建特性并实现HandlerAttribute和重写其中的CreateHandler方法:ExecuteAttribute
//4,在接口上使用ExecuteAttribute特性
//5,在调用之前应设置拦截类,没有第二句代码,方法不会进入到ExecuteHandler.Invoke方法中
// var container1 = new UnityContainer().AddNewExtension<Interception>().RegisterType<Interface1, Class1>();
// container1.Configure<Interception>().SetInterceptorFor<Interface1>(new InterfaceInterceptor());
//6,调用
//注:第5,6说的就是 Main里面的方法。 using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var container1 = new UnityContainer().AddNewExtension<Interception>().RegisterType<Interface1, Class1>();
container1.Configure<Interception>().SetInterceptorFor<Interface1>(new InterfaceInterceptor()); var sample1 = container1.Resolve<Interface1>();
sample1.Add();
Console.ReadLine();
}
} [Execute]
public interface Interface1
{
void Add();
void Delete();
} public class Class1:Interface1
{
public void Add()
{
Console.WriteLine("Add成功!");
} public void Delete()
{
Console.WriteLine("Delete成功!");
}
} public class ExecuteHandler : ICallHandler
{
public int Order
{
get;
set;
} public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
//getNext()(input,getNext)就是具体的执行方法。在这之前或之后你可以做一些其它事情,如记录日志,判断是否有权限操作之类的。
var retvalue = getNext()(input, getNext);
return retvalue;
}
} public class ExecuteAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
{
return new ExecuteHandler();
}
}
}
简单AOP的更多相关文章
- 菜鸟学习Spring——60s配置XML方法实现简单AOP
一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...
- 菜鸟学习Spring——60s使用annotation实现简单AOP
一.概述. AOP大家都知道切面编程,在Spring中annotation可以实现简单的AOP列子.下面还未大家介绍几个概念: Aspect 对横切性关注点的模块化. Advice 对横切性关注点的具 ...
- Unity容器的简单AOP与DI的应用Demo(基于asp.net mvc框架)
转发请注明出处:https://home.cnblogs.com/u/zhiyong-ITNote/ 整个Demo是基于Controller-Service-Repository架构设计的,每一层之间 ...
- 用.net4中的DynamicObject实现简单AOP
public class DynamicWrapper : DynamicObject { private readonly object source; public DynamicWrapper( ...
- 一个简单的Spring AOP例子
转载自: http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html 经过这段日子的学习和使用Spring,慢慢地体会到Sprin ...
- 实现简单的 IOC 和 AOP
1 简单的 IOC 1.1 简单的 IOC 容器实现的步骤 加载 xml 配置文件,遍历其中的标签 获取标签中的 id 和 class 属性,加载 class 属性对应的类,并创建 bean 遍历标签 ...
- 使用spring方式来实现aop编程
1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring核心概念之AOP
一.AOP 的概念 AOP(Aspect Oriented Programming)的缩写,面向切面编程,主要作用就是对代码进行增强处理. 理解面向切面编程的含义:就是在不改变原有程序的基础上为代码增 ...
- OOP的完美点缀—AOP之SpringAOP实现原理
OOP的完美点缀-AOP之SpringAOP实现原理 前言 OOP与AOP OOP(Object Oriented Programming,面向对象编程),通过封装.继承将程序抽象为各个层次的对象,进 ...
随机推荐
- msp430学习笔记-时钟及延时函数
引用:http://blog.chinaunix.net/uid-24343357-id-3271380.html MCLK默认时钟源是DCOCLK,SMCLK默认时钟源也是DCOCLK,DCOCLK ...
- 大数据应用之HBase数据插入性能优化之多线程并行插入测试案例
一.引言: 上篇文章提起关于HBase插入性能优化设计到的五个参数,从参数配置的角度给大家提供了一个性能测试环境的实验代码.根据网友的反馈,基于单线程的模式实现的数据插入毕竟有限.通过个人实测,在我的 ...
- ELK高可用搭建---Elasticsearch配置(1)
########################ElasticSearch#######################环境:192.168.125.200 elasticsearch+logst ...
- 杂项:MSMQ
ylbtech-杂项:MSMQ MicroSoft Message Queuing(微软消息队列)是在多个不同的应用之间实现相互通信的一种异步传输模式,相互通信的应用可以分布于同一台机器上,也可以分布 ...
- [转]连连看游戏 C#
源代码下载地址 http://files.cnblogs.com/files/z5337/%E8%BF%9E%E8%BF%9E%E7%9C%8B%E6%B8%B8%E6%88%8F.rar 代码由 & ...
- Windows下python 安装Mysqldb模块
CMD执行 pip install mysql-python 报错如下: 1.如果报类似 Microsoft Visual C++ 9.0 is required < Unable to fin ...
- MySQL excel导入
说明: 1 因在测试发现如果用SQLyog导入数据需要下载excel驱动,因而选择Navicat 2 之前选择excel文件为xlsx 发现Navicat识别不了,因而转存为xls文件,测试OK 1 ...
- 1043 Is It a Binary Search Tree (25 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- [UE4]C++调用蓝图函数:BlueprintImplementableEvent函数说明符用法
用BlueprintImplementableEvent标明的函数在C++代码中不需要有方法体,方法体在蓝图中实现. 用法: 1,现在C++头文件中定义函数名 UFUNCTION(BlueprintI ...
- [UE4]头文件循环依赖C++
有2个类:aaa和bbb. aaa.h已经#include了bbb.h,则bbb.h就不能#include aaa.h,但bbb.cpp可以#include aaa.h bbb.h已经#include ...