summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/DataAccess/Repositories/CustomerRepository.cs
blob: ade3e8328fc71df28ddabf84c726715954c6ddd8 (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
43
44
45
46
47
48
49
50
51
using Marina.DataAccess.DataMappers;
using Marina.Domain;
using Marina.Domain.Interfaces;
using Marina.Domain.Repositories;
using Marina.Infrastructure.Container;

namespace Marina.DataAccess.Repositories {
	public class CustomerRepository : ICustomerRepository {
		public CustomerRepository()
			: this( new IdentityMap< ICustomer >( ), Resolve.DependencyFor< ICustomerDataMapper >( ) ) {}

		public CustomerRepository( IIdentityMap< ICustomer > identityMap, ICustomerDataMapper mapper ) {
			_identityMap = identityMap;
			_mapper = mapper;
		}

		public ICustomer FindBy( long customerId ) {
			if ( _identityMap.ContainsObjectWithIdOf( customerId ) ) {
				return _identityMap.FindObjectWithIdOf( customerId );
			}
			return FindCustomerBy( customerId );
		}

		public ICustomer FindBy( string username ) {
			return _mapper.FindBy( username );
		}

		public void Save( ICustomer customer ) {
			if ( _identityMap.ContainsObjectWithIdOf( customer.ID( ) ) ) {
				_mapper.Update( customer );
			}
			else {
				_mapper.Insert( customer );
				_identityMap.Add( customer );
			}
		}

		public ICustomer NewCustomer() {
			return new Customer( );
		}

		private ICustomer FindCustomerBy( long customerId ) {
			ICustomer customer = _mapper.FindBy( customerId );
			_identityMap.Add( customer );
			return customer;
		}

		private readonly IIdentityMap< ICustomer > _identityMap;
		private readonly ICustomerDataMapper _mapper;
	}
}