帮助和有关面向方面的编程 [英] Help and Information about Aspect Oriented Programming

查看:194
本文介绍了帮助和有关面向方面的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初来乍到面向方面编程的想法,但我想探讨使用它在我的项目的处理日志记录,报告等。为此的想法我有一些问题:

I'm a newcomer to the idea of aspect-oriented programming but I would like to explore the idea of using it on my project for handling logging, reporting, etc. To this end I have some questions:

  • 我应该麻烦探索AOP的这条道路,这些限制的目的?
  • 提供了哪些.NET框架支持AOP?
  • 在哪这些框架的支持流畅的界面(我讨厌XML配置):)

推荐答案

面向方面编程不仅仅是记录,报告等等,你会看到,如果你看看PostSharp这么多网站。我个人没有做过这么大的静态IL织,主要是动态的IL一代创建AOP拦截器和这样做的时候我主要是用它来包装和容器的控制反转拦截解决了。

Aspect Oriented Programming is so much more than just logging, reporting et cetera, as you will see if you have a look at PostSharp's web site. Personally I haven't done so much static IL weaving, mostly dynamic IL generation to create AOP interceptors and when doing so I have mostly been using it to wrap and intercept resolves from inversion of control containers.

AOP可以改善异常处理,提高跟踪,提高交易拦截。

AOP can improve exception handling, improve tracing, improve transaction interception.

NHibernate的例如有一种AOP的,即使它是静态的,在编译时在简单的事件处理程序的条件;但在发动机某些事件,您可以附加拦截器(又名方面,作为点切等事件) - 我用这个来注入,使用的IoC商业实体到我的域对象

NHibernate for example has a sort of AOP, even though it's static at compile-time in terms of simple event handlers; but for certain events in the engine you can attach interceptors (aka aspects, the events being the point-cuts etc) -- I use this to inject, using IoC business entities into my domain objects.

强大的AOP-框架允许你来概括,甚至更强大让你概括W / O开销在运行时;在原则上,你有这样做的几种不同的方法

Powerful AOP-frameworks allow you to generalize and even more powerful allow you to generalize w/o overhead at runtime; in principle you have a few different ways of doing it:

(0)。 (不是真的)pre处理器,在C ++ AOP又名模板的ifdef等

(0). (not really) "pre-processor" AOP aka templates in C++, ifdefs etc

  1. 在反思AOP
  2. IL-代在运行时通过Reflection.Emit的,需要很高的信任。这是在城堡项目的路径DynamicProxy2采取。 DynamicProxy2是相当不错,很多工作已经进入了它!此外,AFAIK PatternsAndPractices政策框架使用了这种方法为好,有很多的XML,但用自己的发电机。 NHibernate的有DynProx2的依赖。
  3. 要IL编译+的Assembly.Load(...)在运行时通过使用系统。codeDom.Compiler,然后加载您创建assmblies,需要高度信任。与任何其他编译器一样Boo.Compiler编译也有可能,因为它创造了全球功能组件,您可以在脚本的方式打电话,但是现在我们正在走出AOP的一点。
  4. 在探查的API(不要问我关于他们的)
  5. 依托运行时框架:扩展MarshalByRef / ContextBoundObject 见链接并在.NET中使用远程处理基础设施做AOP,这是相当复杂的,引进的依赖,你可能不想。
  6. 后编译静态IL-织造,PostSharp和Mono.Cecil能做到有Reflection.Emit的一个等效的,但在具体子类这一块没有错误的虚方法调用(如果我没记错的话),如Reflection.Emit的和会很高兴地检查你的code类似Assembly.ReflectionOnlyLoad,也将让你的输出IL-操作成code。这是一个很好的选择,如果你正在寻找一个相当低的水平的方法;不要求高信任。
  7. 在管理code的非托管回调到C / C ++,通过P /调用添加扩展点,但这需要一些思考作为例外不交叉米/ UM内存边界开心地(更确切地说,它会乱的应用起来),除非你正在使用Windows VC ++ / C#与托管异常框架这可能赛格故障相当严重。你可以通过回调到C和P / Invoke的为C,从C#和可能从C传递回调到C#,以及只要你定义在C#中的委托。扩展点很可能必须通过静态或动态的IL-韦弗+点切口来完成。
  1. Reflection "AOP"
  2. IL-generation at runtime through Reflection.Emit, requires high trust. This is the path DynamicProxy2 in the Castle project has taken. DynamicProxy2 is rather nice and lots of work has gone into it! Also, afaik PatternsAndPractices Policy Framework uses this approach as well, with lots of XML, although with their own generator. NHibernate has a dependency on DynProx2.
  3. To IL-compilation + Assembly.Load(...) at runtime through using System.CodeDom.Compiler, then loading your created assmblies, requires high trust. Compiling with any other compiler like Boo.Compiler is also possible as it creates "global function assemblies" which you can call in a 'scripted' way, but now we're moving out of AOP a bit.
  4. Profiler APIs (don't ask me about them)
  5. Relying on the runtime framework: extending MarshalByRef/ContextBoundObject see link and using the remoting-infrastructure in .Net to do AOP, which is quite complex and introduce dependencies you might not want.
  6. Post-compile static IL-weaving, PostSharp and Mono.Cecil has an equivalent of Reflection.Emit, but this one doesn't have bugs for virtual methods calls in concrete subclasses (if I remember correctly) like Reflection.Emit and will gladly inspect your code similar to Assembly.ReflectionOnlyLoad and will also allow you to output IL-operations into that code. This is a good candidate if you are looking for a rather low-level approach; doesn't require as high trust.
  7. Adding extension points in your managed code for unmanaged callbacks to C/C++ through p/invoke, but this requires some thought as exceptions don't cross m/um memory-boundaries happily (rather, it will mess your application up), and unless you're using VC++/C# in Windows with managed exception framework this could seg-fault quite badly. You can pass callback to C and p/invoke into C from C# and probably pass callbacks from C to C# as well as long as you define the delegate in C#. The extension points would probably have to be done through a static or dynamic IL-weaver + point-cuts.

用法在交易 看一看<一href="http://www.castleproject.org/container/facilities/trunk/atm/index.html">Castle.Facilities.AutomaticTransactionManagement.TransactionFacility为处理使用AOP交易一个很好的方式DynamicProxy2的拦截能力。本次交易的设施集成System.Transcations和的System.EnterpriseServices是您正在使用分布式事务处理协调器(COM组件)的管理事务。此外,还有是p的多个实例/调用到内核照顾的<一个href="http://channel9.msdn.com/shows/Going+Deep/Transactional-Vista-Kernel-Transaction-Manager-and-friends-TxF-TxR/">TxF与Vista的内核TxR还元件(又名Server 2008中),允许您使用事务的NTFS和注册表,从而确保CRUD你做的是ACID,这也很好地集成了System.Transactions的创建嵌套交易。

Usages in transactions Have a look at Castle.Facilities.AutomaticTransactionManagement.TransactionFacility for a nice way of handling transactions using AOP and the intercepting abilities of DynamicProxy2. The transaction facility integrated with System.Transcations and System.EnterpriseServices is you are using the distributed transaction coordinator (COM-component) for managing transactions. Also, there are multiple examples of p/invoke into the kernel to take care of the TxF and TxR components of the Vista-kernel (aka Server 2008) which allow you to use transactions on NTFS and on the registry, thereby making sure CRUD you do is ACID, which also nicely integrates with System.Transactions for creating nested transactions.

使用实例中不变的验证 您也可以使用他们的契约式设计,通过附加一些属性为您的参数。

Usages in invariant verification You can also use them for design by contract, by appending some attributes to your parameters.

public void PerformOperation([NotNull, NotEmpty] string value) {
// use string
[NotNull] return new string(' ', 5); // can return with attributes as well
}

目前的问题,这是安装这个元数据,并在运行时检查它的开销。但是,你可以指定约束检查方面,当你正在编译与调试,然后这个元数据,也不会转回去导致性能大大恶化才适用。

The problem with this at the moment is the overhead of attaching this meta-data and checking it at runtime. However, you could specify the constraint-checking aspect only to be applied when you're compiling with DEBUG and then this meta data-wouldn't lead to much deterioration in performance.

如果你正在寻找进入公理证明,看看星#/规格#代替,因为这是较为正式的工作是由编译器完成。

If you are looking to get into axiomatic proofs, have a look at Sing#/Spec# instead, since that's more formal and the work is done by the compiler.

事情要注意的 最重要的一点需要注意的是,如果一个问题,即一些片code运行之前或之后你的方法是改变控制流,也可能返回意外的类型,返回太早或一般没有按'牛逼的行为以及你打电话,你可能会得到错误,这是很难调试方法的意图。

Things to be aware of The most important point to be aware of is that if a concern, i.e. some piece of code that runs before or after your method is altering the control-flow, possibly returning an unexpected type, returning too early or in general doesn't behave along the intents of the method you were calling you may get errors which are hard to debug.

此外,要注意从属性的异常投掷的,因为你永远不知道什么时候或什么集会,反射occurrs;当你希望在你的属性的反映可能不会发生。这发生在我自己,当我连接类型的属性,并仔细地检查它们。

Also, beware of the throwing of exceptions from attributes, because you never know when or from what assembly, reflection occurrs; the reflection on your attribute may not happen when you expect to. This happened to myself when I was attaching types in attributes and carefully checking them.

另外要注意的是你的加入,如果有人被访问,可以用来重定向你的系统的大部分全球点 - 裁员打开一个可能的攻击向量的事实。

Also beware of the fact that you're opening a possible attack vector in adding global "point-cuts" which, if someone gets access to, can be used to redirect large portions of your system.

其他框架 如果您有兴趣了解更多有关AOP一般我建议你看看里卡德奥伯格的presentations在 Qi4J ,它在Java中的一个非常好的框架为AOP(Java有稍微不同的对象继承语义虽然这让一丁点特里克在C#中使用/ F#/ Nermle / BOO什么。

Other frameworks If you are interested in learning more about AOP in general I suggest you check out Rickard Öberg's presentations on Qi4J, it's a very good framework in Java for AOP (java has slightly different object-inherited semantics though which makes a wee bit tricker to use in C#/F#/Nermle/Boo whatever.

AOP +代码了 使用面向方面的编程与运行时生成组件另一个有趣的可能性,如dynamicproxy2创建,是你也可以使用它们来包装跨应用程序边界的对象,从而简化了创建一个外接流水线。我暗自希望,微软将利用这个时候,他们创造了他们的外接程序,框架3.5,但他们选择了去静态code根的方式遗憾的是,导致AA相当大的开销,创建插件,为开发。问题是,加载超过反射到一个AppDomain类型不能再卸载,除非完全的AppDomain被卸载,所以你需要1),以反映该插件无需加载它,看看它是什么能力,除非你允许大量的手工元数据写入或生成的(并且相信这一点); 2)一些对象来保存处理您的对象,以便它不是GCed,你不知道类型(因此IContract总成和AddInHandle级) - 这也许可以在一个动态代理/ AOP的一个很好的方式完成

AOP + AddIns Another interesting possibility in using aspect oriented programming with runtime-generated assemblies such as those dynamicproxy2 creates, is that you can also use them to wrap objects that cross application boundaries, thereby simplifying the creation of a add-in-pipeline. I had secretly hoped that Microsoft would use this when they created their AddIn-framework for 3.5, but they chose to go the static code-gen way unfortunately, leading a a rather big overhead in creating add-ins, for the developer. The problem is that a type loaded for "more than reflection" into an AppDomain can't be unloaded again unless the complete AppDomain is unloaded, so you need 1) to reflect on the plugin without loading it to see what it is capable of unless you allow for lots of manual meta-data to be written or generated (and believe in that) and 2) some object to hold the handle to your object so that it's not GCed and you don't know the type (hence the IContract assembly and AddInHandle-class) -- this could probably be done in a nice way with a dynamic proxy/AOP.

使用AOP全球垃圾回收 ...在Linux上运行的分布式系统/在公共语言基础设施建设的窗口。该文件是一个有点难以下载所以我上传到我的服务器,所以我知道它在哪里。

Using AOP for global garbage collection ... in a distributed system running on linux/windows on the common language infrastructure. The paper was a bit hard to download so I uploaded it to my server so I know where it is.

邮政Scriptum (如果您正在使用的CLR,而不是DLR IL-织可能会产生不符合标准的code。特别有趣的F#的非标准语言,我想,是因为使用的很多非标准code的语言有很大好处(元组说) - 你可以标记您的程序集[程序集:CLSCompliant]。如果你想获得编译时的这一警告)

Post Scriptum (If you are using a non-standard language on the CLR and not the DLR IL-weaving might create non-standards compliant code. Especially interesting for F#, I think, because the use a lot of non-standard code to great benefit of the language (tuples say) -- you could mark your assembly with [assembly: CLSCompliant] if you want to get compile-time warnings of this.)

这篇关于帮助和有关面向方面的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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