Asp.Net MVC控制器:声明AOP与Spring.Net [英] Asp.Net MVC Controller: declarative AOP with Spring.Net

查看:290
本文介绍了Asp.Net MVC控制器:声明AOP与Spring.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有可能,那Spring.Net方面不Asp.Net控制器工作?

Is it possible, that Spring.Net Aspects don't work with Asp.Net Controller?

我要上配置控制器的动作方法交易,但该代理似乎没有触发。

I want to configure transactions on Action methods of Controllers but the proxy doesn't seem to trigger.

<object id="ControllerClassPointcut" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
  <property name="patterns">
    <list>
      <value>xxx.Controllers.CompanyController.*</value>
    </list>
  </property>
</object>

<aop:config>
  <aop:advisor pointcut-ref="ControllerClassPointcut" advice-ref="TxAdvice"/>
  <!-- TxAdvice taken from ServiceContext -->
</aop:config>

<tx:advice id="TxAdvice" transaction-manager="TransactionManager">
  <tx:attributes>
    <tx:method name="*" propagation="Required"/>
  </tx:attributes>
</tx:advice>

和CompanyController的行动方法是:

and the action method of the CompanyController is:

    [HttpPost]
    public virtual ActionResult Create(Guid id, CompanyonViewModel vm)
    {
       ...
    }

但我的意见并不虽然切入点是公认生效。如果我采取其他类比控制器切入点它的作品。

but I the advice does not take effect although the pointcut is recognized. If I take an other class than a controller as pointcut it works.

有关的一些方法的咨询工作。例如,对于二传手存储库。但Sprint.Net不承认这一行动方法创建被称为

for some methods the advice works. For example for the setter for the repository. But Sprint.Net does not recognize that the action method "Create" is called

候选人是:xxx.Controllers.CompanyController.set_CompanyService';图案为xxx.Controllers.CompanyController *';匹配= TRUE
  候选顾问[DefaultObjectFactoryPointcutAdvisor:切入点[Spring.Aop.Support.SdkRegularEx pressionMethodPointcut]。咨询对象='TxAdvice']接受TARGETTYPE [xxx.Controllers.CompanyController]

Candidate is: 'xxx.Controllers.CompanyController.set_CompanyService'; pattern is 'xxx.Controllers.CompanyController.*'; matched=True Candidate advisor [DefaultObjectFactoryPointcutAdvisor: pointcut [Spring.Aop.Support.SdkRegularExpressionMethodPointcut]; advice object = 'TxAdvice'] accepted for targetType [xxx.Controllers.CompanyController]

感谢您的帮助。

推荐答案

我与 [交易] 属性类似的问题(使用的作品 Spring.AOP )。
就我而言,我叫 [交易] 从同一个类中标记的方法和感到惊讶的是该交易的建议不火。

I had a similar a problem with the [Transaction] attribute (which works using Spring.AOP). In my case, I called the [Transaction] flagged methods from within the same class and was surprised that the transaction advice didn't fire.

的解释是,这调用 [交易] 标记在类的内部方法时,你持有一个参考实际情况,而不是AOP-代理的情况下,因此,通话不会被拦截。

The explanation was, that when calling a [Transaction] marked method from within the class, you hold a reference to the real instance instead of the AOP-proxied instance, therefore the call does not get intercepted.

当一个请求到MVC应用程序做,然后从请求URL的控制器选择(从 IControllerFactory 实例)。在此控制器,执行方法被调用,这又是负责调用的行动。所以,我认为的动作方法总是从控制器的内部调用。这意味着,通过定义动作方法将永远不会被截获。这可以解释为什么这些切入点的认可,但不火。

When a request is made to an MVC app, then from the request url a controller is chosen (from the IControllerFactory instance). On this controller, the Execute method is called, which in turn is responsible for calling the actions. So I think that action methods are always called from within the controller. Which means that by definition action methods will never get intercepted. This would explain why those pointcuts are recognized, but don't fire.

如果我采取其他类比
  控制器作为切入点它的工作原理

if I take an other class than a controller as pointcut it works

这也解释了为什么在其他班级比控制器切入点做火了:他们有可能从一个控制器,将举行一个AOP-代理参考其他类的实例名为

It also explains why pointcuts on other classes than controllers do fire: they are likely called from a controller, which will hold a AOP-proxied reference to instances of the other classes.

...一些方法建议工作的...例如,对于引领者
  库

... for some methods the advice works ... For example for the setter for the repository

我假设你(例如) CompanyController.CustomerController 有一个属性 CustomerRepository ,使用DI设置。这是有道理的,这个切入点火灾,因为二传是从外面叫 CompanyController.CustomerController ,例如通过您的DI容器(或的ControllerFactory )。

I assume that your (for instance) CompanyController.CustomerController has a property CustomerRepository, set using DI. It makes sense that this pointcut fires, because the setter is called from outside the CompanyController.CustomerController, for instance by your DI container (or your ControllerFactory).

一个解决办法是引入服务对象,在其上定义的交易建议,你现在对你的控制器。从你的控制器,你调用方法对这些服务对象 - 然后切入点将触发

A solution could be to introduce service objects, on which you define the transaction advice that you now have on your controllers. From your controllers, you call methods on these service objects - and then the pointcuts will fire.

这篇关于Asp.Net MVC控制器:声明AOP与Spring.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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