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

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

Jun 25, 2015

Download

Technology

Zubair Ahmed
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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]