What * specifically * makes DataMapper more flexible than ActiveRecord?

I am comparing Doctrine 2 and Propel 1.5 / 1.6 , and I am looking at some templates that they use. Doctrine uses the DataMapper template, while Propel uses the ActiveRecord template. Although I see that DataMapper is much more complex, I would suggest that design flexibility is due to this complication. So far, the only legitimate reason that I have found using DataMapper over ActiveRecord is that DataMapper is better in terms of the principle of single responsibility - since the database rows are not real objects, but with Propel, which really do not care about me because he generated the code anyway.

So - what makes DataMapper more flexible?

+6
database php orm datamapper
source share
2 answers

I worked with both new Propel and Doctrine2. What DataMapper (and I mean Doctrine2) does great is that your domain objects are clean and simple, they don't extend irrelevant classes that add a few unnecessary methods to you (violating SRP, as you said). These are simple objects with several properties and several methods that are part of your business layer. And this, of course, allows you to write unit tests for them and reuse them in the future.

I would not say DataMapper is much more complicated. This is difficult if you write your own implementation of DataMapper, but Doctrine2 is easier to use than propel (perhaps, with the exception of customization, we only do this once in any case). It has an entity manager that manipulates any entities. You may have object repositories for complex queries. And so it is.

And the object is as simple as:

/** * Question * * @Entity */ class Question { /** * @Column(type="string") */ private $title; public function getTitle() { return $this->title; } public function setTitle($title) { $this->title = $title; } } 

In Propel, we will have 6 classes for this object, which will contain a lot of generated and often unused code.

What makes DataMapper more flexible? The simplicity that it provides.

+4
source share

ActiveRecord creates a data type that performs two functions: responsibility for storing data in a database and responsibility for storing data for general computing. This means that if you want to change the data store in which data is stored in the future, code that usually should not care about database behavior is subject to change.

DataMapper completely separates the given database from the set of work objects in memory, which means that the same internal working data type can be stored in several databases without changing the code, which otherwise uses the working type.

For example, with DataMapper templates, if you want to add XML export for your data tomorrow, you will implement one new data mapper, "MyDataToXmlDataMapper" or similar, which adds a new persistence format, and no other code should change. When adding such functions, ActiveRecord is required change the working data type interface.

+2
source share

All Articles