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功能例子的更多相关文章

  1. SpringBoot中使用LoadTimeWeaving技术实现AOP功能

    目录 1. 关于LoadTimeWeaving 1.1 LTW与不同的切面织入时机 1.2 JDK实现LTW的原理 1.3 如何在Spring中实现LTW 2. Springboot中使用LTW实现A ...

  2. (转)使用JDK中的Proxy技术实现AOP功能

    http://blog.csdn.net/yerenyuan_pku/article/details/52863780 AOP技术在企业开发中或多或少都会用到,但用的最多的大概就是做权限系统时,在做权 ...

  3. C# Unity依赖注入利用Attribute实现AOP功能

    使用场景? 很多时候, 我们定义一个功能, 当我们要对这个功能进行扩展的时候, 按照常规的思路, 我们一般都是利用OOP的思想, 在原有的功能上进行扩展. 那么有没有一种东西, 可以实现当我们需要扩展 ...

  4. 实现AOP功能的封装与配置的小框架

    内容 java基础巩固笔记 - 实现AOP功能的封装与配置的小框架 设计(目录): XXX = java.util.ArrayList中 代码 Advice接口 MyAdvice类 BeanFacto ...

  5. Spring AOP功能和目标

    1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封装为一个可 ...

  6. aop学习总结二------使用cglib动态代理简单实现aop功能

    aop学习总结二------使用cglib动态代理简单实现aop功能 模拟业务需求: 1.拦截所有业务方法 2.判断用户是否有权限,有权限就允许用户执行业务方法,无权限不允许用户执行业务方法 (判断是 ...

  7. aop学习总结一------使用jdk动态代理简单实现aop功能

    aop学习总结一------使用jdk动态代理实现aop功能 动态代理:不需要为目标对象编写静态代理类,通过第三方或jdk框架动态生成代理对象的字节码 Jdk动态代理(proxy):目标对象必须实现接 ...

  8. (转)使用CGLIB实现AOP功能与AOP概念解释

    http://blog.csdn.net/yerenyuan_pku/article/details/52864395 使用CGLIB实现AOP功能 在Java里面,我们要产生某个对象的代理对象,这个 ...

  9. Ioc 之 Unity的AOP功能

    前面我们介绍了Unity的依赖注入功能,现在来介绍下Unity的AOP功能.AOP是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...

随机推荐

  1. leetcode207

    拓扑排序问题. class Solution { public: bool canFinish(int numCourses, vector<pair<int, int>>&a ...

  2. 阿里云视频直播PHP-SDK

    阿里云 视频直播 配置 及 PHP-SDK 接入教程准备工作域名管理配置鉴权地址生成器及DEMO演示-熟悉鉴权接入SDK推流回调的配置阿里云 视频直播 配置 及 PHP-SDK 接入教程 个人感觉,阿 ...

  3. ReactiveX 学习笔记(20)使用 RxJava + RxBinding 进行 GUI 编程

    课题 程序界面由3个文本编辑框和1个文本标签组成. 要求文本标签实时显示3个文本编辑框所输入的数字之和. 文本编辑框输入的不是合法数字时,将其值视为0. 3个文本编辑框的初值分别为1,2,3. 创建工 ...

  4. xtrabackup备份还原MySQL数据库

    mysqldump 备份鉴于其自身的某些特性(锁表,本质上备份出来insert脚本或者文本,不支持差异备份),不太适合对实时性要求比较高的情况Xtrabackup可以解决mysqldump存在的上述的 ...

  5. JDK中的注解简单了解

    0.注解(注解是给编译器看的东东) 注解的定义方式是@Interface,注解属性定义是类似于普通类的方法定义的,注解属性赋值是使用default关键字完成的,如下图所示 注解在定义时可以给默认值,也 ...

  6. FM-分解机模型详解

    https://blog.csdn.net/zynash2/article/details/80029969 FM论文地址:https://www.csie.ntu.edu.tw/~b97053/pa ...

  7. [剑指Offer]7-重建二叉树

    链接 https://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tqId=11157&tPa ...

  8. 选择困难症的福音——团队Scrum冲刺阶段-Day 4

    选择困难症的福音--团队Scrum冲刺阶段-Day 4 今日进展 编写提问部分 做了不同问题所对应的游戏选项,但关于游戏分类的界面还没有做完 增加功能 昨天在主界面增加"关于我们" ...

  9. [TestNG] Eclipse/STS中两种安装TestNG的方法

    Two Ways To Install TestNG in Eclipse/STS Today I install the newest Sprint Tool Suite and want to i ...

  10. windows 性能监视器

    转载地址:https://www.cnblogs.com/luo-mao/p/5872374.html