views:

1145

answers:

3

have you re-factored from an Active Record to a Data Mapper pattern? what conditions prompted the switch? i'm primarily interested in web based applications, but would like to know the challenges that accompany such a move in any environment.

+4  A: 

I really do like the ActiveRecord pattern for its simplicity. However, I have been moving away from it for larger web apps. I find that as an ActiveRecord based project becomes more complex the ActiveRecord objects become large and laden with too much code.

By introducing a Repository pattern (essentially a Data Mapper) the domain model classes become simpler and the data mapping / data access logic is kept separate.

Also, it is quite difficult (impossible?) to mock ActiveRecord objects because of their user of static methods.

Mike
Interesting idea! Thanks, Mike
Fedyashev Nikita
A: 

I wrote an integrated build system sitting on top of PDE-Build using camping. I initially used ActiveRecord, but I needed non-blocking thread-safe access to the database, so I switched to using Data Mapper.

I had my fair share of grief from bugs, but the latest versions seem pretty stable.

Hans Sjunnesson
A: 

I use a framework that provides Table Data Gateway and Row Data Gateway as built-in classes that are easy to use because all I have to specify is the primary key (if not just 'id') and the name of the table (if not the same as the name of the class). However, I have recently discovered in the process of refactoring that these patterns begin to degrade the moment that more complex mapping has to occur between the domain and the database.

For example, I'm currently refactoring the code for one website to use Data Mapper so that I can use Single Table Inheritance (uses Inheritance Mapping). Basically, anytime the relationship between database and domain becomes more complex than one-to-one, I would strongly consider using Data Mappers.

Noah Goodrich