简单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,面向对象编程),通过封装.继承将程序抽象为各个层次的对象,进 ...
随机推荐
- IONIC 页面之间传递参数
HomePage 定义goToMyPage方法,传递id和name MyPage接收参数
- 组件之间的通讯:vuex状态管理,state,getters,mutations,actons的简单使用(一)
之前的文章中讲过,组件之间的通讯我们可以用$children.$parent.$refs.props.data... 但问题来了,假如项目特别大,组件之间的通讯可能会变得十分复杂... 这个时候了我们 ...
- C# Web Service 初级教学
原文连接:http://www.codeproject.com/cs/webservices/myservice.asp作者:Chris Maunder Introduction Creating y ...
- 2017头条笔试题:二维点集中找出右上角没有点的点并按x坐标从小到大打印坐标
PS:这篇是之前本来就想发的但是一直没时间写,加上今天做了京东的题,结果代码名就命名为jingdong了……懒得改代码名重新跑一遍结果了=.= 暴力法去做就是遍历每个点,判断它是不是“最大点”.判断过 ...
- 字符串strip相关函数
s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的所有字符,但只要遇到非rm序列中的字符就停止s.lstrip(rm) 删除s字符串中开头处,位于 rm删除序列的所有字符,,但只 ...
- ansible的安装过程 和基本使用
之前安装了一遍,到最后安装成功的时候出现了这种问题: [root@localhost ~]# ansible webserver -m command -a 'uptime' ............ ...
- -Java-Runoob-高级教程-实例-数组:09. Java 实例 – 数组扩容
ylbtech-Java-Runoob-高级教程-实例-数组:09. Java 实例 – 数组扩容 1.返回顶部 1. Java 实例 - 数组扩容 Java 实例 以下实例演示了如何在数组初始化后 ...
- php session保存到memcache或者memcached中
本教程叫你如何将php 的session存储在 memcached中,参考了好多网页,发现错误百出,最后自己还是测试成功了,现在将结果结果分享. 1-)系统环境 : elastix2.4 (cento ...
- Web API 源码剖析之全局配置
Web API 源码剖析之全局配置 Web API 均指Asp.net Web API .本节讲述的是基于Web API 系统在寄宿于IIS. 本节主要讲述Web API全局配置.它是如何优雅的实现 ...
- Spark wordcount开发并提交到集群运行
使用的ide是eclipse package com.luogankun.spark.base import org.apache.spark.SparkConf import org.apache. ...