Delphi 中的 AOP 支持 [英] AOP support in Delphi

查看:25
本文介绍了Delphi 中的 AOP 支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 Delphi 中进行面向方面的编程?我对本机支持以及第三方解决方案感兴趣.

Is it possible to do Aspect Oriented Programming in Delphi? I would be interested in native support as well as third party solutions.

我没有想用 AOP 解决的具体问题,只是对学习 AOP 感兴趣.

I don't have a specific problem I want to solve with AOP, but am simply interested in studying AOP.

推荐答案

AOP 取决于两件事:

AOP depends on two things:

  • 能够将附加代码注入现有代码单元
  • 一种在应注入代码的位置设置条件的机制.

这通常称为代码编织.它是更大范围的程​​序转换研究中的一个专业.

This is commonly referred to as code weaving. It is a specialization within the larger study of program transformation.

JIT 编译语言比静态编译程序有更多的选项来实现代码编织,因为字节码/IL 中保留了更多信息.它们还支持反射,从而提供在运行时操作代码的能力.

JIT compiled languages have more options for implementing code weaving than statically compiled programs because more information is retained in the bytecode/IL. They also support reflection, which offers the ability to manipulate code at runtime.

Delphi.NET 和 Prism 可以像使用任何其他 .NET 语言一样访问这些功能.

Delphi.NET and Prism have the same access to these capabilities as any other .NET language.

我知道有两个适用于 Delphi Win32 的 AOP 框架.第一个是 MeAOP,前面已经提到过.第二个是Infra.这两个项目都采用了类似的 AOP 方法.它们结合使用 RTTI 和巧妙的指针操作来拦截方法调用,因此您可以在方法调用之前或之后运行其他代码.您将横切功能定义为框架的 AOP 类的子类.您可以通过将方法名称作为字符串参数传递给 AOP 框架来注册要拦截的方法.

There are two AOP frameworks for Delphi Win32 that I'm aware of. The first is MeAOP, which has already been mentioned. The second is Infra. Both projects take a similar approach to AOP. They use a combination of RTTI and clever pointer manipulation to intercept method calls so you can run additional code before or after the method call. You define your cross-cutting feature as a subclass of the framework's AOP class. You register the methods you want intercepted by passing the method name as a string argument to the AOP framework.

这两个框架仍在积极开发中,实际上范围比 AOP 更大.不幸的是,文档有点稀少(在 Infra 的情况下主要是葡萄牙语)

Both frameworks are still actively developed and are actually larger in scope than just AOP. Unfortunately documentation is somewhat sparse (and in Infra's case mostly in Portuguese)

另一个项目在 2004 年通过源代码编织尝试了 AOP,并取得了一些成功.基本上,他们在称为 DMS 的通用程序转换工具之上构建了一个方面编织器,并使用它在编译之前将代码注入到 delphi 源文件中.他们的面向方面语言主要受 AspectJ 的影响.

Another project attempted AOP through source code weaving back in 2004 with some success. Basically they built an aspect weaver on top of a general purpose program transformation tool called DMS and used it to inject code into delphi source files prior to compilation. Their aspect oriented language was primarily influenced by AspectJ.

http://www.gray-area.org/Research/GenAWeave/ 提供原始论文和演示文稿的链接,以及一些转换过程的视频.

http://www.gray-area.org/Research/GenAWeave/ has links to the original paper and presentation as well as some videos of the transformation process.

也可以使用运行时代码检测来完成此操作.它是一些分析器用来在不修改原始源代码的情况下将计数器和堆栈跟踪注入运行代码的技术.可以使用类似的技术将横切关注点注入静态编译的可执行文件中.PinTool 项目就是一个很好的例子.

It may also be possible to use runtime code instrumentation to accomplish this as well. Its a technique used by some profilers to inject counters and stack traces into running code without modifying the original source. A similar technique could be used to inject crosscutting concerns into a statically compiled executable. The PinTool project is a good example of this.

这篇关于Delphi 中的 AOP 支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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