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

Active Record vs. Data Mapper Pattern

Active Record vs. Data Mapper Pattern

I gave a talk about the two patterns Active Record and Data Mapper and compared them with special regards to using them with NoSQL Databases. The code examples are in Ruby, but it is not Ruby specific ;)

Lucas Dohmen

October 10, 2012
Tweet

More Decks by Lucas Dohmen

Other Decks in Programming

Transcript

  1. RWTH Aachen, Computer Science Student triAGENS GmbH, Developer moon lum

    moonbeamlabs by Lucas Dohmen Active Record vs. Data Mapper Patterns for Object Document Mappers
  2. “An object that wraps a row in a database table

    or view, encapsulates the database access, and adds domain lo ic on that data. Martin Fowler in Patterns of Enterprise Application Architecture (2003)
  3. Domain Model • It is somethin from the domain •

    It encapsulates data • It adds business lo ic • It may validate the data
  4. • Constructs an instance from an SQL result • Constructs

    a new instance for later insertion • Updates the databases • Wraps commonly used SQL queries In ActiveRecord it also…
  5. # The problem for example in Rails‘ ActiveRecord # (Inspired

    by solnic) require "active_record" class ClassWithActiveRecord < ActiveRecord::Base; end (ClassWithActiveRecord.methods - Object.methods).count # => 391 # It consists of 77 modules
  6. • You could also have multiple classes accessin one collection

    dependin on the data • Or nested attributes • This does not fit very well in that pattern NoSQL specific problems
  7. Warnin for Ruby Developers • You‘ve been lied to •

    DataMapper 1 implements ActiveRecord • DataMapper 2 will be the first implementation of DataMapper for Ruby
  8. “A layer of Mappers that moves data between objects and

    a database while keepin them independent of each other and the mapper itself. Martin Fowler in Patterns of Enterprise Application Architecture (2003)
  9. “ Good desi n is about drawin lines Uncle Bob

    in Architecture the Lost Years (Ruby Midwest 2011)
  10. “The Data Mapper is a layer of software that separates

    the in-memory objects from the database. Martin Fowler in Patterns of Enterprise Application Architecture (2003)
  11. This is especially useful for NoSQL… • because the Mapper

    can choose the class dependin on the data • … or create multiple objects
  12. It is also interestin … • … because the database

    does not validate • This is solely the responsibility of the Domain Model • We are separatin concerns
  13. “ With Data Mapper the in-memory objects needn’t know even

    that there’s a database present Martin Fowler in Patterns of Enterprise Application Architecture (2003)
  14. # Examples from Datamapper 2 # again from solnic #

    Define a model class User include DataMapper::Model attribute :id, Integer attribute :name, String attribute :age, Integer end # Define a mapper DataMapper.generate_mapper_for(User) do key :id map :name, :to => :username end # Search for data DataMapper[User].all DataMapper[User].order(:age) DataMapper[User].find(age: 23)
  15. • But an attribute of the model could also be

    another model • This can be used for has_many etc. • … but it is also handy in a Document Store • The model doesn‘t need to care, only the mapper has to handle the difference
  16. • Keeps track of all objects read from the database

    and their modifications • Handles how updates are made • Instead of the invokin explicit save methods, tell the unit of work to commit • Works reat with Data Mapper • Works reat with batch requests Unit of Work
  17. • ActiveRecord is simpler • Data Mapper is a better

    fit in a lot of cases • It has better support for denormalization • DataMapper and NoSQL make the database become a detail, simplifyin tests • With a Workin Set you can take full advanta e of batch requests