Upload
zubair-ahmed
View
2.452
Download
0
Tags:
Embed Size (px)
Citation preview
Introduction to Aspect Oriented Programming in .NET with PostSharp
Zubair Ahmedtwitter.com/zubairdotnet
Agenda
• The problem• What is AOP?• PostSharp• Writing Aspects in Visual
Studio 2010• Learning Resources
The problem• There was nothing in the
beginningpublic class CustomerProcesses{}
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; }}
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; } }}
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 {
. . . . }}
The problem• Resulting code is:–Boiler plate–Highly Coupled
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
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
PostSharp• A AOP Framework for .NET• Works with all versions
of .NET• Integrates into Visual Studio• Supports static ‘built-time’
weaving
Demo• Aspects with PostSharp in
Visual Studio 2010
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]