Data Mapper vs Active Record

Data Mapper vs Active Record

Using the Active Record approach, you define all your query methods inside the model itself, and you save, remove, and load objects using model methods. Example: Eloquent from Laravel.
Pro: easy and fast to use
Con: you must extend a base class and you are very tightly coupled with the framework

Using the Data Mapper approach, you define all your query methods in separate classes called “repositories”, and you save, remove, and load objects using repositories. In data mapper your entities are very dumb – they just define their properties and may have some “dummy” methods.
Pro: no coupling with framework
Cons: takes longer to setup

The Data Mapper approach helps with maintainability, which is more effective in bigger apps. The Active Record approach helps keep things simple which works well in smaller apps. And simplicity is always a key to better maintainability.

Leave a Reply

Your email address will not be published. Required fields are marked *