diff options
Diffstat (limited to 'slips/src/app/Marina/DataAccess/Repositories')
3 files changed, 129 insertions, 0 deletions
diff --git a/slips/src/app/Marina/DataAccess/Repositories/CustomerRepository.cs b/slips/src/app/Marina/DataAccess/Repositories/CustomerRepository.cs new file mode 100644 index 0000000..ade3e83 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/Repositories/CustomerRepository.cs @@ -0,0 +1,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;
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/Repositories/DockRepository.cs b/slips/src/app/Marina/DataAccess/Repositories/DockRepository.cs new file mode 100644 index 0000000..747ebc2 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/Repositories/DockRepository.cs @@ -0,0 +1,20 @@ +using Marina.DataAccess.DataMappers;
+using Marina.Domain.Interfaces;
+using Marina.Domain.Repositories;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.Repositories {
+ public class DockRepository : IDockRepository {
+ public DockRepository() : this( Resolve.DependencyFor< IDockDataMapper >( ) ) {}
+
+ public DockRepository( IDockDataMapper mapper ) {
+ this.mapper = mapper;
+ }
+
+ public IDock FindBy( long dockId ) {
+ return mapper.FindBy( dockId );
+ }
+
+ private readonly IDockDataMapper mapper;
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/Repositories/SlipsRepository.cs b/slips/src/app/Marina/DataAccess/Repositories/SlipsRepository.cs new file mode 100644 index 0000000..c6f0b3a --- /dev/null +++ b/slips/src/app/Marina/DataAccess/Repositories/SlipsRepository.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic;
+using Marina.DataAccess.DataMappers;
+using Marina.Domain.Interfaces;
+using Marina.Domain.Repositories;
+using Marina.Infrastructure;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.Repositories {
+ public class SlipsRepository : ISlipsRepository {
+ public SlipsRepository() : this( Resolve.DependencyFor< ISlipDataMapper >( ) ) {}
+
+ public SlipsRepository( ISlipDataMapper mapper ) {
+ this.mapper = mapper;
+ }
+
+ public IEnumerable< ISlip > AllAvailableSlips() {
+ return mapper.AllSlips( ).Where( Is.NotLeased( ) );
+ }
+
+ public IEnumerable< ISlip > AllAvailableSlipsFor( IDock dock ) {
+ return mapper.AllSlips( ).Where( Is.NotLeased( ).And( Is.OnDock( dock ) ) );
+ }
+
+ public ISlip FindBy( long slipId ) {
+ return mapper.FindBy( slipId );
+ }
+
+ private readonly ISlipDataMapper mapper;
+
+ private static class Is {
+ public static ISpecificationBuilder< ISlip > NotLeased() {
+ return new SpecificationBuilder< ISlip >( new IsNotLeased( ) );
+ }
+
+ public static ISpecificationBuilder< ISlip > OnDock( IDock dock ) {
+ return new SpecificationBuilder< ISlip >( new OnDockSpecification( dock ) );
+ }
+
+ private class IsNotLeased : ISpecification< ISlip > {
+ public bool IsSatisfiedBy( ISlip slip ) {
+ return !slip.IsLeased( );
+ }
+ }
+
+ private class OnDockSpecification : ISpecification< ISlip > {
+ private readonly IDock _dock;
+
+ public OnDockSpecification( IDock dock ) {
+ _dock = dock;
+ }
+
+ public bool IsSatisfiedBy( ISlip item ) {
+ return _dock.ID( ).Equals( item.Dock( ).ID( ) );
+ }
+ }
+ }
+ }
+}
\ No newline at end of file |
