应用面向方面的编程 [英] Applying Aspect Oriented Programming

查看:30
本文介绍了应用面向方面的编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一些基本的 AOP 样式解决方案来解决跨领域问题,例如安全性、日志记录、验证等.我的解决方案围绕着 Castle Windsor 和 DynamicProxy,因为我可以使用基于 Boo 的 DSL 应用所有内容,并保持我的代码中没有属性.我在周末被告知要查看 PostSharp,因为它应该是一个更好"的解决方案.我快速浏览了 PostSharp,但我对 Attribute 的用法感到厌烦.

I've been using some basic AOP style solutions for cross-cutting concerns like security, logging, validation, etc. My solution has revolved around Castle Windsor and DynamicProxy because I can apply everything using a Boo based DSL and keep my code clean of Attributes. I was told at the weekend to have a look at PostSharp as it's supposed to be a "better" solution. I've had a quick look at PostSharp, but I've been put off by the Attribute usage.

有没有人尝试过这两种解决方案并愿意分享他们的经验?

Has anyone tried both solutions and would care to share their experiences?

推荐答案

我只看了很短时间的 Castle-windsor(还)所以我不能对此发表评论,但我确实使用了 postsharp.

I only looked at castle-windsor for a short time (yet) so I can't comment on that but I did use postsharp.

Postsharp 通过在编译时编织来工作.它在您的构建中添加了一个编译后步骤,它会在其中修改您的代码.代码的编译就像您刚刚将横切关注点编程到您的代码中一样.这比运行时编织的性能要高一些,并且由于使用属性 Postsharp 非常易于使用.我认为将属性用于 AOP 不像将其用于 DI 那样有问题.但这只是我个人的口味.

Postsharp works by weaving at compile time. It ads a post-compile step to your build where it modifies your code. The code is compiled as if you just programmed the cross cutting concerns into you code. This is a bit more performant than runtime weaving and because of the use of attributes Postsharp is very easy to use. I think using attributes for AOP isn't as problematic as using it for DI. But that's just my personal taste.

但是……

如果你已经使用了 Castle 进行依赖注入,我看不出你为什么不应该将它用于 AOP 内容的充分理由.我认为虽然运行时的 AOP 比编译时慢一点,但它也更强大.AOP 和 DI 在我看来是相关的概念,所以我认为对两者使用一个框架是个好主意.所以我可能会在下一个需要 AOP 的项目中再次查看城堡的东西.

If you already use castle for dependency injection I don't see a good reason why you shouldn't also use it for AOP stuff. I think though the AOP at runtime is a bit slower than at compile time it's also more powerful. AOP and DI are in my opinion related concepts so I think it's a good idea to use one framework for both. So I'll probably look at the castle stuff again next project I need AOP.

这篇关于应用面向方面的编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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