AOP 基础知识

发布于 2024-10-14 11:49:29 字数 413 浏览 3 评论 0 原文

面向方面的编程是一个我很难找到任何好的信息的主题。我的旧软件工程教科书只简单地(含糊地)提到它,维基百科和我在它上面找到的各种其他教程/文章给出了超学术的、高度抽象的定义,说明它是什么、如何使用它,以及何时使用它。我似乎不明白定义。

我对 AOP 的理解(非常差)是,生成高质量软件系统的许多方面都不能完全融入到一个漂亮的小内聚包中。有些类,例如 Loggers、Validators、DatabaseQueries 等,将在整个代码库中使用,因此将是高度耦合的。我(再次非常)对 AOP 的理解是,它涉及如何处理这些类型的“通用耦合”包的最佳实践。

问题:这是真的吗,还是我完全不对劲?如果我完全错了,有人可以对 AOP 是什么给出一个简洁、通俗的解释,即所谓的方面的示例,甚至可以提供一个简单的代码示例吗?

Aspect-oriented programming is a subject matter that has been very difficult for me to find any good information on. My old Software Engineering textbook only mentions it briefly (and vaguely), and the wikipedia and various other tutorials/articles I've been able to find on it give ultra-academic, highly-abstracted definitions of just what it is, how to use it, and when to use it. Definitions I just don't seem to understand.

My (very poor) understanding of AOP is that there are many aspects of producing a high-quality software system that don't fit neatly into a nice little cohesive package. Some classes, such as Loggers, Validators, DatabaseQueries, etc., will be used all over your codebase and thus will be highly-coupled. My (again, very poor) understanding of AOP is that it is concerned with the best practices of how to handle these types of "universally-coupled" packages.

Question : Is this true, or am I totally off? If I'm completely wrong, can someone please give a concise, laymen explanation for what AOP is, an example of a so-called aspect, and perhaps even provide a simple code example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

维持三分热 2024-10-21 11:49:29

关注点分离是软件开发中的基本原则,David Parnas 有一篇经典论文 关于将系统分解为模块的标准,可能会向您介绍该主题,还可以阅读 鲍勃叔叔的坚定原则

但是,许多用例中可能包含交叉问题,例如身份验证、授权、验证、日志记录、事务处理、异常处理、缓存等,这些用例产生了软件中的所有层。如果你想在不重复的情况下解决问题并采用 DRY 原则,你必须以复杂的方式处理它。

您必须使用声明式编程,即在 .net 中可以通过属性注释方法或属性,然后根据这些注释更改运行时代码的行为。

您可以在 Sommerville 的软件工程书籍

有用链接
C2 wiki CrossCuttingConcernMSDN, 如何解决面向方面的软件开发中的横切问题

Separation of Concerns is a fundamental principle in software development, there is a classic paper by David Parnas On the Criteria To Be Used in Decomposing Systems into Modules that may introduce you to the subject and also read Uncle Bob's SOLID Principles.

But then there are Cross Cutting concerns that might be included in many use cases like authentication, authorization, validation, logging, transaction handling, exception handling, caching, etc that spawn all the layers in software. And if you want to tackle the problem without duplication and employing the DRY principle, you must handle it in a sophisticated way.

You must use declarative programming, that simply in .net could be annotating a method or a property by an attribute and what happened later is changing the behavior of code in runtime depending of those annotations.

You can find a nice chapter on this topic in Sommerville's Software engineering book

Useful links
C2 wiki CrossCuttingConcern, MSDN, How to Address Crosscutting Concerns in Aspect Oriented Software Development

温暖的光 2024-10-21 11:49:29

AOP 是一种技术,我们可以将代码中的横切关注点(日志记录、异常处理……)提取并删除到它自己的方面。让我们的原始代码只关注业务逻辑。这不仅使我们的代码更具可读性、可维护性,而且代码是 DRY 的。

这可以通过一个例子更好地解释:


使用 Castle Windsor 在 .net 世界中进行面向方面编程 (AOP)
或者
. 中的面向方面编程 (AOP)使用Unity的网络世界

AOP is a technique where we extract and remove the cross cutting concerns (logging, Exception handling, ....) from our code into it’s own aspect. leaving our original code focusing only on the business logic. not only this makes our code more readable, maintainable but also the code is DRY.

This can be better explained by an example:

Aspect Oriented Programming (AOP) in the .net world using Castle Windsor
or
Aspect Oriented Programming (AOP) in the .net world using Unity

橘味果▽酱 2024-10-21 11:49:29

AOP 是关于横切关注点,即您需要在整个应用程序中执行的操作。例如日志记录。假设您想要跟踪进入和退出方法的时间。这对于方面来说非常容易。您基本上为事件指定一个“处理程序”,例如输入一个方法。如果有必要,您还可以使用“通配符”指定您感兴趣的方法,然后只需编写处理程序代码即可,例如可以记录一些信息。

AOP is about crosscutting concerns i.e. things that you need to do throughout the whole application. For instance logging. Suppose you want to trace when you enter and exit a method. This is very easy with aspects. You basically specify a "handler" for an event, such as entering a method. If necessary you can also specify with "wildcards" which methods you are interested in and then it is just a matter of writing the handler code, that could for instance log some info.

幽梦紫曦~ 2024-10-21 11:49:29

面向方面的编程基本上是为了分离横切关注点(非功能性)并将其开发为方面,例如安全性、日志记录、监控等,在应用程序中需要时将其放在一边,您可以将其用作即插即用;玩。我们能实现的唯一好处是干净的代码,更少的代码,程序员可以专注于业务逻辑(核心关注点),从而可以开发出更好的模块化和质量系统。

Aspect Oriented Programming is basically for separating the cross-cutting concerns (Non-functional) and develop it as aspects, like, security, logging, monitor etc., kept it aside whenever you need in your application, you can use it as plug & play. Only benefit we can achieve is clean code ,less code and programmers can focus on business logic(core concerns) , so that better modularity and quality system can be developed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文