A comparison: Data Mapper vs. Active Record - Tundra blog

Feel the cooling sensation of knowing things...


It is a programming blog btw

Hot

5 Kasım 2017 Pazar

A comparison: Data Mapper vs. Active Record


Hello,
In this article I want to compare Data Mapper and Active Record patterns. For this,  I will take into account few points:
  • Learning curve
  • Performance
  • Features & Usability
So let's go into the details. I hope you will enjoy the content. I usually add a new post every 4-5 days. So, quite possible that you will find something interesting for yourself. 

Data Mapper
Basically, a Data Mapper is a Data Access Layer that performs two-ways transfer operations between a relational database and a domain layer in a system. The goal of the Data Mapper pattern is to separate memory representation and data storage from each other.

Data Mappers provide an abstraction layer that makes our system platform independent. But of course, it brings drawbacks with it. For instance, it is very hard to believe that an ORM will take into account all the optimization issues related to your database.

Active Record
In Active Record pattern, a database or a view is wrapped into a class. That means that every object represents a row in our database. That means Active Record ORMs are not really paying attention to the relations between tables in the database. For them every column is considered as a primitive value. That's why Active Record objects usually supported by models (In order to include relations between objects; you can think about it as constraints.).

For the comparison of learning curve I will be using Doctrine 2 (Data Mapper) and Eloquent (Active Record). Let's give some short information about each of them.



Doctrine 2
Doctrine 2 is an ORM (Object-Relational Mapper) for PHP. Basically it sits on top of a database abstraction layer (DBAL). One of its unique features is the Doctrine Query Language (DQL) to write queries in a object oriented SQL dialect.

Doctrine 2's initial commit was made in 2006. As the project became more mature, the adoption began to pick up. The community has become active and development has received regular contributions from various environments such as Google Summer of Code project.
Doctrine 1.0.0 was released on September 1, 2008.

The first stable version of Doctrine 2.0 was released on December 22, 2010, after 2.5 years of dedicated development starting in early 2008. 2.5 years of development may seem too long but Doctrine 2 has come back with strong features, performance improvements and more which made it a corner stone of ORMs.

A trivial information: 1. version of Doctrine was actually employing Active Recording pattern. Then in the 2. version, it is replaced with the Data Mapper pattern.

Eloquent
As written in Laravel documentation, The Eloquent ORM is a beautiful, simple ActiveRecord implementation. We believe that. In Eloquent, each database table has a corresponding "Model" which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table. That is the essence of ActiveRecord ORMs.

Eloquent  is an advanced PHP implementation of the active record pattern, providing internal methods for enforcing constraints on the relationships between database objects. Eloquent ORM presents database tables as classes, with their object instances tied to single table rows.

Learning Curve
Since the learning curve is a process which is very personal, it is hard to say something definite about it. However, we can generalize it. Basically when you started using Doctrine 2, certain things would be new for you. In my opinion these things would consume more of your time to master it:

  • The procedure of using entity manager in every queries
  • Learning DQL in order to use in queries
  • Getting accustomed to the doctrine objects, procedures such as persist, flush and others

When it comes to Eloquent, it is basically crafted for Laravel Framework. But it is possible to use it outside of Laravel too. In my opinion, there will be less things to worry about Eloquent for the beginning since Active Record ORMs let you use objects directly as arrays. However, in Eloquent few things can be time consuming for the beginning:
  • Eager loading 
  • Learning to create proper models
  • Object relations can be confusing, such as manyToMany, hasOne, hasMany, belongsTo and others
Both Doctrine 2 and Eloquent have subjects which can be time consuming. On the other hand, same query can take 2-3 less lines of code on Eloquent since there is no entity manager employed. In my humble opinion, Mastering Doctrine 2 takes additional time, also switching from Eloquent to Doctrine 2 can be more difficult for developers, since it brings completely new concepts into the game.

Performance

It is expected that ORMs have poor performance when they are compared with Raw SQL. Since they are there to eliminate the platform dependency. But when we compare different types of ORMs, we can find an optimum type for our system. Data Mappers basically employ entity managers in order to write into the database. This is in contract with the Active Record since every object represents a row.

Few things about Data Mappers:
  • Takes more time to set-up, develop
  • It is obvious that the Data mapper queries may lack optimization at certain points. That means you will have to do nothing but to write queries by hand to improve performance.

There are few points which I must make about Active Record ORMs:
  • The objects are highly coupled with DB
  • It mixes the persistence logic with the business logic, which is contrary with the SOLID principles
  • It may have performance issues as the DB grows
Features & Usability
When it comes to the number of features, Data Mappers will provide additional features such as Entity Manager, ability to use composite keys, or even DQL (But that is specific feature only for Doctrine). On the contrary, Active record ORMs. will many times contradict with SOLID principles since it does not separate business logic with persistence. As the project grows, it may create weak points in your system.

However, the biggest advantage of the Active Record ORMs is their simplicity. It is not actually  feature but can be mentioned as a pseudo-feature.

Conclusion
Like many other comparisons, it is hard to define a winner. Active Record will give you enough features to accomplish CRUD actions with less effort to set-up and develop. However, when the project gets more complicated (As usual with the projects which employs agile methodology) Data Mapper will give you boundaries to manage your project.

Even the guys who works in development of Doctrine 2 preferred Data Mapper pattern over Active Record. This is sort of a self-explanatory information. Data Mappers will provide you rich features, once you get accustomed to the usage, then you can use it anywhere and anytime.

So in my opinion Data Mappers beat Active Recorders when it comes to a serious project.

References
https://pressupinc.com/blog/2014/11/orm-patterns-active-record-data-mapper/
https://www.thecodingmachine.com/orm-active-record-and-data-mapper/
https://www.tomasvotruba.cz/blog/2017/03/27/why-is-doctrine-dying/


Hiç yorum yok:

Yorum Gönder