introduction to aspect oriented programming in .net with postsharp by zubair ahmed

12
Introduction to Aspect Oriented Programming in .NET with PostSharp Zubair Ahmed twitter.com/zubairdotnet zubairahmed.net [email protected]

Upload: zubair-ahmed

Post on 25-Jun-2015

2.453 views

Category:

Technology


0 download

TRANSCRIPT

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

Introduction to Aspect Oriented Programming in .NET with PostSharp

Zubair Ahmedtwitter.com/zubairdotnet

[email protected]

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

Agenda

• The problem• What is AOP?• PostSharp• Writing Aspects in Visual

Studio 2010• Learning Resources

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

The problem• There was nothing in the

beginningpublic class CustomerProcesses{}

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

The problem• Then there was business

codepublic 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; }}

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

The problem• Then there was logging and

exception handlinginternal 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; }   }}

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

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 {

. . . . }}

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

The problem• Resulting code is:–Boiler plate–Highly Coupled

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

What is AOP?• An approach that extends OOP

and addresses the issue of Cross-cutting concerns–Encapsulate cross-cutting

concerns into Aspects– Improves code reusability,

modularity and separation of concerns

–Reduces defects by reducing boiler-plate code

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

Identify Cross-cutting concerns• Cannot be separated into a module• Challenge to achieve separation of

concerns• Some cross-cutting concerns– Exception Handling– Caching– Tracing– Validation– Authorization– Transactions

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

PostSharp• A AOP Framework for .NET• Works with all versions

of .NET• Integrates into Visual Studio• Supports static ‘built-time’

weaving

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

Demo• Aspects with PostSharp in

Visual Studio 2010

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

Learning Resources• sharpcrafters.com/postsharp/

documentat ion/screencasts• sharpcrafters.com/postsharp/

documentat ion#blogposts• infoq.com/presentat ions/Advanced-AOP• dimecasts.net/Casts/ByTag/PostSharp

My Contacts• twitter.com/zubairdotnet• zubairahmed.net• [email protected]