什么是面向方面的编程? [英] What is Aspect Oriented Programming?
问题描述
每次我在这里播客或阅读有关它的博客条目时,即使在这里,它们也听起来像是弦论之类的东西.用类固醇依赖注射来描述OOP的最好方法是吗?
Every time I here a podcast or read a blog entry about it, even here, they make it sound like string theory or something. Is the best way to describe it OOP with Dependency Injection on steroids?
每次有人试图解释它时,就像,方面,[花生卡通成人的声音],正交,[更多杂音],横切关注点等.说真的,有人可以用外行的术语来形容它.
Every time someone tries to explain it, it’s like, Aspects, [Adults from Peanuts Cartoon sound], Orthogonal, [more noise], cross cutting concerns, etc. Seriously, can anyone describe it in layman’s terms.
推荐答案
Laymans术语,让我举一个例子.假设您有一个Web应用程序,并且需要添加错误日志记录/审核.一种实现是进入每个公共方法并添加您的try catch块等.
Laymans terms so let me give you an example. Let's say you have a web application, and you need to add error logging / auditing. One implementation would be to go into every public method and add your try catch blocks etc...
面向方面的方面说得很对,让我将我的方法注入到您的方法周围,例如,系统可能会调用而不是调用YourClass.UpdateModel(),
Well Aspect oriented says hogwash with that, let me inject my method around your method so for example instead of calling YourClass.UpdateModel(), the system might call,
LoggingHandler.CallMethod()然后,此方法可能会将调用重定向到UpdateModel,但将其包装在try catch块中以处理记录错误.
LoggingHandler.CallMethod() this method might then redirect the call to UpdateModel but wraps it in a try catch block to handle logging errors.
现在的诀窍是,这种重定向通过配置或将属性应用于方法自动进行.
Now the trick is that this redirection happens automagically, through configuration or by applying attributes to methods.
这就像您所说的跨领域工作一样,这是每个域中都非常常见的编程元素,例如:日志记录,审计,事务管理,授权.
This works for as you said cross cutting things which are very common programing elements that exist in every domain, such as: Logging, Auditing, Transaction Mgmt, Authorization.
其背后的想法是从业务/应用程序层中删除所有这些通用管道代码,以便您可以专注于解决问题,而不必担心记录此方法调用或该方法调用.
The idea behind it is to remove all this common plumbing code out of your business / app tier so you can focus on solving the problem not worrying about logging this method call or that method call.
这篇关于什么是面向方面的编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!