Axa Assurance Maroc - Insurer Innovation Award 2024
Introduction to Aspect Oriented Programming in .NET with PostSharp by Zubair Ahmed
1. Introduction to Aspect Oriented
Programming in .NET with PostSharp
Zubair Ahmed
twitter.com/zubairdotnet
zubairahmed.net
inbox@zubairahmed.net
2. Agenda
• The problem
• What is AOP?
• PostSharp
• Writing Aspects in Visual Studio 2010
• Learning Resources
3. The problem
• There was nothing in the beginning
public class CustomerProcesses
{
}
4. The problem
• Then there was business code
public class CustomerProcesses
{
public void RentBook( int bookId, int customerId )
{
Book book = Book.GetById( bookId );
Customer customer = Customer.GetById( customerId );
book.RentedTo = customer;
customer.AccountLines.Add(
string.Format( "Rental of book {0}.", book ), book.RentalPrice );
customer.Balance -= book.RentalPrice;
}
}
5. The problem
• Then there was logging and exception handling
internal class CustomerProcesses
{
private static readonly TraceSource trace =
new TraceSource( typeof (CustomerProcesses).FullName );
public void RentBook( int bookId, int customerId )
{
trace.TraceInformation(
"Entering CustomerProcesses.CreateCustomer( bookId = {0},
customerId = {1} )",
bookId, customerId );
try
{
Book book = Book.GetById( bookId );
Customer customer = Customer.GetById( customerId );
book.RentedTo = customer;
customer.AccountLines.Add(
string.Format( "Rental of book {0}.", book ), book.RentalPrice );
customer.Balance -= book.RentalPrice;
trace.TraceInformation(
"Leaving CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",
bookId, customerId );
}
catch ( Exception e )
{
trace.TraceEvent( TraceEventType.Error, 0,
"Exception: CustomerProcesses.CreateCustomer(
bookId = {0}, customerId = {1} ) failed : {2}",
bookId, customerId, e.Message );
throw;
}
}
}
6. The problem
• Then precondition checks
internal class CustomerProcesses
{
private static readonly TraceSource trace =
new TraceSource(typeof(CustomerProcesses).FullName);
public void RentBook(int bookId, int customerId)
{
if (bookId <= 0) throw new ArgumentOutOfRangeException("bookId");
if (customerId <= 0) throw new ArgumentOutOfRangeException("customerId");
trace.TraceInformation(
"Entering CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",
bookId, customerId);
try
{
. . . .
}
}
8. What is AOP?
• An approach that extends OOP and addresses the
issue of Cross -cutting concerns
– Enca ps ul a te cro s s -cutting co nce rns into As pe cts
– Im prov e s cod e re us a b il ity, m od ul a rity a nd
s e pa ra tion of conce rns
– Re d uce s d e f e cts b y re d ucing b o il e r -pl a te cod e
9. Identify Cross-cutting concerns
• Ca nnot b e s e pa ra te d into a m od ul e
• Cha l l e ng e to a chie v e s e pa ra tion of conce rns
• Some cros s -cutting co nce rns
– Exception Handling
– Caching
– Tracing
– Validation
– Authorization
– Transactions
10. PostSharp
• A AOP Framework for .NET
• Works w ith a l l v e rs ions of .N ET
• Inte g ra te s into Vis ua l Stud io
• Supports s ta tic ‘b uil t -tim e ’ w e a v ing