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是面向切面编程,它能够使我们在不改变现有代码结构的情况下额外的为其添加一些功能. 我们还是使用配置文件来对类型进行注入 ...
随机推荐
- 关于php查询mongodb限制返回字段的问题
最近想做一个前端控制接口字段返回的一个基础方法,通过mongodb 的find($query,$field)查询来规定查询的字段,但是遇到这么一个问题: 工作代码中有两个封装方法 : /** * 查询 ...
- html背景图星际导航图练习
html <body> <div class="box1"> <div></div> ...
- Codeforces Round #437 B. Save the problem!
题意: 给你一个方案数,要求你输出满足该条件的总金额,面值数,和各个面值是多少,答案有多个,随便输出一个即可. Examples Input 18 Output 30 41 5 10 25 Input ...
- java正则积累
1. [.]点:再分割的时候不可以直接使用点,需要加上 \\ 转义才可以得到想要的结果,否则输出的时候会报异常 数据下标越界 String[] split = "output.txt&quo ...
- PowerScript语言基础
注释: 以 "//" 开头,其后书写注释内容,常用于单行注释. "/-/"中间的部分为注释,便于多行说明. //这是一个单行注释 INTEGER I I = I ...
- 转)Ubuntu16.04下安装DDD(Data Display Debugger)
以下转自:http://www.linuxdiyf.com/linux/26393.html 前两天在Linux论坛偶然间看到了DDD这个软件,根据介绍是一个gdb界面化的调试软件,这正是我找了好 ...
- WordPress 自动初始化数据库
背景 自动化搭建开发环境.测试.部署如通过网页操作(访问 /wp-admin/install.php)相对比较麻烦且在有的场景无法实现. 步骤 修改 wp-config.php 配置 wordpres ...
- [leetcode]54. Spiral Matrix螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- [leetcode]80. Remove Duplicates from Sorted Array II有序数组去重(单个元素可出现两次)
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- Mybatis配置问题解决Invalid bound statement (not found)
首先这个异常的原因是系统根据Mapper类的方法名找不到对应的映射文件. 网上也搜索了到了类似的文章,一般可以从以下几个点排查: mapper.xml的namespace要写所映射接口的全称类名,而且 ...