aspect oriented programming in ruby

Post on 26-Jun-2015

436 Views

Category:

Software

2 Downloads

Preview:

Click to see full reader

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

ASPECT-ORIENTED PROGRAMMING IN RUBY

Camille Baldock !

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

WHAT IS AOP?

FROM JAVA TO RUBY

FROM JAVA TO RUBY

FROM JAVA TO RUBY

WHY USE AOP ?

Tangled code Scattered code

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

SOME REQUIREMENTS DON’T MAP NICELY IN OO

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)

JOIN POINTJoin points are the individual points of interest within a

program’s execution which the aspect is advising.

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

point.

TYPES OF ADVICE

Before !

After returning !

After raising !

After (… returning or raising) !

Around

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.

ASPECTAn aspect is a collection of point cuts and their respective

advice, collected to address a specific concern.

SINGLE RESPONSIBILITY CHECK

• One responsibility

• We still have two dependencies:

• Repository object which provides persistence to our snippets.

• Logger which helps us track activity.

THE RAILS APPROACH

class Snippet < ActiveRecord::Base

end

BUT WHAT IF WANT TO USE POROS ?

–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.”

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.

–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.”

METAPROGRAMMING: TOO MUCH POWER ?

~175 uses of alias_method in Rails

METAPROGRAMMING: TOO MUCH POWER ?

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

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.

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.

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.

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.

DIY

• alias

• define_method

• adding a method to an instance

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.

AQUARIUM

AQUARIUM

RUBY PURISTS, LOOK AWAY!

RUBY ADVISING JAVA, JAVA ADVISING RUBY

• Aquarium can run on JRuby

• Ruby aspects can advise Java

• Java aspects can advise Ruby code

ASPECTOR

WHAT CROSS-CUTTING CONCERNS ?

• Persistence

• Logging

• Verification

• Policy enforcement

• Security management

TRACEPOINT

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

• Ruby 2.0

THANKS!

Stephen Best (@thebestie)

Notes, references, slides and further reading on:

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

Q&A

top related