Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Mapper Pattern

Tags:

datamapper

Up until now I've been using Active records in all my c# database driven applications. But now my application requires my persistence code being split from my business objects. I have read a lot of posts regarding Martin Fowler's data mapping pattern, but my knowledge of this pattern is still very limited.

Let's use the following example:

If I have 2 tables - Customer and CustomerParameters. The CustomerParameters table contains default Customer values for creating a new Customer.

I will then have to create a CustomersMapper class to handle all of the Customer persistence. My Customer and CustomersList class will then collaborate with this mapper class in order to persist customer data.

I have the following questions:

  1. How would I transfer raw data TO & FROM my Customer class to the mapper without breaking certain business rules? DTO's?

  2. Is it acceptable to have a SaveAll and LoadAll method in my Mapper class for updating and loading multiple customers' data? If so, in case of SaveAll, how will the mapper know when to update or insert data?

  3. Will the Customer mapper class be responsible for retrieving the default values from the CustomerParameters table as well, or will it be better to create a CustomerParameters mapper?

A O/R mapper tool is not really here. The database I'm using is Transactional and requires that I write my own Mapper Pattern.

Any ideas and comments will be greatly appreciated.

like image 373
MegaByte Avatar asked Oct 16 '08 08:10

MegaByte


People also ask

What is Data Mapper design pattern?

The Data Mapper Pattern is an architectural pattern introduced by Martin Fowler in his book Patterns of Enterprise Application Architecture. A Data Mapper is a type of Data Access Layer that performs bi-directional transfer of data between objects in memory and persistent storage.

Is mapper a design pattern?

In software engineering, the data mapper pattern is an architectural pattern. It was named by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture.

Is Data Mapper an ORM?

A data mapper is an instance of a class that implements the Opis\Orm\IDataMapper interface. This interface provides several methods that can be used to manipulate the data associated with an entity.

What is Data Mapper in Java?

The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other.


1 Answers

Shaun I would answer your questions this way:

ad 1) Mapper is responsible for creating Customer object. Your Mapper object will have something like RetrieveById method (for example). It will accept an ID and somehow (that't he responsibility of the Mapper object) construct the valid Customer object. The same is true the other way. When you call Mapper.Update method with a valid Customer object, the Mapper object is responsible for making sure that all the relevant data are persisted (wherever appropriate - db, memory, file, etc.)

ad 2) As I noted above retrieve/persist are methods on Mapper object. It is its responsibility to provide such a functionality. Therefore LoadAll, SaveAll (probably passing an array of value objects) are valid Mapper methods.

ad 3) I would say yes. But you can separate various aspects of Mapper objects into separate classes (if you want to/need to): default values, rule validation, etc.

I hope it helps. I really suggest/recommend you to read Martin Fowler's book Patterns of Enterprise Application Architecture.

like image 117
David Pokluda Avatar answered Sep 17 '22 20:09

David Pokluda