Spring.Net 可以用作 PostSharp 吗? [英] Can Spring.Net function as PostSharp?

查看:31
本文介绍了Spring.Net 可以用作 PostSharp 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前我发现了 PostSharp,有一段时间,它很好.

A few months back I've discovered PostSharp, and for a time, it was good.

但是,legal 回复说他们不喜欢旧版本的许可证.然后部门告诉我 2.0 的价格高得令人无法接受(对于我们需要的座位数量)......我非常失望,但并不灰心.不可能是唯一的这样的框架,我想.

But then legal came back with an answer saying that they don't like the licence of the old versions. Then the department told me that 2.0's price was unacceptably high (for the number of seats we need)... I was extremely disapponted, but not disheartened. Can't be the only such framework, I thought.

我一直在寻找替代品,但其中大部分要么死了,维护不善(特别是在文档部门),用于学术用途,或以上所有(我在看你 Aspect.Net)

I kept looking for a replacement, but most of it was either dead, ill maintained (especially in documentation department), for academic use, or all of the above (I'm looking at you Aspect.Net)

然后我发现了 Spring.Net,有一段时间,它很好.

Then I've discovered Spring.Net, and for a time, it was good.

我一直在阅读文档,它继续描绘了一幅 AOP 必杀技的高级图景.我不再被锁定到属性来标记我希望代码拦截发生的位置,而是可以在 XML 中配置它,并且不需要重新编译对它的更改.太好了.

I've been reading the documentation and it went on to paint what seemed to be a supperior picture of an AOP nirvana. No longer was I locked to attributes to mark where I wanted code interception to take place, but it could be configured in XML and changes to it didn't require re-compile. Great.

然后我查看了示例,并在每个使用场景中看到了以下内容:

Then I looked at the samples and saw the following, in every single usage scenario:

// Create AOP proxy using Spring.NET IoC container.
IApplicationContext ctx = ContextRegistry.GetContext();
ICommand command = (ICommand)ctx["myServiceCommand"];    
command.Execute();
if (command.IsUndoCapable)
{
    command.UnExecute();
}

为什么前两行代码必须存在?它毁了一切.这意味着我不能简单地向用户提供一组方面和属性或 XML 配置,他们可以通过在适当的方法/类/等上粘贴适当的属性或在 XML 中编辑匹配模式来使用这些配置.他们必须修改他们的程序逻辑才能完成这项工作!

Why must the first two lines of code exist? It ruins everything. This means I cannot simply provide a user with a set of aspects and attributes or XML configs that they can use by sticking appropriate attributes on the appropriate methods/classes/etc or editing the match pattern in XML. They have to modify their program logic to make this work!

在这种情况下,有没有办法让 Spring.Net 表现得像 PostSharp?(即用户只需要添加属性/XML 配置,不需要编辑任何方法的内容.

Is there a way to make Spring.Net behave as PostSharp in this case? (i.e. user only needs to add attributes/XML config, not edit content of any methods.

或者,是否有 PostSharp 的有价值且实用的替代品?我在 SO 上看到了一些类似这样的问题,但他们中没有一个真正想取代 PostSharp,他们只是想补充它的功能.我需要完全更换.

Alternatively, is there a worthy and functioning alternative to PostSharp? I've seen a few question titled like this on SO, but none of them were actually looking to replace PostSharp, they only wanted to supplement its functionality. I need full replacement.

推荐答案

简而言之,是的,Spring.Net AOP 可以按照您使用基于 XML 的配置描述的方式工作:您不必使用最初的两行代码,实际上不鼓励基于代码的配置.您只能使用基于 XML 的配置来配置 Spring.Net AOP,这实际上是推荐的方法.

In short, yes, Spring.Net AOP can work in the way you describe using XML-based configuration: you do not have to use those initial two lines of code, in fact code-based configuration is discouraged. You can configure Spring.Net AOP using XML-based configuration only and this is in fact the recommended approach.

有几个步骤:

  1. 创建您的建议:BeforeAdvice、AroundAdvice、AfterReturningAdvice 和 ThrowsAdvice 是支持的建议类型.AroundAdvice 使用 AOPAlliance 接口,其他使用 Spring.AOP 接口.
  2. 定义切入点
  3. 应用切入点和建议

示例配置(从实时配置中概括而来):

Example configuration (generalized from a live configuration):

  <!-- START Spring.Net AOP -->

  <object id="beforeAdvice" type="MyBeforeAdvice, MyAOP"></object>
  <object id="beforeAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop">
    <property name="Advice" ref="beforeAdvice" />
  </object>

  <object id="returnsAdvice" type="MyAfterReturningAdvice, MyAOP"></object>
  <object id="returnsAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop">
     <property name="Advice" ref="returnsAdvice" />
  </object>

  <object id="throwsAdvice" type="MyThrowsAdvice, MyAOP"></object>
  <object id="throwsAdvisor" type="Spring.Aop.Support.DefaultPointcutAdvisor, Spring.Aop">
    <property name="Advice" ref="throwsAdvice" />
  </object>


  <!-- Advise objects -->
  <object type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
    <property name="ObjectNames">
      <list>
        <value>*Command</value>
        <value>...</value>
      </list>
    </property>
    <property name="InterceptorNames">
      <list>
        <value>beforeAdvisor</value>
        <value>returnsAdvisor</value>
        <value>throwsAdvisor</value>
      </list>
    </property>
  </object> 


  <!-- END Spring.Net AOP -->

织入是在运行时执行的,速度非常快且不干扰.

Weaving is performed at runtime and is pretty fast and unintrusive.

希望有用,

安德鲁

这篇关于Spring.Net 可以用作 PostSharp 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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