summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/DataAccess/IdentityMap.cs
blob: c251379edcde1874564b1323e360176aa84cd6f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections.Generic;
using Marina.DataAccess.Exceptions;
using Marina.Domain.Interfaces;

namespace Marina.DataAccess {
	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;
	}
}