使用AOP技术拦截ADO.Net [英] Using AOP techniques to intercept ADO.Net

查看:27
本文介绍了使用AOP技术拦截ADO.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相当大的代码库,使用各种不同的 ADO 技术(即一些 EF,在某些情况下直接使用 ADO.Net).

I have quite a large code base using a variety of different ADO technologies (i.e. some EF and in some cases using ADO.Net directly).

我想知道是否有任何方法可以全局拦截任何 ADO.Net 调用,以便我可以开始审核信息,例如执行的确切 SQL 语句、花费的时间、返回的结果等.

I'm wondering if there is any way to globally intercept any ADO.Net calls so that I can start auditing information like, exact SQL statements that executed, time taken, results returned, etc.

主要想法是,如果我能做到这一点,我就不必更改任何现有代码,而且我应该能够拦截/包装 ADO.Net...这可能吗?

The main idea being that if I can do this, I shouldn't have to change any of my existing code and that I should be able to just intercept/wrap the ADO.Net... Is this possible?

编辑

有人建议我查看 PostSharpCciSharpAfterthought,但我如何使用其中之一来获得所需的结果?

Its been suggested that I look into PostSharp, CciSharp or Afterthought, but how do I use one of these to get the desired results?

推荐答案

没有.如果您直接针对 ADO.NET 类型进行编码,您不能只告诉 DynamicProxy:嘿,在所有这些 DLL 中我使用 SqlConnection 的任何地方放置一个代理".DynamicProxy 是一个运行时代理生成器.看起来你想要编译后 AOP,所以看看PostSharpCciSharpAfterthought 代替.

No. If you're coding directly against ADO.NET types you can't just tell DynamicProxy: "hey, put a proxy wherever I use SqlConnection in all these DLLs". DynamicProxy is a runtime proxy generator. It seems that you want post-compilation AOP, so look into PostSharp, CciSharp or Afterthought instead.

如果这些工具都不够用,您可以使用 Mono.Cecil 直接更改您的 IL,但这并不容易.参见示例:

if none of those tools are enough, you could use Mono.Cecil to directly alter your IL, but it's not easy. See for example:

这篇关于使用AOP技术拦截ADO.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆