• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Introduction to aop
 

Introduction to aop

on

  • 1,524 views

Every software developer knows object oriented programming; in fact most use it every day. Using OOP enables code reuse and creates readable and maintainable code. ...

Every software developer knows object oriented programming; in fact most use it every day. Using OOP enables code reuse and creates readable and maintainable code.
But there are places where object oriented is just not enough. There are features that cut across the entire system such as security, logging and transaction handling which are hard to implement efficiently using the OO paradigm.
Aspect Oriented Programming (AOP) helps us deal with these application-wide concerns by adding a high level of reuse to the traditional OOP framework.
In this session I'll explain what AOP is all about and how it can be implemented in .NET using simple methods and industrial grade frameworks to improve the developer's everyday work by using aspects.

Statistics

Views

Total Views
1,524
Views on SlideShare
706
Embed Views
818

Actions

Likes
0
Downloads
0
Comments
0

9 Embeds 818

http://blog.drorhelper.com 734
http://feeds.feedburner.com 65
http://buttonwood8.zulti.com 7
http://theoldreader.com 4
http://speakerrate.com 3
http://8412831559140848595_f1c899fa869bd1531ed045d3dbef124bbb2a30ce.blogspot.com 2
http://translate.googleusercontent.com 1
http://feeds2.feedburner.com 1
http://www.linkedin.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Non functional requirments
  • A call to a methodThe method’s executionAssignment to a variableReturn statementObject constructionConditional checkComparisonException handlerLoops
  • A group of Join pointFilter drivenAttribute driven
  • IoC containers
  • Using IoC containers
  • Add IL in compile timeDifficult and fragile“Mini AOPs”
  • Adding step to compilationUsing IL weavingShow diagram

Introduction to aop Introduction to aop Presentation Transcript

  • Introduction to AOPDror Helper@dhelper
  • About.Me Software developer and technical lead PostSharp MVP Blogger: http://blog.drorhelper.com
  • Developer write codevoid transfer(Account fromAcc, Account toAcc, int amount){ if (fromAcc.getBalance() < amount) { throw new InsufficientFundsException(); } fromAcc.withdraw(amount); toAcc.deposit(amount);}
  • Permission support – Right away!void transfer(Account fromAcc, Account toAcc, int amount, User user){ if (! checkUserPermission(user)) { throw new UnauthorizedUserException(); } if (fromAcc.getBalance() < amount) { throw new InsufficientFundsException(); } fromAcc.withdraw(amount); toAcc.deposit(amount);}
  • Need to check parameters – Done!void transfer(Account fromAcc, Account toAcc, int amount, User user){ if(fromAcc == null || toAcc == null) { throw new IllegalArgumentException (); } if(balance <= 0) { throw new IllegalArgumentException (); } if (! checkUserPermission(user)) { throw new UnauthorizedUserException(); } if (fromAcc.getBalance() < amount) { throw new InsufficientFundsException(); } fromAcc.withdraw(amount); toAcc.deposit(amount);}
  • Needs logging – no problems!void transfer(Account fromAcc, Account toAcc, int amount, User user){ logger.info("transferring money..."); if(balance <= 0) { throw new IllegalArgumentException (); } if (! checkUserPermission(user)) { logger.info("User has no permission."); throw new UnauthorizedUserException(); } if (fromAcc.getBalance() < amount) { logger.info("Insufficient Funds, sorry"); throw new InsufficientFundsException(); } fromAcc.withdraw(amount); toAcc.deposit(amount);
  • And finally – exception handlingvoid transfer(Account fromAcc, Account toAcc, int amount, User user){ logger.info("transferring money..."); if(balance <= 0) { throw new IllegalArgumentException (); } if (! checkUserPermission(user)) { logger.info("User has no permission."); throw new UnauthorizedUserException(); } if (fromAcc.getBalance() < amount) { logger.info("Insufficient Funds, sorry"); throw new InsufficientFundsException(); } try { fromAcc.withdraw(amount); toAcc.deposit(amount); } catch(Exception exc) { logger.info(“failed transaction."); return; }
  • What can we do?
  • The Problem Code duplication Hard to read Difficult to maintain Not all programmers code the same Ctrl + C, Ctrl + V
  • Problem - Cross cutting concerns Customer Product Order LoggingCross-Cutting SecurityConcerns Caching
  • Non-Functional Requirements Security Caching Tracing Exception handling Monitoring Validation Persistence Transactions Thread sync
  • Solution  AOP Separation of concerns Extension of OOP1. Encapsulate Cross cutting concerns into aspects2. Apply aspects to code
  • Why AOP • AOP exist for more then 15 Mature years Industry • Industry standard adopted Observed • -15% lines of code Benefits • -20% decoupling
  • Terminology
  • Join Pointstatic void Main(){ myClass1.Func1 var myClass1 = new MyClass(); var myClass2 = new MyClass(); myClass1.Func1(); myClass1.Func2(2, 3); myClass1.Func2 myClass2.Func2(4, 5);} myClass2.Func2
  • Point Cuts myClass1.Func1 myClass1.Func2 Before Func2 myClass2.Func2
  • Advice The code to run Typically small Injected at join point
  • AspectJoin Point + Advice = Aspect
  • How to “do” AOP
  • Our example for today – the calculator
  • My co-worker solution
  • #1 Functional programming Using higher order functions to “warp” existing code Decorator pattern
  • 1# Functional Programming + - No additional  You cannot automatically dependency in your pass context from the caller to the aspect code build or runtime code.  Must modify the code of Programmers are every method to which already familiar with the you want to add the technology. aspect.  Aspect composition is less convenient.
  • Why not use existing tools
  • What IoC containers can do for you? Enable Dependency Injection (DI) Create objects and satisfy it’s dependencies A lot more..
  • #2 Dynamic proxies FrameworkApplication Proxy Aspect Object
  • .NET IoC tools Unity http://unity.codeplex.com/ Spring.NET http://www.springframework.net/ Castle http://www.castleproject.org/
  • Dynamic proxies + - You may already be  Very limited aspect-oriented features. using a DI framework.  Your objects must be instantiated using the Aspects can be container configured after build.  Does not work on static, non-public, and/or Some aspects may be non-virtual methods provided by the  Does not work on framework. fields, properties, or events.
  • #3 MSIL transformations IL Final Code Compile weaving Assembly
  • IL transformation tools Mono.Cecil http://www.mono-project.com/Cecil NotifyPropertyWeaver http://code.google.com/p/notifypropertyweaver/
  • MSIL transformations + - Very powerful. You can  Requires an advanced knowledge of MSIL. achieve virtually any code transformation  Very low level These frameworks are  typically do not compose well when many are free of charge and applied to the same backed by large method. companies.  No Visual Studio tooling.
  • #4 Build time AOP Build Process Intermediate FinalSource Compiler Assembly AOP Assembly Code (IL)Aspects
  • Summery Use AOP because: Less code = fewer bugs Less code = easier to read and maintain Reduce duplicate code Improve team work/architecture Reduce development cost
  • ResourcesCross Cutting Concerns bloghttp://crosscuttingconcerns.com/My blog: http://blog.drorhelper.com