使用 C# 的 AOP 建议 [英] Advice on AOP with C#

查看:23
本文介绍了使用 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 中的 AOP 似乎有 4 个主要框架

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

  • Castle Windsor - 我一般都远离这个,因为它做了很多我真的不需要的东西
  • Spring.net - 听起来它有良好的记录,但通过其 xml 配置激发了恐惧(我不喜欢非流畅的配置)
  • PostSharp - 属性驱动(我喜欢这个)但在某一点有一些行号问题,不确定它们是否还存在
  • Unity - 很多我并不真正需要的东西
  • 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?

作为参考,我为 DI 选择 Autofac,因为它流畅、易于使用,找不到任何负面评论,而且它很管用.

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天全站免登陆