C# (.Net) 的面向方面编程 (AOP) 解决方案及其特性 [英] Aspect Oriented Programing (AOP) solutions for C# (.Net) and their features

查看:28
本文介绍了C# (.Net) 的面向方面编程 (AOP) 解决方案及其特性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里询问 3 个信息:

I would like to ask for 3 information here:

  1. 没有针对面向方面编程 (AOP) 的集成解决方案来自 Microsoft 是对的吗?是否有任何此类解决方案正在开发或计划中?

  1. There is no integrated solution for Aspect Oriented Programing (AOP) in C# (.Net) from Microsoft is that correct ? Is there any such solution under development or planned ?

有哪些解决方案允许在 C# (.Net) 中使用面向方面编程 (AOP)?它们是什么优点/缺点?我还没有找到任何包含所有可用选项和一些信息的综合列表,供我决定使用哪个选项.最接近的是这个列表.

What solutions are there that allow Aspect Oriented Programing (AOP) to be used in C# (.Net) ? What are they advantages/disadvantages ? I haven't find any comprihensive list that would contain all avatable options and some information for me to decide which is the one to use. The closest is this list.

考虑以下标准:对于 C#(.Net)最佳 AOP 解决方案是什么(在您看来):>

What is (in your opinion) the best AOP solution for C#(.Net) considering following criteria:

  1. 它的工作方式应该与 AspectJ 类似,并且具有类似的语法
  2. 使用简单:不需要 XML 配置 - 只需编写一些常规类、一些方面类并编译以将它们编织在一起,然后运行.
  3. 应包括 AspectJ 的所有功能.支持泛型.
  4. 解决方案应该稳定、广泛使用和维护.
  5. 应该提供二进制文件(因此可以使用)或 C# 源代码的编织.
  6. 用于可视化的 GUI 工具(甚至更好 - VS 插件)是一个优势.

我认为如果某些东西满足 3. 中的大部分标准,那么它就是一个通用解决方案的候选者.如果某些现有解决方案符合我的需求,我找不到任何地方.

I think that if something fullfils most of criteria in 3. then it is a candidate for a generaly used solution. And I cannot find anywhere if some of existing solutions fits to my needs.

推荐答案

正如 Adam Rackis 所指出的,Post# 是要走的路,它是您将在 .NET 平台上接近 AspectJ 的方法.

As Adam Rackis points out, Post# is the way to go, it is as close you will get to AspectJ on the .NET platform.

主要区别显然是 AspecJ 具有方面的语言支持,而 Post# 是 .NET 程序集的后编译编织器.(因此没有语言集成)

Main differences is obviously that AspecJ has language support for aspects while Post# is a post compile weaver for .NET assemblies. (thus no language integration)

但是,Post# 可以使用连接点,例如字段访问、try catch 块、调用和函数(即调用者和被调用者)

However, Post# can use join points such as field access, try catch blocks, both calls and functions (that is, caller and callee)

  1. 不甚至接近,AspectJ 是一种语言,Post# 可以使用自定义切入点但最常见的是使用属性来装饰要切入点的方法(eh..grammar?)

  1. No not even close, AspectJ is a language, Post# can use custom pointcuts but the most common is to use attributes to decorate methods to be pointcutted(eh..grammar?)

检查

除了语言支持之外的一切

everything but language support

检查

check - 这是一个编译后编织器

check - it is a post compile weaver

有限,weaver 将生成智能感知信息并显示哪些方法受到影响

limited, the weaver will generate intellisense information and show what methods have been affected

如果您想要一种支持方面的 .NET 语言,请查看 http://aspectsharpcomp.sourceforge.net/samples.htm

If you want a .NET language that supports aspects, check out http://aspectsharpcomp.sourceforge.net/samples.htm

关于不同的方法,有几个:

Regarding different approaches, there are a few:

  1. Post compile weaving ,这就是 Post# 所做的.它只是破坏 .NET 程序集并注入方面代码.

  1. Post compile weaving , this is what Post# does. It simply mangles the .NET assembly and injects the aspect code.

真正的代理/MarshallByRefObject.基于远程基础设施.要求您的类从基类继承.性能极差,没有自我拦截"

Real Proxy / MarshallByRefObject. Based on remoting infrastructure. Requires your classes to inherit from a base class. Extremely bad performance and no "self interception"

动态代理.这是我的旧库 NAspect 使用的.您可以使用工厂来创建要应用方面的类型的子类.子类将使用接口添加混合代码并覆盖虚拟方法并注入拦截器代码.

Dynamic Proxy. This is what my old library NAspect used. you use a factory to create a subclass of the type on which you want to apply aspects. The subclass will add mixin code using interfaces and override virtual methods and inject interceptor code.

源代码编织.顾名思义,它会在编译之前转换您的源代码.

Source code weaving. As the name implies, it transforms your source code before compilation.

[edit] 我忘了把这个添加到列表中:

[edit] I forgot to add this one to the list:

  1. 接口代理类似于动态代理,但不是将拦截代码应用于子类,而是将拦截代码添加到运行时生成的接口代理中.也就是说,您获得一个实现给定接口的对象,然后该对象将每个对任何接口方法的调用首先委托给 AOP 拦截代码,然后将调用委托给实际对象.也就是说,这里有两个对象在起作用,代理和主体(您的真实对象).

客户端 ->接口代理 ->AOP拦截->目标/主题

这就是 Spring 所做的 AFAIK.

This is AFAIK what Spring does.

1) 和 3) 是最常见的.它们各有优缺点:

1) and 3) are the most common. They both have pros and cons:

后期编译:

优点:

  • 几乎可以点切所有内容,静态的、密封的、私有的
  • 仍然可以使用new"创建对象

缺点:

  • 不能基于上下文来应用切面,即如果一个类型受到影响,它将影响整个应用程序.

  • Can not apply aspects based on context, that is , if a type is affected, it will be affected for the entire application.

切入点私有的、静态的、密封的构造可能会导致混淆,因为它违反了基本的面向对象规则.

Pointcutting private, static, sealed constructs may lead to confusion since it breaks fundamental OO rules.

动态代理:

优点:

  • 上下文,一种类型可以根据上下文应用不同的方面.

  • Contextual, one typ can have different aspects applied based on context.

易于使用,无需配置或构建步骤.

Easy to use, no configuration or build steps.

缺点:

  • 有限切入点,只能拦截接口成员和虚拟成员

  • Limited pointcuts, only interface members and virtual members can be intercepted

必须使用工厂来创建对象

must use factory to create objects

这篇关于C# (.Net) 的面向方面编程 (AOP) 解决方案及其特性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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