Thursday, 6 April 2017

AOP - Aspect Oriented Programming

Aspect-oriented programming was first described 20 years ago due to work of a team led by legendary programmer Gregor Kiczales [who also co-authored the book "The Art of the Metaobject Protocol" ]

What is it?

AOP aims to eliminate the noise of cross-cutting concerns, CCC, by putting them all in one place while still allowing the code previously "tangled" up with CCC to work just as before. The business logic is clearer, easier to read and maintain.

AOP concepts

  • cross-cutting concern, CCC: Is code which applies all over the place and often relates to getting an application to work rather than to any business concern. It cuts across an application. e.g. authentication, logging, exception handling, caching data, wrapping a transaction around something, auditing, ...
  • join point - a place in code where it can be 'cut', joined to, or intercepted. e.g. just before I call svc.SaveName()
  • Pointcut - a set of join points. e.g. just before any SaveName() method in project UI is called.
  • advice - some code to be applied by AOP
  • aspect - a combination of Pointcuts with advice
  • tangling - when CCC(s) are mixed with business or core logic like spaghetti such that the core logic is confusing to read.
  • scattering - having CCCs in multiple methods and classes
  • weaving - how AOP applies aspects to core logic.

Potential applications for ApoG

  • Authentication and Authorization : by putting this all together in one place the problem of security is not confused with other business logic.
  • Data caching
  • Exception handling - hopefully including all unhandled exceptions. ApoG currently has 991 exceptions.
  • Logging - There are over 2000 of these
  • Transaction wrap-around e.g. to save a unit of work. ApoG currently does not have unit of work transactions but these will be needed in future.
  • Auditing? - i.e. The process of saving PDF files and entries to the database can be put in one place.


  • Cleaner code
  • DRY - It applies Don't repeat yourself. It reduces code duplication
  • SRP - It applies the Single Responsibility Principle. Ensures a method or class does only one job.
  • Readability is improved - because one can read the core logic without being distracted by the CCCs
  • Maintenance is easier - because code relating to a CCC is in only one place it is easier to update. Because core logic is cleaner and uncluttered by tangential concerns it is easier to change just the core logic without interfering with anything else.
  • Money is no object because every AOP tool but one is free. Dozens of AOP tools have been written.


  • ????


Top 20 AOP tools

Interception, code. License: MIT OSS

NConcern: Is a relative newcomer. License: OSS

PostSharp: This has been the main .net AOP package since it arrived on the scene. License: Commercial software. Licenses are up to £449 per developer for a lifetime license. Limited licences are less. Not so expensive. Consider: If AOP improves productivity by 5%, the saving is at least £2500 per year, when one considers the average annual cost of a programmer is certainly over £50k/year. PostSharp Express is free but limited.


More articles:

AOP with the RealProxy Class | Principles of AOP, from 2015


Also see book listed above: AOP in .net. Although it may look like many AOP packages are not well documented, they often use similar implementation patterns. Like dependency injection, once one understands the principle, changing tools is relatively easy.

No comments:

Post a Comment