能否Spring.Net功能PostSharp?功能、Spring、Net、PostSharp

2023-09-03 23:13:58 作者:小疯°子

几个月前,我发现PostSharp,和今后一个时期,这是很好的。

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

但后来合法带回来的回答说他们不喜欢旧版本的许可证。然后,该部门对我说2.0的价格是不能接受的(对我们需要的座位数)......我非常disapponted,但不灰心。不能是唯一的这样的框架,我想。

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必杀技的supperior图片。不再是我锁定的属性标记的地方,我想code截取的发生,但它可以在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();
}

为什么一定要第一个两行code存在吗?它破坏了一切。这意味着我不能简单地给用户提供了一组方面和属性或XML的configs,他们可以通过坚持适当的属性上相应的方法/类/等或编辑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?我见过的题目是这样的这么几个问题,但他们都不是真正寻求替换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的配置方式工作:您不必使用这些最初的两行code,其实code型配置气馁。可以配置Spring.Net AOP只使用基于XML的配置,其实这是推荐的方法。

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.

有几个步骤是:

创建你的意见:BeforeAdvice,AroundAdvice,AfterReturningAdvice和ThrowsAdvice支持意见的类型。 AroundAdvice使用AOPAlliance接口,其他使用Spring.AOP接口。 定义你的切入点 应用的切入点和通知

实例配置(从现场配置广义):

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 -->

织造是在运行时进行,是pretty的快速,非侵入的。

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

希望这是使用,

安德鲁