Professional Documents
Culture Documents
Producing High-Quality
Software with Aspect-Oriented
Programming
Published: July 2011
CONTENTS
INTRODUCTION.................................................................................... 3
THE TECHNICAL PROBLEM.................................................................. 4
Cross-Cutting Concerns...................................................................... 4
Common Cross-Cutting Concerns .................................................. 4
The Effect of Cross-Cutting Concerns............................................ 5
A Framework for Cross-Cutting Behaviors................................... 6
EXISTING SOLUTIONS..........................................................................7
Dynamic Proxies.....................................................................................7
Functional Programming....................................................................7
Code Generation................................................................................... 8
Meta-Programming.............................................................................. 8
ASPECT-ORIENTED PROGRAMMING: A BETTER SOLUTION............ 9
Overview of AOP................................................................................... 9
The History and Evolution of AOP................................................10
THE BENEFITS OF AOP....................................................................... 12
Lowers Costs by Reducing Lines of Code...................................12
Decreases Defects..............................................................................12
Eases Implementation of Quality Assurance............................13
Improves Maintainability.................................................................13
Improves Teamwork ..........................................................................14
CASE STUDIES..................................................................................... 15
Siemens, Healthcare Industry.........................................................15
FullArmor, IT Industry........................................................................17
POSTSHARP: THE LEADING AOP FRAMEWORK
FOR .NET.............................................................................................. 19
PostSharp Technology.......................................................................19
PostSharp Features............................................................................20
About PostSharp Technologies......................................................21
Featured Customers...........................................................................22
CONCLUSIONS.................................................................................... 23
WORKS CITED..................................................................................... 24
OTHER RESOURCES............................................................................ 25
INTRODUCTION
For years, software engineers have struggled with the question of how
to structure their code in order to maximize reuse and reduce defects.
While traditional object-oriented programming (OOP) provides a solid
framework for code organization, it breaks down when developers must
implement features that cut across the entire system, such as logging and
transaction handling. The usual methods for implementing such crosscutting functionality lead to unnecessary code duplication, an increase in
product bugs, and a decrease in both quality and time to market.
Aspect-Oriented Programming (AOP) adds a high level of reuse to the
traditional OOP framework, with minimal impact on existing code bases.
Aspect-Oriented Programming delivers:
a decrease in development costs and software delivery time;
a reduction in software defects; and
an increase in application maintainability.
The following paper demonstrates these benefits in detail, and
demonstrates how two companies successfully implemented AOP:
Siemens Healthcare using the open-source AspectJ AOP framework; and
FullArmor, using the PostSharp framework.
The study, based on the analysis of three widely used opensource projects, also showed that cross-cutting concerns whose
implementation is always very scattered have more defects than
classical business features. Using a rank-order correlation coefficient, the
authors found a strong correlation between a features bug count and
that features diffusion across multiple classes and methods. Crosscutting
concerns in traditional software, noted the researchers, are hard to find,
understand, and work with.
EXISTING SOLUTIONS
Besides AOP, several other frameworks that were originally designed for
other purposes have been recruited to address the problem of crosscutting concerns. Each has its strengths, but because none were originally
designed to address the challenges posed by cross-cutting concerns, each
also possesses significant shortcomings.
Dynamic Proxies
PostSharp enables
me to achieve things
unobtrusively where
other frameworks are
large, ungainly, intrusive,
slow, and require me to
alter my architecture. As
a past user of other AOP
systems I am convinced
there is no better system
out there right now.
Andrew Matthews
Application Architect at
Premier Technologies
Functional Programming
Functional programming has become more popular with the addition of
higher-order functions and other functional language constructs to C#. In
a functional paradigm, a function is itself an object, which makes it easy
to compose a family of functions together.
Functional programming has been used with some success to address
some crosscutting concerns such as transaction management or
exception handling. However, this approach has significant drawbacks for
the majority of crosscutting concerns:
1. Developers cannot automatically pass context from the caller to
the aspect code. Tracing of code execution, for example, cannot be
implemented with the functional approach.
2. Developers must modify the code of every function to which they wish
to introduce cross-cutting behavior.
Code Generation
Every developer who has used an IDE is familiar with the code generation
paradigm, in which UI gestures or models, such as data schemas, are
converted into executable code. Visual Studio and the .NET Framework
define several components that attempt to turn such cross-cutting
behaviors as multithreading into a matter of code generation. This is
of only limited effectiveness, however, as it cannot be used to add new
behaviors to existing code.
Meta-Programming
Meta programming, which has been made popular by dynamic languages,
allows programs to change their behavior extensively at both runtime
and build time.
AOP is a specific case of meta-programming. However, AOP operates at a
higher level of abstraction. Meta programming is powerful, but complex
to learn and implement. Developers who have sought to implement
meta-programming as a comprehensive solution to the problem of
cross-cutting have found themselves writing their own aspect-oriented
framework.
10
11
Decreases Defects
As demonstrated by [Eaddy, 2008], the number of defects in a feature
increases with both the number of lines used to implement the feature,
and the degree to which the features code base is scattered across the
system. Aspects address both concerns. Code is reduced and consolidated
by isolating the aspect into a single location. The use of declarative
syntax for applying the aspect to code artifacts eliminates the amount
of boilerplate code that must be injected throughout the system code
that itself may have contained defects, or otherwise required revision in
subsequent versions.
Although aspects can also contain defects, it is much easier to fix a bug
once in the aspect class than in every of the thousands of methods in
which it is applied, as is the case when the behavior is implemented
without AOP.
12
Improves Maintainability
The initial development of an application represents only a fraction of
its lifetime cost. The applications evolution occurs during maintenance.
As Meir Lehman noticed early, software systems evolve dramatically after
their initial release, and grow in complexity over time [Lehman, 1980].
During the maintenance phase, developers spend as much time reading
code as they do writing it [LaToza, 2006; Ko, 2006]. Analyzing the source
code is necessary in order to understand the changes made by other
members of the team. It also becomes necessary for developers to reeducate themselves on the structure of the system as time progresses,
and their memory of the initial development fades. Turnover is also a
factor: fresh-blooded team members with no prior knowledge of the
system must rely primarily on the source code to educate themselves
about its operation.
Teams that minimize code scattering of cross-cutting concerns make it
easier for developers to find the code affected by a change in a feature.
AOP enables developers to keep business code clean by moving technical
concerns out of business code. A new developer reading source code that
employs AOP doesnt have to differentiate between business logic and
cross-cutting concerns, as the cross-cutting behaviors are neatly isolated
into aspects.
Besides making code easier to read, AOP increases cohesion within
classes. Some studies [Greenwood, 2007] have shown that AOP
applications begin with better cohesion, and maintain more stable
cohesion within their classes over subsequent versions when compared
with traditional OOP applications.
13
Improves Teamwork
When I started to use
PostSharp intensively
in my project, my
code became much
simpler and much
more homogeneous.
At that time, my team
was mainly composed
of junior developers.
Giving them code that
was much simpler that
was just business logic
made it much easier for
them to maintain.
Pierre-Emmanuel
Dautreppe
Architect at Thales
Information Systems
14
CASE STUDIES
Siemens, Healthcare Industry
Anyone looking for proof that AOP can scale to large applications need
look no further than Siemens Healthcare [Wiese, 2005]. The companys US
division decided to implement AOP on a large-scale hospital information
system being built by 400 developers spread across four countries.
Aspects
Siemens used AOP in four principal areas:
15
16
FullArmor, IT Industry
FullArmor (http://www.fullarmor.com/) is a software and Software-as-aService (SaaS) vendor specializing in endpoint management and endpoint
security products for the Windows platform. Its software offerings focus
around scalable management solutions for Windows application delivery
and installation, policy management, profile virtualization, and cloud data
management.
FullArmor maintains a heterogeneous family of software applications
using multiple technologies and deployment environments. Its
application code runs on the server using ASP.NET, as well as in the
browser using Microsoft Silverlight. Further, FullArmors Web server
applications operate both on dedicated physical Web servers as well as
in cloud computing environments, such as Microsofts Windows Azure
and Amazons EC2. Any technology it adds to its software base must be
able to function with high quality and high performance across all these
environments.
FullArmor has integrated PostSharp into three of its key products:
AppPortal, PolicyPortal and ProfilePortal. These three applications and
their shared components comprise approximately 1.2 million lines of
code.
Aspects
FullArmor has used PostSharp to simplify implementation of a range of
cross-cutting features, including:
Tracing. Extensive logging was especially important in phases of the
project, when the target platform (Windows Azure) was still in early
testing phases.
Resource monitoring. FullArmor used aspects to meter consumption
of resources across the system, including devices, users, and network
bandwidth.
Build-time validation. WCF require interfaces and methods to be
annotated with specific custom attributes. An aspect emitted a buildtime error when this protocol was not respected.
Transaction handling. Declarative management of database
transactions.
Exception handling. Application policies required to automatically
retry an operation in response to certain exceptions, or ignore
exceptions that dont indicate a critical failure.
17
[PostSharp] makes it
easier to see whats
relevant, and of course,
that makes [the code]
easier to maintain.
David Gressett
Product Manager and
Chief Architect at
FullArmor
The technology was well received by the companys developers, and the
results have been roundly beneficial. Developers and product managers at
the company report that PostSharp has enabled them to separate crosscutting concerns such as logging and security from their middle tier Web
Services.
Like many large companies, FullArmor uses this easy separation of
concerns to divide its development team according to competencies:
the architecture team focuses on creating aspects that implement
cross-cutting behavior, which the business development team can then
associate with join points across the business layers of FullArmors
application suite.
18
PostSharp Technology
PostSharp relies on post-compilation, also called static weaving or MSIL/
bytecode rewriting, which enhances the output of the compiler by
producing a new executable program that includes additional behaviors
added by aspects.
All compilers targetting the .NET platform emit programs expessed in
a binary language called Microsoft Intermediate Instruction Language
(MSIL). MSIL is extensively documented in the ECMA-335 standard
specification, and has proved very stable. Used in some Microsoft products
(e.g. code coverage in Visual Studio Team System, Code Contracts),
post-compiling techniques are fully supported and do not use any
undocumented feature.
Only post-compilation technology supports all of the features required
by robust AOP frameworks high expressiveness, no impact on business
logic, robust aspect composition, build time validation, IDE integration,
and a gentle learning curve.
19
PostSharp Features
High expressiveness
Although most frameworks support simple features, such as method
interception, only a few frameworks including PostSharp implement
the concepts of pointcuts and advices in order to implement most
complex design patterns.
PostSharp allows developers to express aspects using the following
primitives:
Execute code when a method is entered or is exited with success or
failure.
Intercept calls to a method, a field, a property, or an event.
Introduce an interface, a property, or an event to a class.
Add custom attributes to any code element.
Add managed resources to assemblies.
No impact on business logic
Development teams should not be forced to change the architecture of
their existing business logic in order to add AOP behaviors. PostSharp
supports several methods for adding aspects or advices to code:
Declaratively, through custom attributes and multicast custom
attributes. Several elements of code can be targeted in a single line of
code.
Imperatively, by executing C#/LINQ code at build time to evaluate
queries of any complexity over assembly metadata (System.Reflection).
While other frameworks require developers to use special factory
methods instead of constructors, or make most methods virtual, PostSharp
never requires developers to change their code.
Robust aspect composition
An AOP framework must behave in well-documented and predictable
ways when several aspects are applied to the same element of
code especially when those aspects have been developed by two or
more independent teams of developers.
PostSharp has been engineered to guarantee that independently
authored aspects will compose well if they are applied to the same
elements of code.
20
21
Featured Customers
PostSharp is used daily by thousands of professional software developers
worldwide.
Finance
Bank of America
BNP Paribas
Deutsche Bank
Societe Generale
Healthcare
Philips
Oracle
Siemens
Technology
AEDS
Intel
NetApp
Thales
SolidWorks
Other
AEDS
eBay
Skanska
Thomson-Reuters
US Airways
22
CONCLUSIONS
Since the foundation of the discipline, the challenges faced by software
engineering have evolved considerably. Whereas the first generation
of computer programmers had to struggle with scarcity of hardware
resources, todays limiting factor is the ability of individuals and teams to
cope with complexity. The capacity to keep source code simple not only
during its inception phase, but during its whole lifetime is one of the
most important keys to success.
As any engineering discipline, software engineering learned that
simplicity is achieved by pursuing a good separation of concerns. A
separation of concerns is what allows designers to componentize
solutions to recurring problems whether in software engineering,
mechanical engineering, electrical and electronic engineering, or
architecture.
Although object-oriented programming (OOP) made significant progress
in this direction, there is still a large category of issues that cannot be
addressed appropriately using traditional programming styles: technical
requirements and others features grouped under the term of crosscutting concerns.
Aspect-oriented programming (AOP) is an extension of OOP and is
specifically designed to address cross-cutting concerns in software
development. AOP enables developers to write a formal expression
of solutions to repeating problems and automatically implement
them rather than manually doing so with conventional programming
techniques. Just as object-oriented programming did not replaced
structure programming, aspect-oriented programming is not a
replacement of OOP but rather an extension of it. AOPs positive impact
on software development is measurable in both controlled studies and
real-world development efforts.
Today, AOP is supported in most languages and platforms. AspectJ, the
reference AOP implementation, is the leading tool for Java. For Microsoft
.NET, PostSharp is by far the most advanced and mature framework, and
has been used commercially for several years by Fortune 500 companies.
The best way for teams to learn AOP is to begin with simple aspects
and add them to non-critical applications. For instance, developers can
use diagnostic aspects during development and remove them from the
production release, all without any impact on code. Then, as developers
become more confident in AOP, they can begin to add more complex
aspects in more critical applications.
By embracing aspect-oriented programming today, engineers are moving
toward software of higher quality and lower complexity.
23
WORKS CITED
[Eaddy, 2008] Eaddy M., Zimmerman T., Sherwood K., Garg V., Murphy
G., Nagappan N., Aho A. (2008). Do Crosscutting Concerns Cause
Defects? IEEE Transactions on Software Engineering, Vol. 34, No. 4,
July/August. Available online at http://citeseerx.ist.psu.edu/viewdoc/
download?doi=10.1.1.164.7117&rep=rep1&type=pdf. Illustrations
reproduced with kind permission of IEEE.
[Greenwood, 2007] Greenwood P., Bartolomei T., Figueiredo E., Dosea
M., Garcia A., Cacho N;. SantAnna C., Soares S., Borba P, Kulesza U.,
Rashid, A. (2007). On the Impact of Aspectual Decompositions on Design
Stability: An Empirical Study. ECOOP 2007 - Object-Oriented Programming,
21st European Conference, Berlin, Germany, July 30 - August 3, 2007,
Proceedings. Available online at http://eprints.comp.lancs.ac.uk/1665/.
[Ko, 2006] Andrew J. Ko, Brad A. Myers, Michael J. Coblenz, and Htet
Htet Aung. An Exploratory Study of How Developers Seek, Relate, and
Collect Relevant Information during Software Maintenance Tasks,
IEEE Transactions on Software Engineering, vol. 32, no. 12, December
2006. Available online at http://faculty.washington.edu/ajko/papers/
Ko2006SeekRelateCollect.pdf
[LaToza, 2006] Thomas D. LaToza , Gina Venolia , Robert DeLine.
Maintaining Mental Models: A Study of Developer Work Habits.
Proceedings of ICSE06, May 2028, 2006. Available online at http://www.
cs.cmu.edu/~tlatoza/Maintaining%20Mental%20Models%20-%20A%20
Study%20of%20Developer%20Work%20Habits.pdf
[Lehman, 1980] Lehman M., 1980. Program, Life-Cycles and the Laws of
Software Evolution. Proceedings of IEEE, 68, 9, pp. 1060-1076
[Wiese, 2005] Wiese D., Meunier R. Large Scale Application of AOP in the
Healthcare Domain: A Case Study. Proceedings of SIGPLAN`05, June 12-15,
2005
24
OTHER RESOURCES
AspectJ http://www.eclipse.org/aspectj/
AspectJ Development Tools http://www.eclipse.org/ajdt/
PostSharp http://www.postsharp.net/
25