18 December 2007

The Identity Map

by mo

Patterns of Enterprise Application Architecture (The Addison-Wesley Signature Series)
by Martin Fowler

Read more about this title…

This book is amazing, I could probably read it over and over again. The patterns that it presents makes a lot of sense, the actual implementations and examples could probably re-vamped but I’m finding that it’s a great starter to trying to understand the concepts.
So today I’m trying to grok this concept of the “IDENTITY MAP”. The book defines it as:
“Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.”
I put together a quick implementation that I’m using for an assignment I’m writing for school. I’m creating repositories that have 2 dependencies. First dependency is a Data Mapper (not to be confused with a Mapper) and the second is an Identity Map.
As objects are requested from the repository, the repository is checking the identity map to see if an instance has been loaded, if it has it’s immediately returned to the caller. If the object is not in the map the repository leverages the data mapper to load the object, then adds it to the map and returns it to the caller.
The Data Mapper is defined as:

“A layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself.” - PoEAA I created a Layer Supertype to register objects in the map. The Domain Layer Super type demands that domain objects had an ID. The ID is what’s getting used to load and register objects into the map.
The Layer Supertype is defined as:

“A type that acts as a the supertype for all types in it’s layer.” - PoEAA

The Domain Layer Super Type.

  public interface IDomainObject {
    long ID();

My actual implementation of the identity map looks like this…

    public class IdentityMap> T > : IIdentityMap> T > where T : IDomainObject {
        public IdentityMap() {
            _items = new List> T >( );

        public void Add( T domainObject ) {
            EnsureObjectHasNotAlreadyBeenAdded( domainObject );
            _items.Add( domainObject );

        public T FindObjectWithIdOf( long idOfObjectToFind ) {
            foreach ( T item in _items ) {
                if ( item.ID( ).Equals( idOfObjectToFind ) ) {
                    return item;
            return default( T );

        private void EnsureObjectHasNotAlreadyBeenAdded( T domainObject ) {
            if ( ContainsObjectWithIdOf( domainObject.ID( ) ) ) {
                throw new ObjectAlreadyAddedToIdentityMapException( domainObject );

        private readonly IList> T > _items;

One of the benefits I like about the Identity Map is that it acts as an in memory cache for objects loaded from the database. This reduces the number of trips to and from the persistence store.

Source Code

books csharp designpatterns