Autofac和DynamicProxy2搭配实现Aop动手训练

时间:2021-10-27 00:25:12

http://www.cnblogs.com/zhengwl/p/5433181.html

Aop含义:aspect-oriented programming

  • 实现工具介绍

  Autofac是一个比较流行的Ioc组件,DynamicProxy2是 the Castle Project core的一部分,提供了一个拦截框架

  • 组件安装和实现步骤

1.先打开vs新建一个web项目,打开nuget执行安装命令如下图所示

Autofac和DynamicProxy2搭配实现Aop动手训练

2.配置autofac

在Global.asax文件中在添加如下代码

1

2

3

4

5

6

7

public class Global : HttpApplication, IContainerProviderAccessor

{

static IContainerProvider _containerProvider;

// Instance property that will be used by Autofac HttpModules   // to resolve and inject dependencies.

public IContainerProvider ContainerProvider { get { return _containerProvider; } }

}

在Application_Start方法中配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

void Application_Start(object sender, EventArgs e)

{

// 在应用程序启动时运行的代码

RouteConfig.RegisterRoutes(RouteTable.Routes);

BundleConfig.RegisterBundles(BundleTable.Bundles);

// Build up your application container and register your dependencies.    

var builder = new ContainerBuilder();

// Once you're done registering things, set the container     // provider up with your registrations.  

builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());

builder.RegisterType<Calculator>().EnableClassInterceptors().InterceptedBy(typeof(CallLogger)).As<ICalculator>();        

builder.RegisterType<CallLogger>();

var container = builder.Build();

_containerProvider = new ContainerProvider(container);

}

3.aop示例代码如下包含一个接口,一个实现类和一个拦截类

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

public interface ICalculator

{

int Add(int lhs, int rhs);

int Multiply(int lhs, int rhs);

}

[Intercept(typeof(CallLogger))]

public class Calculator : ICalculator

{

public virtual int Add(int lhs, int rhs)

{

return lhs + rhs;

}

public virtual int Multiply(int lhs, int rhs)

{

return lhs * rhs;

}

}

public class CallLogger : IInterceptor

{

TextWriter _output = HttpContext.Current.Response.Output;

//public CallLogger(TextWriter output)

//{

//    _output = output;

//}

public void Intercept(IInvocation invocation)

{

_output.Write("Calling method {0} with parameters {1}... ",

invocation.Method.Name,

string.Join(", ", invocation.Arguments.Select(a => (a ?? "").ToString()).ToArray()));

invocation.Proceed();

_output.WriteLine("Done: result was {0}.", invocation.ReturnValue);

}

}

  global文件中的代码已经写完,其引用应该包括如下几个

using Autofac;
using Autofac.Extras.DynamicProxy2;
using Autofac.Integration.Web;
using Castle.Core;
using Castle.DynamicProxy;

4.效果展现,在Page页添加如下代码,然后运行浏览。

Autofac和DynamicProxy2搭配实现Aop动手训练

 public partial class _Default : Page
{
//public ICalculator handler { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
var cpa = (IContainerProviderAccessor)HttpContext.Current.ApplicationInstance;
var cp = cpa.ContainerProvider;
var handler = cp.RequestLifetime.Resolve<ICalculator>();
var test = handler.Add(1, 33);
Response.Write(test.ToString());
}
}

Autofac和DynamicProxy2搭配实现Aop动手训练

Autofac和DynamicProxy2搭配实现Aop动手训练

训练结束,

参考文档http://docs.autofac.org/en/v3.5.2/advanced/interceptors.html#enabling-interception