AOP in C# 2013
-
Upload
antya-dev -
Category
Technology
-
view
6.954 -
download
1
description
Transcript of AOP in C# 2013
![Page 1: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/1.jpg)
AOP IN C#
Introduction to Aspect-Oriented-Programming
![Page 2: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/2.jpg)
@Antya Dev
http://antyadev.blogspot.com/
https://twitter.com/AntyaDev
![Page 3: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/3.jpg)
TOPICS
- What is bad and good design ?- Problem statement.- What is AOP ?- Demo
![Page 4: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/4.jpg)
WHAT IS BAD DESIGN ?
![Page 5: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/5.jpg)
WHAT IS BAD DESIGN ?
-the system is rigid: it's hard to change a part of the system without affecting too many other parts of the system
-the system is fragile: when making a change, unexpected parts of the system break
- the system or component is immobile: it is hard to reuse it in another application because it cannot be disentangled from the current application
![Page 6: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/6.jpg)
![Page 7: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/7.jpg)
![Page 8: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/8.jpg)
![Page 9: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/9.jpg)
class GarbageService { public void Transfer(int sourceID, int destinationID, int size) { Storage source = Storage.GetById(sourceID); Storage destination = Storage.GetById(destinationID);
var garbage = source.GetGarbage(size); destination.PutGarbage(garbage); } }
![Page 10: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/10.jpg)
class GarbageService{ public void Transfer(int sourceID, int destinationID, int size) { Trace.TraceInformation("Entering GarbageService.Transfer( sourceID={0},destinationID={1})", sourceID, destinationID);
try { Storage source = Storage.GetById(sourceID); Storage destination = Storage.GetById(destinationID);
var garbage = source.GetGarbage(size); destination.PutGarbage(garbage); } catch (Exception ex) { Trace.TraceError("Exception: GarbageService.Transfer( sourceID = {0}, destinationID = {1}) failed : {2}“, sourceID, destinationID, ex.Message); throw; } }}
![Page 11: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/11.jpg)
class GarbageService{ public void Transfer(int sourceID, int destinationID, int size) { Trace.TraceInformation("Entering GarbageService.Transfer(sourceID = {0}, destinationID = {1})", sourceID, destinationID);
if (sourceID <= 0) { throw new ArgumentOutOfRangeException("sourceID"); } if (destinationID <= 0) { throw new ArgumentOutOfRangeException("destinationID"); } if (size <= 0) { throw new ArgumentOutOfRangeException("size"); }
try { Storage source = Storage.GetById(sourceID); Storage destination = Storage.GetById(destinationID);
var garbage = source.GetGarbage(size); destination.PutGarbage(garbage); } catch (Exception ex) { Trace.TraceError("Exception: GarbageService.Transfer(sourceID = {0}, destinationID = {1}) failed : {2}“, sourceID, destinationID, ex.Message); throw; } }}
![Page 12: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/12.jpg)
REQUIREMENTS
• Functional Requirements• Line-of-business
• Non functional requirements• Logging• Caching• Transaction• Validation • Exception Handling• Thread Sync• GUI Binding• … and a lot more! Cross-C
utting Concerns
![Page 13: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/13.jpg)
WHAT IS AOP ?
AOP - is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns.
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.
With AOP, you still define the common functionality in one place, but you can declaratively define how and where this functionality is applied without having to modify the class to which you are applying the new feature.
![Page 14: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/14.jpg)
AOP
• doesn’t solve any new problem• it’s just another tool in your toolbox• the main goal is nice separation of concerns• a decrease in development costs and software delivery time;• an increase in application maintainability.• reduce noise in source == more clean model
![Page 15: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/15.jpg)
AOP TERMINOLOGY
• Join Point • place where behavior can be added• Advice• code that can be injected at join points
• Point Cut• join points where advices should be applied
![Page 16: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/16.jpg)
![Page 17: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/17.jpg)
![Page 18: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/18.jpg)
![Page 19: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/19.jpg)
![Page 20: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/20.jpg)
AOP WEAVING
• Compile time weaving • Source-Level Weaving• Modifying the MSIL code
• Run-Time weaving• Dynamic Proxy
![Page 21: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/21.jpg)
Gael Fraiteur
PostSharp is the most comprehensive aspect-
oriented framework for .NET
![Page 22: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/22.jpg)
Demo
![Page 23: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/23.jpg)
AOP is NOT a Decorator
pattern
![Page 24: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/24.jpg)
Philip Laureano
LinFu
![Page 25: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/25.jpg)
Comparing Aspect Frameworks
STATIC VS DYNAMIC AOP
Build-Time:Very ExpressiveRobust ModelNot InvasiveStatic
Run-Time:Less ExpressiveBrittle ModelInvasiveDynamic
Hybrid
PostSharp
Spring.NETCastleMS Unity/PIAB
LinFu
![Page 26: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/26.jpg)
Comparing Aspect Frameworks
EXPRESSIVENESS
PostSharp Linfu Spring.NE
T Castle Unity/PIAB
Method Interception Yes Yes Yes Yes
Private/Sealed Member Interception
Yes Yes
Event Interception Yes
Member Introduction Yes
What can you do with the framework?
![Page 27: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/27.jpg)
Aspects to Object
vs Aspects to
Class
![Page 28: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/28.jpg)
We need Aspects!
![Page 29: AOP in C# 2013](https://reader033.fdocuments.net/reader033/viewer/2022061201/547a3a8cb4af9f60508b4730/html5/thumbnails/29.jpg)
We have great frameworks!