简单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,面向对象编程),通过封装.继承将程序抽象为各个层次的对象,进 ...
随机推荐
- Tomcat 8 中的startup.bat
1. bat文件 批处理文件,在DOS和Windows(任意版本)的系统中,bat文件是可执行文件 2. startup.bat 中涉及的bat语法 2.1 @. echo. echo off @ec ...
- [boost] : asser库用法
基本用法 需要包含头文件#include <boost/assert.hpp> assert库定义了两个断言宏 BOOST_ASSERT BOOSE_ASSERT_MSG 第一种形式等价于 ...
- thinkphp 3.2.3在nginx+php下的url重写配置经验
环境:centos7.2+lnmp1.3(nginx+php7.0+mysql5.5) 进入服务器配置路径:cd /usr/local/nginx/conf/nginx.conf 修改nginx.co ...
- qt ISO C++ forbids declaration of 'XXXX' with no type
error: ISO C++ forbids declaration of 'XXXX' with no type 出现这个错误,一般是由于两个CPP相互都相互包含了对方的头文件造成的,比如: 当 ...
- linux 线程的同步 二 (互斥锁和条件变量)
互斥锁和条件变量 为了允许在线程或进程之间共享数据,同步时必须的,互斥锁和条件变量是同步的基本组成部分. 1.互斥锁 互斥锁是用来保护临界区资源,实际上保护的是临界区中被操纵的数据,互斥锁通常用于保护 ...
- OOD与OOP的思想的感悟
Walking on water and developing software from a specification are easy if both are frozen) -Edward V ...
- Python数据结构算法
Python内置了许多非常有用的数据结构,比如列表(list),集合(set)以及字典(dictionary).就绝大部分情况而言,我们可以直接使用这些数据结构.但是,我们通常还要考虑比如搜索,排序, ...
- idea配置(卡顿、开发环境等配置),code style template
Tomcat配置VM Options: -XX:PermSize=512m -XX:MaxPermSize=1024m 1.IDEA卡顿,修改IDEA使用内存 修改idea配置文件 在IDEA的 ...
- Delphi Webbrowser使用方法详解(二)
delphi如何用webbrowser模拟登录网站? 我们就以如何登录博客园来做示例: 1.要登入一个网站,首先要获取网页的源代码,我们可以通过网页菜单--查看--查看源代码来获取. 2.我们找到登录 ...
- [jni]Getting Started
写一个java应用程序来调用C函数打印“Hello World!" 这个过程包括以下步骤: 1:创建一个申明了native方法的java类(HelloWorld.java): 2:使用jav ...