关于使用C#进行AOP的建议 [英] Advice on AOP with C#

查看:392
本文介绍了关于使用C#进行AOP的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET 3.5 SP1 Webforms应用程序.我将MVP模式(监督控制器)与DI(autofac)一起使用.我的演示者需要调用在我的域(DDD)中定义的存储库合同,这些合同是在基础架构项目中实现的.

I have an ASP.NET 3.5 SP1 Webforms Application. I use the MVP pattern (supervising controller) with DI (autofac). My presenters call to the repository contracts defined in my Domain (DDD) which are implemented in an Infrastructure project.

演示者调用的存储库方法可能存在问题,因此我需要记录异常,然后在视图上设置错误消息.

The repository methods the presenters call can hork, so I need to log exceptions and then set an error message on the View.

过去,我会向Presenter构造函数添加另一个参数,将引用存储在Base Presenter中,并在我的Catch块中调用Log方法.我不是很喜欢,但是可以完成工作.

In the past I would have added another parameter to the Presenter constructors, stored the reference in a Base Presenter, and called a Log method in my Catch blocks. I don't really like this, but it gets the job done.

我可以使用工厂来获取日志记录类的方法如此处所述,但我想先探讨一下AOP,因为它看起来很有趣.

I could go the route of using a factory to get the logging class as described here, but I'd like to explore AOP first, as it seems pretty interesting.

我已经阅读了有关编译时与运行时AOP的文章,我想知道人们在不同解决方案,优点,缺点,建议之类的内容上的经验.

I have done the reading on compile-time vs runtime AOP, and I'd like to know what peoples' experiences are with the different solutions, pluses, minuses, words of advice, etc.

从我所做的挖掘看来,.NET中似乎有4个用于AOP的主要框架

From the digging I have done it seems like there are 4 main frameworks for AOP in .NET

  • 温莎城堡-我通常不这样做,因为它可以做很多我真的不需要的东西
  • Spring.net-听起来像是一个良好的记录,但是通过其xml配置激发了人们的恐惧(我不喜欢非流利的配置)
  • PostSharp-属性驱动(我喜欢这样),但在某一时刻出现了一些行号问题,不确定它们是否仍然存在
  • 团结-我真的不需要很多东西
  • Castle Windsor - I have stayed away from this in general because it does a whole lot of stuff I really don't need
  • Spring.net - sounds like it has a good track record but inspires fear through its xml configuration (I don't like non-fluent configurations)
  • PostSharp - Attribute driven (I like this) but at one point had some line number issues, not sure if they still exist
  • Unity - a whole lot of stuff I don't really need

我看到了另一个问题,尽管有一年半以前,它还是有一些不错的答案.在此期间是否开发了更新的更好"的框架,或者对现有解决方案进行了增强?

I see another question that has some good answers but its from a year and a half ago. Are there newer, "better", frameworks that have been developed in the meantime, or enhancements made to existing solutions that should be taken into consideration?

作为参考,我选择了Autofac for DI,因为它流利,易于使用,找不到关于它的任何负面评论,并且可以正常工作.

For reference, I chose Autofac for DI because its fluent, simple to use, couldn't find any negative comments about it, and it just works.

是否有关于应尝试使用的AOP框架的建议?感谢您阅读所有内容并添加任何想法.

Are there recommendations on which AOP framework I should try? Thanks for reading all of this and for adding any thoughts.

推荐答案

简短的回答,为了简单易用,PostSharp确实不会出错.

Short answer, for simplicity and ease of use, you really can't go wrong with PostSharp.

更长的答案:在我看来,您应该根据要实现的目标在两个框架之间进行选择.

Longer answer: In my mind, you should choose between the two frameworks depending on what you are trying to achieve.

如果要根据上下文更改方面,请考虑使用Spring.NET(或任何基于配置在运行时注入代码的aop框架).这使您可以根据自己的行为自定义对象的行为.例如,通过配置,您可以在控制台应用程序中使用一种登录方式,而在Web应用程序中使用另一种登录方式.请注意,Spring还是一个DI容器(以及其他一些东西),它远远超出了AOP,绝对值得学习如何使用.

If you want aspects that should change based upon context, consider Spring.NET (or any aop framework that injects code at runtime based on configuration). This allows you to customise the behaviour of your objects depending on what you are doing. For example, via your configuration, you may use one type of logging in a console app, and another in a web app. Note that Spring is also a DI container (and several other things) -- it goes well beyond AOP, and it is definitely worth learning how to use.

另一方面,如果您希望行为始终有效,无论上下文如何,那么PostSharp(编译时编织)是您的最佳选择.这实际上与在将方面应用到的每种方法中都包括代码一样.

On the other hand, if you want behaviour that should always be in effect, regardless of context, then PostSharp (compile time weaving) is your best bet. This is effectively the same as if you include the code in each method you apply the aspect to.

对于您正在做的事情,我建议您从PostSharp开始.

For what you're doing, I recommend you start with PostSharp.

这篇关于关于使用C#进行AOP的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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