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是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...
随机推荐
- issue_hana
2019-01-11T14:35:01.910187+08:00 SHADEVDB01 cron[57062]: PAM unable to dlopen(/lib64/security/pam_sy ...
- Java 日期比较大小
import org.junit.Test; import java.text.SimpleDateFormat; import java.util.Date; /** * @author DateJ ...
- 【学习】基础知识:数组和矢量计量【Numpy】
Numpy是高性能科学计算和数据分析的基础包.功能如下: ndarray 一个具有矢量算法运算和复杂广播能力的快速且节省空间的多维数组 用于对整组数据进行快速运算的标准数学函数(无需编写循环) 用于读 ...
- PHP安装Commposer
一先把php加到环境变量里面测试 看一下版本号: 二,composer得安装注意安装的时候 php必须在5.59以上版本,openssl的扩展开启,pdo的扩展开启,mbstring的扩展开启 1,下 ...
- SpringBoot 试手(简易的SpringBoot搭建步骤)
SpringBoot 也算AI吧,它根据您架构中引用的依赖,自动化地按默认方案帮您完成了Spring那些复杂繁琐的配置工作.为了让您不会看低此 AI 水平,还特地喊出了“约定大于配置”的口号.从这个角 ...
- oracle数据库中存储过程使用MD5算法加密
一.技术点 1. DBMS_OBFUSCATION_TOOLKIT.MD5 DBMS_OBFUSCATION_TOOLKIT.MD5是MD5编码的数据包函数,但偶在使用select DBMS_OBFU ...
- CSS 图像
CSS 图像 <上一节下一节> 通过CSS可以控制图像的大小和对齐方式. 图像大小 虽然在HTML中,img标签有属性height.width设置高和宽,在工作中却使用得非常少,通常使用C ...
- azkaban 执行hive语句
#hivef.jobtype=commandcommand=hive -f test.sql #test.sql use default;drop table aztest;create table ...
- servlet中的请求响应与重定向区别
一.概念 请求响应(转发):将客户端请求转发另一个servlet或者jsp页面------------------------getRequestDispatcher()方法 重定向: 返回一个连接给 ...
- Zabbix告警脚本-微信
1.weixin.sh [root@iot-svndata02 bin]# cat weixin.sh #!/bin/bash ###SCRIPT_NAME:weixin.sh### ###send ...