public interface IIdentityMap< T > { void Add( T domainObject ); bool ContainsObjectWithIdOf( long idOfObjectToFind ); T FindObjectWithIdOf( long idOfObjectToFind ); } 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 bool ContainsObjectWithIdOf( long idOfObjectToFind ) { foreach ( T item in _items ) { if ( item.ID( ).Equals( idOfObjectToFind ) ) { return true; } } return false; } 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; } [TestFixture] public class IdentityMapTest { public IIdentityMap< IDomainObject > CreateSUT() { return new IdentityMap< IDomainObject >( ); } [Test] public void Should_be_able_to_add_a_new_domain_object_to_the_identitiy_map() { IDomainObject objectThatHasBeenAddedToMap = new DomainObject( 1 ); IDomainObject objectThatHasNotBeenAddedToMap = new DomainObject( 2 ); IIdentityMap< IDomainObject > map = CreateSUT( ); map.Add( objectThatHasBeenAddedToMap ); Assert.IsTrue( map.ContainsObjectWithIdOf( objectThatHasBeenAddedToMap.ID( ) ) ); Assert.IsFalse( map.ContainsObjectWithIdOf( objectThatHasNotBeenAddedToMap.ID( ) ) ); } [Test] public void Should_return_true_if_searching_for_a_() { IIdentityMap< IDomainObject > map = CreateSUT( ); int id = 1; map.Add( new DomainObject( id ) ); Assert.IsTrue( map.ContainsObjectWithIdOf( id ) ); Assert.IsFalse( map.ContainsObjectWithIdOf( 2 ) ); } [Test] public void Should_be_able_to_retrieve_an_object_that_has_been_added_to_the_map() { IIdentityMap< IDomainObject > map = CreateSUT( ); IDomainObject objectAddedToMap = new DomainObject( 1 ); map.Add( objectAddedToMap ); Assert.AreEqual( objectAddedToMap, map.FindObjectWithIdOf( 1 ) ); } [Test] public void Should_return_null_if_the_object_is_not_in_the_map() { Assert.IsNull( CreateSUT( ).FindObjectWithIdOf( 1 ) ); } [Test] [ExpectedException( typeof( ObjectAlreadyAddedToIdentityMapException ) )] public void Should_not_be_able_to_add_an_object_that_has_already_been_added() { IIdentityMap< IDomainObject > map = CreateSUT( ); IDomainObject addedObject = new DomainObject( 1 ); map.Add( addedObject ); map.Add( addedObject ); } [Test] [ExpectedException( typeof( ObjectAlreadyAddedToIdentityMapException ) )] public void Should_not_be_able_to_add_an_object_with_the_same_id_as_one_that_was_already_added() { IIdentityMap< IDomainObject > map = CreateSUT( ); int id = 1; map.Add( new DomainObject( id ) ); map.Add( new DomainObject( id ) ); } } public interface IDomainObject { long ID(); } public class DomainObject : IDomainObject { private readonly long _id; public DomainObject( long id ) { _id = id; } public long ID() { return _id; } } public class ObjectAlreadyAddedToIdentityMapException : Exception { public ObjectAlreadyAddedToIdentityMapException( IDomainObject domainObject ) : base( "With ID Of: " + domainObject.ID( ) ) {} }