Mapping rows in ORM
Marshmallow ORM framework from Spring4D just received a new interface, called IRowMapper<T>. What is it and why do we need it?
Description of the IRowMapper<T> from the official code documentation:
Represents interface for mapping rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of mapping each row to a result object.
And how it is defined in Spring.Persistence.Core.Interfaces unit:
Basically when you are using Marshmallow (MM) ORM framework, it performs all the mapping for you. You don’t need to worry how it’s done, you just need to annotate your entities with ORM specific attributes. But in some cases you might want to have a full control on the mapping process. There might be a few reasons for this:
- Since MM maps entities using RTTI, it introduces some performance penalty which may not be desirable. Mapping you entities manually can speed-up your data reads significantly.
- You don’t need to update or insert new data (of course you can also use IRowMapper only for select queries and ORM mapping for update or insert queries). If so, you could decide to go even further and do not annotate your entities with MM attributes. In this case you will need to use hand-written queries and your row mappers when retrieving data.
Suppose we have a simple TProduct class:
Now we can define our TProductRowMapper which implements IRowMapper<TProduct> interface:
And finally, we need to register it within our TSession and get the data from the database:
You can find Marshmallow in Spring4D bitbucket repository, feature/marshmallow branch. Note, that framework is under big changes regarding integration with Spring4D, so some things might not work yet.
If you have some questions of thoughts, please write a comment.