how aspects clean your code

16
Aspect-Oriented Programming How can aspects clean your code? @BasiaFusinska

Upload: barbara-fusinska

Post on 14-Jun-2015

201 views

Category:

Software


0 download

DESCRIPTION

Aspect Oriented Programming

TRANSCRIPT

Page 1: How aspects clean your code

Aspect-Oriented Programming

How can aspects clean your code?

@BasiaFusinska

Page 2: How aspects clean your code

About me

Aspect-Oriented Programming

How can aspects clean your code? 2

@basiafusinska

Programmer

Architect

Coach

Manager

Teacher

Trainer

Page 3: How aspects clean your code

Agenda

3

❖ What the hell...are aspects?❖ Some examples of AOP❖ Demo 1: refactor your way to aspects❖ Every-day aspects❖ Demo 2: some not-so-obvious aspects❖ How to test aspects❖ Demo 3: testing in practice❖ Q & A

Aspect-Oriented Programming

How can aspects clean your code?

Page 4: How aspects clean your code

Some history

4

❖ Gregor Kiczales (1997), Xerox PARC❖ OOP is unable to solve some common problems❖ Aspects encapsulate cross-cutting concerns and make them

easier to reuse

Aspect-Oriented Programming

How can aspects clean your code?

Page 5: How aspects clean your code

Cross cutting concerns

5

❖ Cross-cutting concerns are scattered throughout the code❖ Code becomes difficult to develop and modify❖ Code breaks the Single Responsibility Principle

Aspect-Oriented Programming

How can aspects clean your code?

Page 6: How aspects clean your code

Cross-cutting concerns are everywhere!

7

❖ Logging❖ Validation❖ Persistence❖ Caching❖ Synchronization❖ Transactions❖ Exception handling❖ Authentication❖ Authorization❖ ...

Aspect-Oriented Programming

How can aspects clean your code?

Page 7: How aspects clean your code

What the heck are aspects?

6

❖ Purpose: cross-cutting concerns – pieces of functionality used across multiple parts of a system

❖ Job: the advice – code that performs cross-cutting concern❖ Map: pointcut – set of joinpoints❖ How: weaving

Aspect-Oriented Programming

How can aspects clean your code?

Page 8: How aspects clean your code

How it works?

6Aspect-Oriented Programming

How can aspects clean your code?

Page 9: How aspects clean your code

Types of advices?

6

❖ Entry❖ Exit❖ Exception❖ Success❖ Around

❖ Method boundaries vs method interceptions

Aspect-Oriented Programming

How can aspects clean your code?

Page 10: How aspects clean your code

Some benefits of AOP

8

❖ Code is cleaner❖ DRY (Don’t Repeat Yourself): promotes code reuse❖ Better encapsulation

Aspect-Oriented Programming

How can aspects clean your code?

Page 11: How aspects clean your code

9

Demonstration #1Refactor your way to aspects

Aspect-Oriented Programming

How can aspects clean your code?

Page 12: How aspects clean your code

You’ve been using AOP without knowing it

10

❖ IHttpModule - requests boundary aspects❖ IActionFilter - code run before and after the action execution❖ ASP.Net MVC Authentication - AuthorizeAttribute❖ Actions per request (Session Open/Close, ORM changes

saving)

Aspect-Oriented Programming

How can aspects clean your code?

Page 13: How aspects clean your code

11

Demonstration #2Some not-so-obvious aspects

Aspect-Oriented Programming

How can aspects clean your code?

Page 14: How aspects clean your code

Testing aspects

12

❖ Testing the pointcut – testing if aspects were used in correct places

❖ Testing the advice – testing if the aspects are doing what they are supposed to do

Aspect-Oriented Programming

How can aspects clean your code?

Page 15: How aspects clean your code

13

Demonstration #3Testing aspects

Aspect-Oriented Programming

How can aspects clean your code?

Page 16: How aspects clean your code

Thank you :)Any questions?

@basiafusinskabarbarafusinska.

com