Introduction to Aspect OrientedProgramming in .NET with PostSharp              Zubair Ahmed        twitter.com/zubairdotne...
Agenda•   The problem•   What is AOP?•   PostSharp•   Writing Aspects in Visual Studio 2010•   Learning Resources
The problem• There was nothing in the beginning  public class CustomerProcesses  {  }
The problem• Then there was business code  public class CustomerProcesses  {      public void RentBook( int bookId, int cu...
The problem• Then there was logging and exception handling  internal class CustomerProcesses  {      private static readon...
The problem• Then precondition checks  internal class CustomerProcesses  {      private static readonly TraceSource trace ...
The problem• Resulting code is:  – B oil e r pl a te  – H ig hl y Coupl e d
What is AOP?• An approach that extends OOP and addresses the  issue of Cross -cutting concerns  – Enca ps ul a te cro s s ...
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 ...
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• Support...
Demo• Aspects with PostSharp in Visual Studio 2010
Learning Resources•   sharpcrafters.com/postsharp/documentation/screencasts•   sharpcrafters.com/postsharp/documentation#b...
Upcoming SlideShare
Loading in …5
×

Introduction to Aspect Oriented Programming in .NET with PostSharp by Zubair Ahmed

2,883 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,883
On SlideShare
0
From Embeds
0
Number of Embeds
1,150
Actions
Shares
0
Downloads
39
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Introduction to Aspect Oriented Programming in .NET with PostSharp by Zubair Ahmed

  1. 1. Introduction to Aspect OrientedProgramming in .NET with PostSharp Zubair Ahmed twitter.com/zubairdotnet zubairahmed.net inbox@zubairahmed.net
  2. 2. Agenda• The problem• What is AOP?• PostSharp• Writing Aspects in Visual Studio 2010• Learning Resources
  3. 3. The problem• There was nothing in the beginning public class CustomerProcesses { }
  4. 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. 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. 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 { . . . . } }
  7. 7. The problem• Resulting code is: – B oil e r pl a te – H ig hl y Coupl e d
  8. 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. 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. 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
  11. 11. Demo• Aspects with PostSharp in Visual Studio 2010
  12. 12. Learning Resources• sharpcrafters.com/postsharp/documentation/screencasts• sharpcrafters.com/postsharp/documentation#blogposts• infoq.com/presentations/Advanced -AOP• dimecasts.net/Casts/ByTag/PostSharpMy Contacts• twitter.com/zubairdotnet• zubairahmed.net• inbox@zubairahmed.net

×