问题描述
什么是cross-cutting concern的好例子? wikipedia 页面上的病理记录示例页面对我来说似乎不完整.
特别是在此示例中,为什么记录会导致代码重复( scationing )? (除了无处不在的简单调用,例如log("...."),这似乎没什么大不了的).
core concern和a cross-cutting concern?
有什么区别我的最终目标是更好地了解AOP.
推荐答案
在理解交叉切割问题之前,我们必须了解关注.
a 关注是一个术语,它是指系统的一部分基于功能.
有两种类型的问题:
- 代表主要需求的单一功能和特定功能的问题被称为核心问题.
或
该系统的主要功能被称为核心问题.
例如:业务逻辑 - 代表次要要求功能的问题称为交叉弯曲问题或全系统范围的关注.
或
交叉挑战关注是整个应用程序中适用的关注点,它会影响整个应用程序.
例如:记录,安全性和数据传输是应用程序的几乎每个模块中都需要的关注点,因此它们是交叉切割的问题.
该图代表一个典型的应用程序,该应用程序被分解为模块.每个模块的主要关注点是为其特定领域提供服务.但是,这些模块中的每个模块都需要类似的辅助功能,例如安全记录和交易管理.横切问题的一个示例是"记录",它经常用于分布式应用程序中,以通过跟踪方法调用来帮助调试.假设我们在每个功能主体的开头和末端都进行记录.这将导致跨越至少一个功能的所有类.
其他推荐答案
我认为,交叉切割关注的最佳例子是交易行为.例如,必须在所有服务方法中将try-catch块带入提交和回滚调用将是驱逐出境的.用AOP可以用标记用标记来注释方法以所需的交易行为将其封装是一个很大的胜利.
另一个好候选人作为交叉切割关注的一个例子是授权.用标记来注释服务方法,该方法告诉谁可以调用它,并让一些AOP建议决定是否允许该方法调用,比在服务方法代码中处理该方法是可取的.
使用AOP建议实施日志记录可能是一种获得更灵活性的方法,因此您可以通过更改加入点来更改记录的内容.实际上,我看不到项目经常这样做.通常使用诸如log4j之类的库,该库可以让您通过记录级别和类别过滤,如果需要,在运行时,可以很好地工作.
核心问题是应用程序存在的原因,即应用程序自动化的业务逻辑.如果您有处理运输货物的物流应用程序,请弄清楚您可以在卡车上打包多少货物,或者卡车下车的最佳路线是核心问题.跨切割问题通常是实现细节,需要与业务逻辑分开.
其他推荐答案
除了接受的答案外,我还想提及另一个有关交叉切割问题的示例:远程.假设我只想在本地生态系统中调用其他组件,就好像它们正在运行一样.也许在某些情况下他们甚至这样做.但是现在我想运行在云或集群中分发的服务.我为什么要关心作为应用程序开发人员的这一方面?一个方面可以照顾找出谁打电话,以及在必要时如何序列的传输数据并进行远程呼叫.如果一切都在过程中运行,则方面只会转发本地呼叫.在Callee方面,该方面可以将数据评估,进行本地调用并返回结果.
现在让我告诉您一个关于"琐事"之类的小故事:几周前,我重构了一个复杂的代码库(约25万行的代码),为客户端重构.在几百个班级中,使用了一种伐木框架,又有几百种.然后有几千行System.out.println(*)确实应该有日志输出.因此,我最终固定了数千条散布在整个代码库中的代码.幸运的是,我可以在Intellij Idea(结构搜索和替换)中使用一些巧妙的技巧来加快整个动作,但是男孩不认为这很琐碎!当然,强烈依赖上下文的调试记录将始终发生在方法主体内,但是许多重要类型的记录类型,例如跟踪方法调用(甚至在层次上具有很好的缩进输出),登录处理或未手动的例外,用户审核(记录调用呼叫呼叫基于用户角色的限制方法)可以轻松地在方面实现,而无需污染源代码.日常应用程序开发人员无需考虑它,甚至看到散布在代码库中的记录器调用.有人负责使方面保持最新状态,甚至可以将记录策略或整个伐木框架集中在一个地方.
我可以提出类似的解释,以解决其他跨切割问题.保持代码清洁,没有散射和缠结的IMO是专业精神的问题,而不是可选的.最后但并非最不重要的一点是,它可以使代码可读,可维护,可重构.阿们.
问题描述
What is a good example of a cross-cutting concern? The medical record example on the wikipedia page seems incomplete to me.
Specifically from this example, why would logging lead to code duplication (scattering)? (Besides simple calls such as log("....") everywhere, which doesn't seem like a big deal).
What is the difference between a core concern and a cross-cutting concern?
My end goal is to get a better understanding of AOP.
推荐答案
Before understanding the Crosscutting Concern, we have to understand the Concern.
A Concern is a term that refers to a part of the system divided on the basis of the functionality.
There are two types of concerns:
- The concerns representing single and specific functionality for primary requirements are known as core concerns.
OR
Primary functionality of the system is known as core concerns.
For example: Business logic - The concerns representing functionalities for secondary requirements are referred to as crosscutting concerns or system-wide concerns.
OR
The crosscutting concern is a concern which is applicable throughout the application and it affects the entire application.
For example: logging, security and data transfer are the concerns which are needed in almost every module of an application, hence they are cross-cutting concerns.
This figure represents a typical application that is broken down into modules. Each module’s main concern is to provide services for its particular domain. However, each of these modules also requires similar ancillary functionalities, such as security logging and transaction management. An example of crosscutting concerns is "logging," which is frequently used in distributed applications to aid debugging by tracing method calls. Suppose we do logging at both the beginning and the end of each function body. This will result in crosscutting all classes that have at least one function.
其他推荐答案
I think the single best example of a cross-cutting concern is transactional behavior. Having to put try-catch blocks with commit and rollback calls in all your service methods would be repellent, for instance. Annotating the methods with a marker that AOP can use to encapsulate them with the desired transactional behavior is a big win.
Another good candidate as an example of a cross-cutting concern is authorization. Annotating a service method with a marker that tells who can call it, and letting some AOP advice decide whether to allow the method call or not, can be preferable to handling that in service method code.
Implementing logging with AOP advice could be a way to get more flexibility, so that you can change what gets logged by changing a joinpoint. In practice I don't see projects doing that very often. Typically using a library like log4j that lets you filter by logging-level and category, at runtime if you need to, works out well enough.
A core concern is a reason that the application exists, the business logic that the application automates. If you have a logistics application that handles shipping freight, figuring out how much cargo you can pack on a truck or what's the best route for the truck to take to drop off its deliveries might be core concerns. Cross-cutting concerns are typically implementation details that need to be kept separate from business logic.
其他推荐答案
In addition to the accepted answer I want to mention another example for a cross-cutting concern: remoting. Say I just want to call other components in my ecosystem locally as if they were running in process. Maybe in some cases they even do. But now I want to run my services distributed in a cloud or cluster. Why should I care about this aspect as an application developer? An aspect could take care of finding out who to call and and how, serialising transmitted data if necessary and making a remote call. If everything was running in process the aspect would just forward the local call. On the callee side the aspect would deserialise the data, make the local call and return the result.
Now let me tell you a little story about "trivial" things like log output: Just a few weeks ago I refactored a complex, but not too big code base (around 250K lines of code) for a client. In a few hundred classes one kind of logging framework was used, in another few hundred another. Then there were several thousand lines of System.out.println(*) where there really should have been log output. So I ended up with fixing thousands of lines of code scattered throughout the code base. Fortunately I could use some clever tricks in IntelliJ IDEA (structural search & replace) in order to speed up the whole action, but boy don't you think it was trivial! Sure, strongly context-dependent debug logging will always occur within a method body, but many important types of logging such as tracing method calls (even hierarchically with a nicely indented output), logging both handled or unhandled exceptions, user auditing (logging calls to restricted methods based on user roles) and so forth can easily be implemented in aspects without them polluting the source code. The everyday application developer does not need to think about it or even see the logger calls scattered across the code base. Someone is responsible for keeping the aspect up to date and can even switch the logging strategy or the whole logging framework centrally in one place.
I can come up with similar explanations for other cross-cutting concerns. Keeping code clean and free from scattering and tangling IMO is a matter of professionalism, not anything optional. Last but not least it keeps the code readable, maintainable, refactorable. Amen.