aspect oriented programming in ruby

47
ASPECT-ORIENTED PROGRAMMING IN RUBY Camille Baldock @camille_, http://camillebaldock.co.uk

Upload: camille-baldock

Post on 26-Jun-2015

470 views

Category:

Software


2 download

DESCRIPTION

Aspect oriented programming in Ruby: talk given on 14/04/2014 at LRUG (London Ruby User Group). http://camillebaldock.co.uk/aspect-oriented-programming-in-ruby Many of us developers love arguing about architecture that we dislike and refactoring our code to loosen coupling and weaken dependencies between our objects. Unfortunately, some overarching parts of our applications, like persistence, networking, notifications, logging, auditing, are scattered in our code, forcing us to specific explicit dependencies between them and our domain objects. Aspect-oriented programming is a solution to the problem of some features affecting virtually all business requirements, and expresses that problem in a compact and DRY way. In this practical talk, I: - introduce the basic concepts of AOP, and how it is still relevant even in a non-statically typed language like Ruby - show you how to easily and quickly leverage some AOP principles in your Rails application - play with some AOP-friendly constructs in Ruby 2, in particular TracePoint walk you through two existing Ruby frameworks to practice Aspect-Oriented Programming I even attempted to prove that not all things coming from the Java world are necessarily bad. Website: http://camillebaldock.co.uk Twitter: @camille_

TRANSCRIPT

Page 1: Aspect oriented programming in Ruby

ASPECT-ORIENTED PROGRAMMING IN RUBY

Camille Baldock !

@camille_, http://camillebaldock.co.uk

Page 2: Aspect oriented programming in Ruby

WHAT IS AOP?

Page 3: Aspect oriented programming in Ruby

FROM JAVA TO RUBY

Page 4: Aspect oriented programming in Ruby

FROM JAVA TO RUBY

Page 5: Aspect oriented programming in Ruby

FROM JAVA TO RUBY

Page 6: Aspect oriented programming in Ruby

WHY USE AOP ?

Tangled code Scattered code

A modularity approach for logical concerns that cut across the domain model.

Page 7: Aspect oriented programming in Ruby

SOME REQUIREMENTS DON’T MAP NICELY IN OO

Page 8: Aspect oriented programming in Ruby

CROSS-CUTTING CONCERNS

• Logging (tracking program behaviour)

• Verification (checking program behaviour)

• Policy enforcement (correcting behaviour)

• Security management (preventing attacks)

• Profiling (exploring where a program spends its time)

Page 9: Aspect oriented programming in Ruby

JOIN POINTJoin points are the individual points of interest within a

program’s execution which the aspect is advising.

Page 10: Aspect oriented programming in Ruby

ADVICEAdvice is code executed before, after, or around a given join

point.

Page 11: Aspect oriented programming in Ruby

TYPES OF ADVICE

Before !

After returning !

After raising !

After (… returning or raising) !

Around

Page 12: Aspect oriented programming in Ruby

POINTCUTA point cut is a set of join points. It represents the total set of

criteria necessary for execution of a given piece of advice code.

Page 13: Aspect oriented programming in Ruby

ASPECTAn aspect is a collection of point cuts and their respective

advice, collected to address a specific concern.

Page 14: Aspect oriented programming in Ruby
Page 15: Aspect oriented programming in Ruby
Page 16: Aspect oriented programming in Ruby
Page 17: Aspect oriented programming in Ruby
Page 18: Aspect oriented programming in Ruby

SINGLE RESPONSIBILITY CHECK

• One responsibility

• We still have two dependencies:

• Repository object which provides persistence to our snippets.

• Logger which helps us track activity.

Page 19: Aspect oriented programming in Ruby
Page 20: Aspect oriented programming in Ruby
Page 21: Aspect oriented programming in Ruby
Page 22: Aspect oriented programming in Ruby
Page 23: Aspect oriented programming in Ruby
Page 24: Aspect oriented programming in Ruby

THE RAILS APPROACH

class Snippet < ActiveRecord::Base

end

Page 25: Aspect oriented programming in Ruby

BUT WHAT IF WANT TO USE POROS ?

Page 26: Aspect oriented programming in Ruby

–DHH

“A standardised AOP framework has never really taken off in Ruby because the language itself already supports most of the desirable functionality of AOP.”

Page 27: Aspect oriented programming in Ruby
Page 28: Aspect oriented programming in Ruby

METAPROGRAMMING - A SUBSTITUTE FOR AOP ?

• Metaprogramming can at most be the vehicle for implementing AOP.

• AOP enforces the semantics which are supposed to be solved by metaprogramming.

Page 29: Aspect oriented programming in Ruby

–Gregor Kiczales, creator of AOP

“Another advantage of a direct semantics for AOP is that it helps with abstraction. When we look at a complex object model, it's simpler to think of it as describing the behaviour of the objects, rather than describing transformations on method tables that

implement the behaviour of objects. The same is true of AOP—it's simpler to think in terms of aspects

of an object's behaviour, rather than transformations on classes, which transform

method tables, which implement the behaviour of objects.”

Page 30: Aspect oriented programming in Ruby

METAPROGRAMMING: TOO MUCH POWER ?

~175 uses of alias_method in Rails

Page 31: Aspect oriented programming in Ruby

METAPROGRAMMING: TOO MUCH POWER ?

base.class_eval do alias_method :render_without_layout, :render alias_method :render, :render_with_layout end

Page 32: Aspect oriented programming in Ruby

DOESN’T DEPENDENCY INJECTION ALREADY SOLVE THIS ?

• Both achieve a loosely coupled architecture.

• Both achieve a better separation of concerns.

• Both offload some concerns from the base code.

Page 33: Aspect oriented programming in Ruby

DOESN’T DEPENDENCY INJECTION ALREADY SOLVE THIS ?

• DI is good when you have a dependency on a component, you just don’t know to which implementation.

Page 34: Aspect oriented programming in Ruby

DOESN’T DEPENDENCY INJECTION ALREADY SOLVE THIS ?

• AOP is good when you need to apply a common behaviour to a large number of elements of code. The target code is not necessarily dependent on this behaviour to be applied.

Page 35: Aspect oriented programming in Ruby

TO BE “AOP”, YOU NEED…• Interception: Interjection of advice, at least around

methods.

• Introduction: Enhancing with new (orthogonal!) state and behaviour.

• Inspection: Access to meta-information that may be exploited by point cuts or advice.

• Modularisation: Encapsulate as aspects.

Page 36: Aspect oriented programming in Ruby

DIY

• alias

• define_method

• adding a method to an instance

Page 37: Aspect oriented programming in Ruby

ABOUT METHOD_MISSING

• Used everywhere in Rails and other toolkits.

• Your method_missing often collides with mine…

• Consider using aspects to preserve modularity and reduce collisions.

• Reduce the actual calls to method_missing.

Page 38: Aspect oriented programming in Ruby
Page 39: Aspect oriented programming in Ruby

AQUARIUM

Page 40: Aspect oriented programming in Ruby

AQUARIUM

Page 41: Aspect oriented programming in Ruby

RUBY PURISTS, LOOK AWAY!

Page 42: Aspect oriented programming in Ruby

RUBY ADVISING JAVA, JAVA ADVISING RUBY

• Aquarium can run on JRuby

• Ruby aspects can advise Java

• Java aspects can advise Ruby code

Page 43: Aspect oriented programming in Ruby

ASPECTOR

Page 44: Aspect oriented programming in Ruby

WHAT CROSS-CUTTING CONCERNS ?

• Persistence

• Logging

• Verification

• Policy enforcement

• Security management

Page 45: Aspect oriented programming in Ruby

TRACEPOINT

• To be considered for profiling, analysing method usage and your call stack

• Ruby 2.0

Page 46: Aspect oriented programming in Ruby

THANKS!

Stephen Best (@thebestie)

Notes, references, slides and further reading on:

http://camillebaldock.co.uk/aspect-oriented-programming-in-ruby

Page 47: Aspect oriented programming in Ruby

Q&A