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是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...
随机推荐
- RTTI和反射小结
Java有两种方式让我们在运行时识别对象和类的信息:1.“传统的”RTTI,假定所有的类型编译时已知:2.“反射”机制,允许在运行时发现和使用类的信息. 一.RTTI RTTI(Run-Time Ty ...
- MYSQL性能优化(2)
Insert语句优化 1. 多行并为一个语句 insert into table values (行1),(行2),........... 2. 使用中间内存队列, 逻辑是立马执行插入,其他数据放 ...
- 使用swiper插件,隐藏swiper后再显示,不会触发自动播放的解决办法
问题: 项目中有一个需求,当点击P1时,两个页面进行轮播.当点击P2时,页面不轮播. 设置好以后,点击P2,再点击P1,此时页面不能自动轮播,只能手动触发. 解决: 在轮播器配置里,配置observe ...
- week07 13.1 NewsPipeline之 一 NewsMonitor
我们要重构一下代码 因为我们之前写了utils 我们的NewsPipeline部分也要用到 所以我们把他们单独独立得拿出来 删掉原来的 将requirements.txt也拿出去 现在我们搬家完成 我 ...
- Linux简单学习
参考自:http://www.runoob.com/linux/linux-tutorial.html 一.是什么 Linux 类Unix操作系统.是一个基于POSIX和UNIX的多用户.多任务.支 ...
- actuator/hystrix.stream 没有反应的方法
http://localhost:8086/actuator/hystrix.stream 在启动类加上,就ok了 @Bean public ServletRegistrationBean hystr ...
- .Net 中读写Oracle数据库常用两种方式
.net中连接Oracle 的两种方式:OracleClient,OleDb转载 2015年04月24日 00:00:24 10820.Net 中读写Oracle数据库常用两种方式:OracleCli ...
- VMware虚拟机配置嵌套虚拟化
VMware虚拟机下创建kvm-sever,server下继续创建kvm虚拟机(嵌套虚拟化),返回libvirt错误解决办法:SSH连接VMwarevi /etc/vmware/config增加一行设 ...
- phpcms的一些问题 乱码,安装
一.乱码:我这的网站出现的乱码情况:后台栏目名乱码,迁站后更新缓存,再更新栏目,内容,前台都乱码. 找了半天原因,经过本地测试,没问题,一上线就出现问题,不同点就是线上的数据库版本是mysql5.5, ...
- 业务数据实体(model) 需要克隆的方法
业务数据实体(model) 需要克隆的时候 可以使用 Json.Deserialize<InquireResult>(Json.Serialize<InquireResult> ...