Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond the ORM - RuLu Conf 2012

Piotr Solnica
June 24, 2012
1.3k

Beyond the ORM - RuLu Conf 2012

Piotr Solnica

June 24, 2012
Tweet

Transcript

  1. 4 Facts about This Talk • It has 2 sections:

    “ActiveRecord Retrospective” and “DataMapper 2” • It has 2 obligatory pictures of a cat • It has only 1 quote from Martin Fowler’s PoEAA book • It has only 16 code examples Sunday, June 24, 12
  2. Active Record Pattern Facts • Simple ORM Pattern • Designed

    for simple usecases where the business domain is simple. ie. basic CRUD • Mixes together data & behavior • 1:1 mapping between database schema & objects (means tight coupling with the db) Sunday, June 24, 12
  3. Active Record in Rails (User.methods.sort - Object.methods.sort).count # => 391

    391 new public class methods... Sunday, June 24, 12
  4. • Persistence • Validations • Associations • Life cycle hooks

    • Typecasting • Mass-assignment security • ...and much much more! Active Record retrospective Sunday, June 24, 12
  5. Active Record retrospective • Slow test suites • Lack of

    real unit tests • God objects • Code hard to reuse and extend • Code hard to refactor Sunday, June 24, 12
  6. I don’t care if we can create a blog in

    15 minutes Sunday, June 24, 12
  7. but I do care if we can work on a

    project for 15 months and keep it in a good shape Sunday, June 24, 12
  8. DataMapper 2 “A layer of Mappers that moves data between

    objects and a database while keeping them independent of each other and the mapper itself.” Martin Fowler Sunday, June 24, 12
  9. Domain Model class City attr_reader :name, :lat, :lng def initialize(attributes)

    @name, @lat, @lng = attributes.values_at( :name, :lat, :lng ) end end Sunday, June 24, 12
  10. Domain Model class GeoLocation include Virtus::ValueObject attribute :lat, Float attribute

    :lng, Float end class City include Virtus attribute :name, String attribute :location, GeoLocation end Sunday, June 24, 12
  11. Session city = City.new( name: 'Kraków', location: { lat: 123456789.123,

    lng: 987654321.987 } ) person = Person.new(name: 'Piotr') Sunday, June 24, 12
  12. Repository • Complete support for all relational algebra operations •

    All operations can be run in-memory • Can be extended to support any kind of a datastore • Can be used with multiple different databases • Designed to support per database optimizations Sunday, June 24, 12
  13. Repository relation = Veritas::Relation.new([ [ :id, Integer ], [ :name,

    String ], [ :color, String ] ], [ [ 1, 'Nut', 'Red' ], [ 2, 'Bolt', 'Green' ], [ 3, 'Screw', 'Blue' ], [ 4, 'Screw', 'Red' ], [ 5, 'Cam', 'Blue' ], [ 6, 'Cog', 'Red' ], ] ) Sunday, June 24, 12
  14. Repository # projection new_relation = relation.project([ :id ]) # removal

    new_relation = relation.remove([ :name ]) # rename new_relation = relation.rename(id: :other_id, name: :other_name) # restriction new_relation = relation.restrict { |r| r.color.eq('Red').or(r.color.eq('Blue')) } # natural join new_relation = relation.join(other) Sunday, June 24, 12
  15. Repository include Veritas adapter = Adapter::DataObjects.new( "postgres://localhost/test") header = [

    [ :id, Integer ], [ :name, String ], [ :color, String ] ] relation = Relation::Base.new( 'items', header) gateway = Relation::Gateway.new( adapter, relation) Sunday, June 24, 12
  16. Repository new_relation = gateway.restrict { |r| r.color.eq('Red').or(r.color.eq('Blue')) } new_relation.to_a #

    SELECT "id", "name", "color" # FROM items # WHERE ( # "color" = 'Red' OR "color" = 'Blue' # ) Sunday, June 24, 12
  17. What’s Done • Veritas • Veritas DataObject Adapter for PostgreSQL

    • Veritas Optimizer • Virtus (Domain Model features) Sunday, June 24, 12
  18. Work in Progress • “write” support for Veritas • Mapper

    • Session/Unit of Work Sunday, June 24, 12
  19. Work in Progress • Validations • Migrations • Constraints •

    Extension Interfaces • More Veritas adapters (sqlite, MySQL, MongoDB, Riak etc.) Sunday, June 24, 12
  20. Development Process • All “levels” of API are treated with

    the same amount of care & love • Using metric tools to verify code quality • Mutation testing with Heckle • 100% YARD documentation coverage Sunday, June 24, 12