Slide 1

Slide 1 text

RWTH Aachen, Computer Science Student triAGENS GmbH, Developer moon lum moonbeamlabs by Lucas Dohmen Active Record vs. Data Mapper Patterns for Object Document Mappers

Slide 2

Slide 2 text

Ashikawa • Ruby adapter for Aran oDB • But how?

Slide 3

Slide 3 text

NoSQL Object Relational Mappers

Slide 4

Slide 4 text

So I sat down to think… Source: http://www.fotocommunity.de/pc/pc/display/24290075

Slide 5

Slide 5 text

Active Record

Slide 6

Slide 6 text

“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)

Slide 7

Slide 7 text

Domain Model • It is somethin from the domain • It encapsulates data • It adds business lo ic • It may validate the data

Slide 8

Slide 8 text

• 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…

Slide 9

Slide 9 text

# 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

Slide 10

Slide 10 text

It does too much.

Slide 11

Slide 11 text

…and it is hard to test it

Slide 12

Slide 12 text

• 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

Slide 13

Slide 13 text

Data Mapper

Slide 14

Slide 14 text

Warnin for Ruby Developers • You‘ve been lied to • DataMapper 1 implements ActiveRecord • DataMapper 2 will be the first implementation of DataMapper for Ruby

Slide 15

Slide 15 text

“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)

Slide 16

Slide 16 text

“ Good desi n is about drawin lines Uncle Bob in Architecture the Lost Years (Ruby Midwest 2011)

Slide 17

Slide 17 text

Domain Models Persistence

Slide 18

Slide 18 text

Domain Models Persistence

Slide 19

Slide 19 text

“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)

Slide 20

Slide 20 text

This is especially useful for NoSQL… • because the Mapper can choose the class dependin on the data • … or create multiple objects

Slide 21

Slide 21 text

It is also interestin … • … because the database does not validate • This is solely the responsibility of the Domain Model • We are separatin concerns

Slide 22

Slide 22 text

“ 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)

Slide 23

Slide 23 text

…which simplifies unit testin

Slide 24

Slide 24 text

# 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)

Slide 25

Slide 25 text

• 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

Slide 26

Slide 26 text

“ The database is a detail! Uncle Bob in Architecture, the Lost Years (Ruby Midwest 2011)

Slide 27

Slide 27 text

• 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

Slide 28

Slide 28 text

To conclude…

Slide 29

Slide 29 text

• 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

Slide 30

Slide 30 text

Ashikawa • Ruby adapter for Aran oDB • Both patterns will be implemented

Slide 31

Slide 31 text

Newsletter at nerdhub.de Launch: Soon!