Autofac与AOP功能例子
using Autofac.Extras.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace aopTest
{
[Intercept(typeof(CallLogger))]
public interface IProduct
{
string Title { get; set; }
decimal Price { get; set; }
string Show();
} [Intercept(typeof(CallLogger))]
public class Apple : IProduct
{
public Apple()
{
Title = "苹果";
Price = 5.0m;
} public string Title { get ; set ; }
public decimal Price { get ; set; } public virtual string Show()
{
return $"{Title} - {Price}";
}
} [Intercept(typeof(CallLogger))]
public class Book : IProduct
{
public Book()
{
Title = "Asp.net开发";
Price = 35.0m;
} public string Title { get; set; }
public decimal Price { get; set; } [Auth("product.show")]
public virtual string Show()
{
Test();
return $"{Title} - {Price}";
} protected virtual void Test()
{
Console.WriteLine("is a test code....");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace aopTest
{
[AttributeUsage(AttributeTargets.Method)]
public class AuthAttribute : Attribute
{
public AuthAttribute(string code)
{
Code = code;
}
public string Code { get; set; }
}
} using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks; namespace aopTest
{
public class UserInfo
{
static LocalDataStoreSlot storeSlot = Thread.AllocateNamedDataSlot("user-info"); public static void SetUser(string name)
{
Thread.SetData(storeSlot, name);
} public static string GetUser()
{
return Thread.GetData(storeSlot).ToString();
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy; namespace aopTest
{
public class CallLogger : IInterceptor
{
TextWriter _output; public CallLogger(TextWriter output)
{
_output = output;
} public void Intercept(IInvocation invocation)
{
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
string m = invocation.TargetType.ToString() + "." + invocation.Method.Name;
_output.WriteLine($"方法:{m}调用开始");
_output.Write("Calling method {0} with parameters {1}... ",
invocation.Method.Name,
string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray())); var authAttribute = invocation.Method.GetCustomAttributes(typeof(AuthAttribute), false).FirstOrDefault();
if (authAttribute != null)
{
var authInfo = (AuthAttribute)authAttribute;
if (UserInfo.GetUser() != "zhangsan" && authInfo.Code == "product.show")
{
_output.WriteLine($"当前用户{UserInfo.GetUser()}没有方法{m}的访问权限,需要权限{authInfo.Code}");
}
} invocation.Proceed();
_output.WriteLine("Done: result was {0}.", invocation.ReturnValue);
stopWatch.Stop();
_output.WriteLine($"方法:{m}调用结束,总耗时{stopWatch.ElapsedMilliseconds}毫秒");
}
}
}
using Autofac;
using Autofac.Extras.DynamicProxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace aopTest
{
class Program
{
static void Main(string[] args)
{
var builder = new ContainerBuilder();
/*builder.RegisterType<Apple>()
.As<IProduct>()
.EnableInterfaceInterceptors(); builder.RegisterType<Book>()
.As<IProduct>()
.EnableInterfaceInterceptors();*/ builder.RegisterType<Apple>()
.As<IProduct>()
.EnableClassInterceptors(); builder.RegisterType<Book>()
.As<IProduct>()
.EnableClassInterceptors(); builder.Register(c => new CallLogger(Console.Out));
var container = builder.Build(); UserInfo.SetUser("zhaoliu"); var products = container.Resolve<IEnumerable<IProduct>>();
products.ToList().ForEach(product => {
product.Show();
}); Console.ReadKey();
}
}
}

Autofac与AOP功能例子的更多相关文章
- SpringBoot中使用LoadTimeWeaving技术实现AOP功能
目录 1. 关于LoadTimeWeaving 1.1 LTW与不同的切面织入时机 1.2 JDK实现LTW的原理 1.3 如何在Spring中实现LTW 2. Springboot中使用LTW实现A ...
- (转)使用JDK中的Proxy技术实现AOP功能
http://blog.csdn.net/yerenyuan_pku/article/details/52863780 AOP技术在企业开发中或多或少都会用到,但用的最多的大概就是做权限系统时,在做权 ...
- C# Unity依赖注入利用Attribute实现AOP功能
使用场景? 很多时候, 我们定义一个功能, 当我们要对这个功能进行扩展的时候, 按照常规的思路, 我们一般都是利用OOP的思想, 在原有的功能上进行扩展. 那么有没有一种东西, 可以实现当我们需要扩展 ...
- 实现AOP功能的封装与配置的小框架
内容 java基础巩固笔记 - 实现AOP功能的封装与配置的小框架 设计(目录): XXX = java.util.ArrayList中 代码 Advice接口 MyAdvice类 BeanFacto ...
- Spring AOP功能和目标
1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封装为一个可 ...
- aop学习总结二------使用cglib动态代理简单实现aop功能
aop学习总结二------使用cglib动态代理简单实现aop功能 模拟业务需求: 1.拦截所有业务方法 2.判断用户是否有权限,有权限就允许用户执行业务方法,无权限不允许用户执行业务方法 (判断是 ...
- aop学习总结一------使用jdk动态代理简单实现aop功能
aop学习总结一------使用jdk动态代理实现aop功能 动态代理:不需要为目标对象编写静态代理类,通过第三方或jdk框架动态生成代理对象的字节码 Jdk动态代理(proxy):目标对象必须实现接 ...
- (转)使用CGLIB实现AOP功能与AOP概念解释
http://blog.csdn.net/yerenyuan_pku/article/details/52864395 使用CGLIB实现AOP功能 在Java里面,我们要产生某个对象的代理对象,这个 ...
- Ioc 之 Unity的AOP功能
前面我们介绍了Unity的依赖注入功能,现在来介绍下Unity的AOP功能.AOP是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...
随机推荐
- 协程,greenlet,gevent
""" 协程 """ ''' 协程: 类似于一个可以暂停的函数,可以多次传入数据,可以多次返回数据 协程是可交互的 耗资源大小:进程 --& ...
- [UGUI]Image源码分析
unity版本5.3.5 一.属性 1.overrideSprite 脚本对精灵的访问均使用overrideSprite,如果m_OverrideSprite存在就使用m_OverrideSprite ...
- python 列表、元组、字典的区别
区别: 相互转换:https://www.cnblogs.com/louis-w/p/8391147.html 一.列表 list [1,[2,'AA'],5,'orderl'] 1.任意对象的有序集 ...
- Django 的ORM 数据操作
from teatcher.models import Student 导入app(teatcher)下的模型(Student) In [11]: res = Student.objects ...
- Linux LVM扩容和缩容
将原硬盘上的LVM分区/dev/mapper/RHEL-Data由原来的60G扩展到80G Step1:将LVData扩容+20G,如下图: [root@esc data]# lvextend -L ...
- Power designer 的使用
1.Powere Designer 逆向 工程 首先 逆向工程 就是将数据库表 导入到模型, 首先新建个模型, 此处就省略 ... 工具栏,数据库(database) 下的 update model ...
- Spring Kafka中关于Kafka的配置参数
#################consumer的配置参数(开始)################# #如果'enable.auto.commit'为true,则消费者偏移自动提交给Kafka的频率 ...
- PHP开发——数据类型
概述 l 变量就是一个容器,变量本身并没有类型,变量的类型解决值的类型. l PHP和JS都属于弱类型语言,变量在运行过程中,类型是可以变的.但是,Java不可以. l 标量(基本)数据类型:字 ...
- elk kibana查询语法
elk日志系统中kibana查询语法 单项term查询 例: 搜 Dahlen, Malone 字段field查询 field:value 例:city:Keyport, age:26 通配符 ? 匹 ...
- 主机性能监控之wmi 获取进程信息
标 题: 主机性能监控之wmi 获取进程信息作 者: itdef链 接: http://www.cnblogs.com/itdef/p/3990499.html 欢迎转帖 请保持文本完整并注明出处 仅 ...