Introduction to aop

1,737 views

Published on

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.

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

  • Be the first to like this

No Downloads
Views
Total views
1,737
On SlideShare
0
From Embeds
0
Number of Embeds
833
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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

    1. 1. Introduction to AOPDror Helper@dhelper
    2. 2. About.Me Software developer and technical lead PostSharp MVP Blogger: http://blog.drorhelper.com
    3. 3. Developer write codevoid transfer(Account fromAcc, Account toAcc, int amount){ if (fromAcc.getBalance() < amount) { throw new InsufficientFundsException(); } fromAcc.withdraw(amount); toAcc.deposit(amount);}
    4. 4. 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);}
    5. 5. 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);}
    6. 6. 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);
    7. 7. 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; }
    8. 8. What can we do?
    9. 9. The Problem Code duplication Hard to read Difficult to maintain Not all programmers code the same Ctrl + C, Ctrl + V
    10. 10. Problem - Cross cutting concerns Customer Product Order LoggingCross-Cutting SecurityConcerns Caching
    11. 11. Non-Functional Requirements Security Caching Tracing Exception handling Monitoring Validation Persistence Transactions Thread sync
    12. 12. Solution  AOP Separation of concerns Extension of OOP1. Encapsulate Cross cutting concerns into aspects2. Apply aspects to code
    13. 13. Why AOP • AOP exist for more then 15 Mature years Industry • Industry standard adopted Observed • -15% lines of code Benefits • -20% decoupling
    14. 14. Terminology
    15. 15. 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
    16. 16. Point Cuts myClass1.Func1 myClass1.Func2 Before Func2 myClass2.Func2
    17. 17. Advice The code to run Typically small Injected at join point
    18. 18. AspectJoin Point + Advice = Aspect
    19. 19. How to “do” AOP
    20. 20. Our example for today – the calculator
    21. 21. My co-worker solution
    22. 22. #1 Functional programming Using higher order functions to “warp” existing code Decorator pattern
    23. 23. 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.
    24. 24. Why not use existing tools
    25. 25. What IoC containers can do for you? Enable Dependency Injection (DI) Create objects and satisfy it’s dependencies A lot more..
    26. 26. #2 Dynamic proxies FrameworkApplication Proxy Aspect Object
    27. 27. .NET IoC tools Unity http://unity.codeplex.com/ Spring.NET http://www.springframework.net/ Castle http://www.castleproject.org/
    28. 28. 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.
    29. 29. #3 MSIL transformations IL Final Code Compile weaving Assembly
    30. 30. IL transformation tools Mono.Cecil http://www.mono-project.com/Cecil NotifyPropertyWeaver http://code.google.com/p/notifypropertyweaver/
    31. 31. 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.
    32. 32. #4 Build time AOP Build Process Intermediate FinalSource Compiler Assembly AOP Assembly Code (IL)Aspects
    33. 33. 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
    34. 34. ResourcesCross Cutting Concerns bloghttp://crosscuttingconcerns.com/My blog: http://blog.drorhelper.com

    ×