There has been a lot written about Object Relational Mapping — or ORM but I came up with a design that I intend to apply to my own projects and wanted to publish and share the idea.
This idea was my own; when I wrote this I hadn’t seen this idea published elsewhere. However, the idea is derived from a set of very common design patterns. It would not surprise me in the least if another designer/developer already implemented a similar idea. However, I digress…
Preliminary Information
The ORM is a method to take data stored in a relational database-type structure and represent the same data as object orientated classes.
I feel that the ten thousand foot level the idea is straight-forward. Assume that we are using the MVC (Model-View-Controller) design pattern for your application. Furthermore, lets assume that we’ve got a simple object to which will represent our Model (part of the MVC).
Lets assume our model object is the following:

Data Model Example
The idea is to take a mix of the strategy and the decorator pattern. The strategy pattern is a design pattern that allows you to define the interface for an algorithm which can be different depending on the implementation.
The idea of the decorator pattern is to simply add responsibility to a class (through inheritance). My ORM is a combination of the two patterns.
The Design – a Class Diagram
The pattern is simple.

Diagram with OR Design
We use the interface to represent the ability to write out or read in with the Encoder. You’d implement the interface on to a derived class of the target class.
Why do we implement this interface on a derived class? Strictly speaking, we don’t have to. Though, this is intentional and part of the pattern that I’m trying to describe. This is the part of the strategy pattern working for us. This would allow us to implement different encoders for different types of storage mediums.
This means that we could create a set for SQL server, XML and perhaps a web service if so inclined.
Usage
The usage, I imagine, to be simple. Setup your basic interface and then derive a target class and apply the interface. Implement your interface to write the object and read the object to/from your storage medium.
One point of doing this, is that if a model object contains a collection or an instances of another model object, you can use this interface to write and retrieve that object.
An Example
In all honesty, I had hoped to put together a simple but working example of this. I’ve not had the time and I’ve been sitting on this post for a while. I wanted to get this post out. I still hope to get together a simple example but for the time being the description is all I’m posting.
If you have specific interest in an actual implemention of my concept here, please leave a message below. The interest will help modivate me to take the time to get something together.
Recent Comments