AspectF(Fluent Aspect Framework)是不是一种类似AOP的设计,可以不用太担心呢?
Omar Al Zabir 正在寻找“一种更简单的 AOP 风格编码方法”。
他创建了一个名为 AspectF 的框架,这是“一种将方面添加到您的代码”。
这不是真正的 AOP,因为它不做任何编译时或运行时编织,但它是否实现了与 AOP 相同的目标?
以下是 AspectF 用法的示例:
public void InsertCustomerTheEasyWay(string firstName, string lastName, int age,
Dictionary<string, string> attributes)
{
AspectF.Define
.Log(Logger.Writer, "Inserting customer the easy way")
.HowLong(Logger.Writer, "Starting customer insert", "Inserted customer in {1} seconds")
.Retry()
.Do(() =>
{
CustomerData data = new CustomerData();
data.Insert(firstName, lastName, age, attributes);
});
}
以下是作者的一些帖子,进一步阐明了 AspectF 的目标:
- AspectF 流畅的方式将方面放入代码中以实现关注点分离(博客)
- AspectF (谷歌代码)
- AspectF 流畅地添加方面以实现更简洁的可维护代码 (CodeProject)
根据作者的说法,我认为 AspectF 的设计并不像AOP 的替代品,而是一种实现“关注点分离并保持代码整洁”的方法。
一些想法/问题:
- 随着项目的发展,对使用这种编码风格有什么想法吗?
- 它是一个可扩展的架构吗?
性能问题?- 可维护性与真正的 AOP 解决方案相比如何?
Omar Al Zabir is looking for "a simpler way to do AOP style coding".
He created a framework called AspectF, which is "a fluent and simple way to add Aspects to your code".
It is not true AOP, because it doesn't do any compile time or runtime weaving, but does it accomplish the same goals as AOP?
Here's an example of AspectF usage:
public void InsertCustomerTheEasyWay(string firstName, string lastName, int age,
Dictionary<string, string> attributes)
{
AspectF.Define
.Log(Logger.Writer, "Inserting customer the easy way")
.HowLong(Logger.Writer, "Starting customer insert", "Inserted customer in {1} seconds")
.Retry()
.Do(() =>
{
CustomerData data = new CustomerData();
data.Insert(firstName, lastName, age, attributes);
});
}
Here are some posts by the author that further clarify the aim of AspectF:
- AspectF fluent way to put Aspects into your code for separation of concern (Blog)
- AspectF (google code)
- AspectF Fluent Way to Add Aspects for Cleaner Maintainable Code (CodeProject)
According to the author, I gather that AspectF is not designed so much as an AOP replacement, but a way to achieve "separation of concern and keep code nice and clean".
Some thoughts/questions:
- Any thoughts on using this style of coding as project grows?
- Is it a scalable architecture?
performance concerns?- How does maintainability compare against a true AOP solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我并不是要抨击这个项目,但是
恕我直言,这是滥用 AOP。方面并不适合所有情况,这样使用只会妨碍可读性。
此外,我认为这错过了 AOP 的要点之一,即能够在不触及底层代码的情况下添加/删除/重新定义方面。
方面应该在受影响的代码之外定义,以使它们真正横切关注点。在 AspectF 的情况下,这些方面嵌入在受影响的代码中,这违反了SoC< /a>/SRP。
在性能方面,没有任何损失(或者可以忽略不计),因为没有运行时 IL 操作,正如代码项目文章中所解释的那样。然而,我在使用 Castle DynamicProxy 时从未遇到过任何性能问题。
I don't mean to bash the project, but
IMHO this is abusing AOP. Aspects are not suitable for everything, and used like this it only hampers readability.
Moreover, I think this misses one of the main points of AOP, which is being able to add/remove/redefine aspects without touching the underlying code.
Aspects should be defined outside of the affected code in order to make them truly cross-cutting concerns. In AspectF's case, the aspects are embedded in the affected code, which violates SoC/SRP.
Performance-wise there is no penalty (or it's negligible) because there is no runtime IL manipulation, just as explained in the codeproject article. However, I've never had any perf problems with Castle DynamicProxy.
在最近的一个项目中,有人建议我尝试一下 AspectF。
我牢记这样的想法:把所有的关注点都放在前面,让代码幸福地完成真正的工作,而不需要意识到它之外发生的所有检查和平衡。
实际上,我更进一步,添加了一个安全“问题”,即需要作为 WCF 请求的一部分接收的凭据。它进入数据库并执行其必须执行的操作。在运行返回所需数据的实际代码之前,我进行了明显的验证和安全检查。
我发现这种方法相当令人耳目一新,而且我当然喜欢在调试和测试服务调用时可以浏览 AspectF 的源代码。
在办公室里,一些人认为这些问题应该作为类/方法的装饰来实现。但你在哪里装饰它并不重要,在某个地方的某个时刻,你需要说你希望执行某些操作/检查。我喜欢这样一个事实:它全部就地布局,而不是作为另一个代码文件,不是某种配置文件,并且这一次,没有向类/方法添加另一个装饰。
我并不是说这是真正的 AOP - 我当然认为在某些解决方案和情况下它确实不是实现目标的最佳方式,但考虑到它只是几 K 的源文件,这使得轻量级实施。
AspectF 基本上是一种将委托链接在一起的非常聪明的方法。
我不认为每个开发人员都会查看代码并说它看起来多么美妙,事实上,在我们的办公室,它让我们中的一些人感到困惑!但是,一旦您了解了发生的情况,就会发现这是一种廉价的方法,可以实现其他方法也可以完成的大部分工作。
On a recent project, it was recommended to me that I give AspectF a try.
I took to heart the idea of laying all the concerns up front, and having the code that does the real work blissfully unaware of all the checks and balances that happened outside of it.
I actually took it a little further, and added a security "concern" that required credentials that were being received as part of a WCF request. It went off to the database and did what it had to. I did obvious validations, and the security check before running the actual code that would return the required data.
I found this approach quite a refreshing change, and I certainly liked that I had the source of AspectF to walk through as I was debugging and testing the service calls.
In the office, some argued that these concerns should be implemented as a decoration on a class / method. But it doesn't really matter where you decorate it, at some point somewhere, you need to say you wish to perform certain actions / checks. I like the fact that it's all laid out in-place, not as another code file, not as some kind of configuration file, and for once, not adding yet another decoration to a class / method.
I'm not saying it's true AOP - and I certainly think there are solutions and situations where it really isn't the best way of implementing your objectives, but given that it's just a couple of K of source files, that makes for a very light-weight implementation.
AspectF is basically a very clever way of chaining delegates together.
I don't think every developer is going to look at the code and say how wonderful it is to look at, indeed in our office it confused some of us! But once you understand what's going on, it's an inexpensive way of achieving much of what can be done by other approaches too.