1 / 11

Presented by Ramaswamy Krishnan-Chittur

AOP for better Encapsulation and Abstraction Illustrated using an example employing validation service. Presented by Ramaswamy Krishnan-Chittur. Contents. 1. Introduction to AOP What are Aspects? What is aspect-oriented programming? The advantages of employing the AO-approach

uri
Download Presentation

Presented by Ramaswamy Krishnan-Chittur

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AOP for betterEncapsulation and AbstractionIllustrated using an example employing validation service. Presented by Ramaswamy Krishnan-Chittur

  2. Contents • 1. Introduction to AOP • What are Aspects? • What is aspect-oriented programming? • The advantages of employing the AO-approach • 2. AOP: Illustrated with an example • Background • Problem • Solution • Evaluation • Conclusion • References

  3. What are Aspects? • An Aspect is a property of the system that tends NOT to be a unit of the system’s functional decomposition, but rather one that affects the performance or semantics of the components in Systemic ways; like memory access patterns and synchronization of concurrent objects. [1]

  4. Aspect-Oriented Programming (AOP) • The primary goal of AOP is to extract functionality scattered throughout the whole application such as logging, transaction management, or security management into a separate module, [1] so that the resultant client-code is more purpose-specific, more readable, and less tangled. • AOP is a way of executing arbitrary code orthogonal to a module’ s primary purpose.[2]

  5. AOP way – the better way? • AOP facilitates: • Isolation of ‘aspects’, • Less tangled code, • Better code encapsulation, and • Better code reusability.

  6. Background: An example ofa simple function that does its function • Let us consider the following code: public double SectorArea (double radius, double angle) { return (radius * radius * angle / 2); } • Now, if we want to ensure that the code enables range check on its parameters and on the return value, i.e., • The radius must be non negative. • The angle should be between 0 and 2 PI, and may be • The return value, i.e., the sector area, is a non-negative value. then, the code should be modified as follows:

  7. Problem: Orthogonal, though vital, additions to the code increase the complexity and disturb the flow • The code with the validation check for its parameters would be: public double SectorArea (double radius, double angle) { if (radius < 0.0) throw new ArgumentOutOfRangeException ("radius out of range ..."); if ( (angle < 0.0) || (angle > 2 * 3.14) ) throw new ArgumentOutOfRangeException ("angle out of range ..."); return (radius * radius * angle / 2); } • Or may be, if we want to check just the return value, public double SectorArea (double radius, double angle) { double retVal = radius * radius * angle / 2; if (retVal < 0) throw new Exception (“Return value out of range …"); return retVal; }

  8. Solution: Isolate the orthogonal stuff, and capture it as a separate entity • Ideally, we would like to take those if … then…else… out of the client code, and still enable the validation checks. • AOP helps achieve this beautifully.

  9. Evaluation: AOP code - The better code? • The AOP code that enables range check: [return: Range (Lower = 0.0)] publicdouble SectorArea ([Range (Lower = 0.0)] double radius, [Range (Lower = 0.0, Upper = 6.28)] double angle) { return (radius * radius * angle / 2); } • It is strikingly clear from the code that: • The return value has a lower bound of 0.0 • The parameter radius has a lower bound of 0.0 • The parameter angle has to be between 0.0 and 2 PI • If any of the parameters, or the return value, go out of range, an exception is thrown with all the necessary details of the parameter which caused the exception, and the allowed range of the parameter. The exception can in turn be handled appropriately by the client.

  10. Conclusion • AOP helps reduce client code complexity. • lessif-then-elses in the client code. • AOP captures the purpose of the code better. • [Range (Lower = 0.0)] double radius at the function declaration is more explicit and elegant than if (radius < 0.0) throw new ArgumentOutOfRangeException (“..."); in the body of the function. • AOP enhances abstraction and encapsulation. • The validation check is isolated as a separate module. Any change to validation check handling can be made at this ONE place. • AOP promotes reusability. • The range-checker attribute provides a common point where the out-of-range exceptions can be handled. • The range-checker can be applied to a variety of classes which need range-checks, like: class Geometry, class Stock_Market etc.

  11. References • Aspect-Oriented Programming - Kiczales et al; • Aspect-Oriented Programming with C# and .NET - Wolfgang Schult and Andreas Polze • Aspect-Oriented Programming in C#/.NET - Edward Garson • Advanced .NET remoting – Ingo Rammer. • Essential .NET, volume 1 – Don Box, Chris Sells. • Microsoft .NET Remoting - Kim Williams, James Naftel, Scott McLean • Remoting with C# and .NET: Remote Objects for Distributed Applications – David Conger • Visual C# .NET – Jason Price, Mike Gunderloy.

More Related