diff options
| author | mokhan <mokhan@da190166-9cfc-4ee1-ae03-434a172be219> | 2009-02-21 21:44:27 +0000 |
|---|---|---|
| committer | mokhan <mokhan@da190166-9cfc-4ee1-ae03-434a172be219> | 2009-02-21 21:44:27 +0000 |
| commit | 1dfdccb8118aeaa3cd844ac8de2a672c93312166 (patch) | |
| tree | 4b19e7f816ab1019f180a46b68572af4b66fe4bc /slips/src/app/Marina/DataAccess/DataMappers | |
| parent | 42d66bcab8262c7b8b2452615df535e694a3ec1c (diff) | |
git-svn-id: http://svn.xp-dev.com/svn/mokhan-sait@2 da190166-9cfc-4ee1-ae03-434a172be219
Diffstat (limited to 'slips/src/app/Marina/DataAccess/DataMappers')
13 files changed, 550 insertions, 0 deletions
diff --git a/slips/src/app/Marina/DataAccess/DataMappers/BoatDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/BoatDataMapper.cs new file mode 100644 index 0000000..fbd33cd --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/BoatDataMapper.cs @@ -0,0 +1,74 @@ +using System;
+using System.Collections.Generic;
+using Marina.DataAccess.Builders;
+using Marina.DataAccess.Schemas;
+using Marina.Domain;
+using Marina.Domain.Interfaces;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.DataMappers {
+ public class BoatDataMapper : IBoatDataMapper {
+ public BoatDataMapper() : this( Resolve.DependencyFor< IDatabaseGateway >( ) ) {}
+
+ public BoatDataMapper( IDatabaseGateway gateway ) {
+ _gateway = gateway;
+ }
+
+ public IEnumerable< IBoat > AllBoatsFor( long customerId ) {
+ return Map.From( _gateway.FindAllRowsMatching( Queries.SelectBoatsFor( customerId ) ) );
+ }
+
+ public void Insert( IEnumerable< IBoat > boats, long forCustomerId ) {
+ _gateway.Execute( Queries.InsertQueriesFor( boats, forCustomerId ) );
+ }
+
+ public void Update( IEnumerable< IBoat > boats, long forCustomerId ) {
+ using ( IDatabaseTransaction transaction = new DatabaseTransaction( ) ) {
+ _gateway.Execute( DatabaseDelete.Where( BoatTable.CustomerID, forCustomerId ) );
+ Insert( boats, forCustomerId );
+ transaction.Commit( );
+ }
+ }
+
+ private readonly IDatabaseGateway _gateway;
+
+ private class Queries {
+ public static IQuery SelectBoatsFor( long customerId ) {
+ return DatabaseSelect.From( BoatTable.TableName )
+ .AddColumn( BoatTable.Length )
+ .AddColumn( BoatTable.Manufacturer )
+ .AddColumn( BoatTable.ModelYear )
+ .AddColumn( BoatTable.RegistrationNumber )
+ .AddColumn( BoatTable.BoatID )
+ .Where( BoatTable.CustomerID, customerId.ToString( ) ).Build( );
+ }
+
+ public static IEnumerable< IQuery > InsertQueriesFor( IEnumerable< IBoat > boats, long customerId ) {
+ foreach ( IBoat boat in boats ) {
+ yield return DatabaseInsert.Into( BoatTable.TableName )
+ .AddValue( BoatTable.CustomerID, customerId )
+ .AddValue( BoatTable.Length, boat.LengthInFeet( ) )
+ .AddValue( BoatTable.Manufacturer, boat.Manufacturer( ) )
+ .AddValue( BoatTable.ModelYear, boat.YearOfModel( ).Year )
+ .AddValue( BoatTable.RegistrationNumber, boat.RegistrationNumber( ) ).Build( );
+ }
+ }
+ }
+
+ private class Map {
+ public static IEnumerable< IBoat > From( IEnumerable< IDatabaseRow > rows ) {
+ foreach ( IDatabaseRow dataRow in rows ) {
+ yield return new Boat(
+ dataRow.From< long >( BoatTable.BoatID ),
+ dataRow.From< string >( BoatTable.RegistrationNumber ),
+ dataRow.From< string >( BoatTable.Manufacturer ),
+ dataRow.From< int >( BoatTable.ModelYear ) != 0
+ ? new DateTime( dataRow.From< int >( BoatTable.ModelYear ), 1, 01 )
+ : DateTime.MinValue,
+ dataRow.From< long >( BoatTable.Length )
+ );
+ }
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/CustomerDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/CustomerDataMapper.cs new file mode 100644 index 0000000..cdc56be --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/CustomerDataMapper.cs @@ -0,0 +1,82 @@ +using Marina.DataAccess.Builders;
+using Marina.DataAccess.Schemas;
+using Marina.Domain;
+using Marina.Domain.Interfaces;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.DataMappers {
+ public class CustomerDataMapper : ICustomerDataMapper {
+ public CustomerDataMapper() : this
+ (
+ Resolve.DependencyFor< IDatabaseGateway >( ),
+ Resolve.DependencyFor< IBoatDataMapper >( ),
+ Resolve.DependencyFor< ILeaseDataMapper >( ),
+ Resolve.DependencyFor< IRegistrationDataMapper >( )
+ ) {}
+
+ public CustomerDataMapper( IDatabaseGateway gateway, IBoatDataMapper boatMapper, ILeaseDataMapper leaseMapper,
+ IRegistrationDataMapper registrationMapper ) {
+ _gateway = gateway;
+ _boatMapper = boatMapper;
+ _leaseMapper = leaseMapper;
+ _registrationMapper = registrationMapper;
+ }
+
+ public ICustomer FindBy( long customerId ) {
+ if ( customerId > 0 ) {
+ return new Customer( customerId,
+ _boatMapper.AllBoatsFor( customerId ),
+ _leaseMapper.AllLeasesFor( customerId ),
+ _registrationMapper.For( customerId ) );
+ }
+ return null;
+ }
+
+ public ICustomer FindBy( string username ) {
+ IDatabaseRow row = _gateway.LoadRowUsing( Queries.SelectCustomerBy( username ) );
+ return FindBy( row.From< long >( AuthorizationTable.CustomerID ) );
+ }
+
+ public void Insert( ICustomer customer ) {
+ using ( IDatabaseTransaction workUnit = new DatabaseTransaction( ) ) {
+ customer.ChangeIdTo( _gateway.ExecuteScalar( Queries.Insert( ) ) );
+ _registrationMapper.Insert( customer.Registration( ), customer.ID( ) );
+ _boatMapper.Insert( customer.RegisteredBoats( ), customer.ID( ) );
+ _leaseMapper.Insert( customer.Leases( ), customer.ID( ) );
+ workUnit.Commit( );
+ }
+ }
+
+ public void Update( ICustomer customer ) {
+ using ( IDatabaseTransaction workUnit = new DatabaseTransaction( ) ) {
+ _registrationMapper.Update( customer.Registration( ), customer.ID( ) );
+ _boatMapper.Update( customer.RegisteredBoats( ), customer.ID( ) );
+ _leaseMapper.Update( customer.Leases( ), customer.ID( ) );
+ workUnit.Commit( );
+ }
+ }
+
+ private readonly IDatabaseGateway _gateway;
+ private readonly IBoatDataMapper _boatMapper;
+ private readonly ILeaseDataMapper _leaseMapper;
+ private readonly IRegistrationDataMapper _registrationMapper;
+
+ private static class Queries {
+ public static IQuery Insert() {
+ return DatabaseInsert.Into( CustomerTable.TableName )
+ .AddValue( CustomerTable.FirstName, string.Empty )
+ .AddValue( CustomerTable.LastName, string.Empty )
+ .AddValue( CustomerTable.Phone, string.Empty )
+ .AddValue( CustomerTable.City, string.Empty ).Build( );
+ }
+
+ public static IQuery SelectCustomerBy( string username ) {
+ return DatabaseSelect.From( AuthorizationTable.TableName )
+ .AddColumn( AuthorizationTable.CustomerID )
+ .AddColumn( AuthorizationTable.UserName )
+ .Where( AuthorizationTable.UserName, "'" + username + "'" )
+ .Build( );
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/DockDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/DockDataMapper.cs new file mode 100644 index 0000000..bf2ae13 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/DockDataMapper.cs @@ -0,0 +1,54 @@ +using Marina.DataAccess.Builders;
+using Marina.DataAccess.Schemas;
+using Marina.Domain;
+using Marina.Domain.Interfaces;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.DataMappers {
+ public class DockDataMapper : IDockDataMapper {
+ public DockDataMapper() : this( Resolve.DependencyFor< IDatabaseGateway >( ) ) {}
+
+ public DockDataMapper( IDatabaseGateway gateway ) {
+ _gateway = gateway;
+ }
+
+ public IDock FindBy( long dockId ) {
+ return Map.From( _gateway.LoadRowUsing( Queries.SelectDockBy( dockId ) ) );
+ }
+
+ private readonly IDatabaseGateway _gateway;
+
+ private static class Queries {
+ public static IQuery SelectDockBy( long dockId ) {
+ return DatabaseSelect
+ .From( DockTable.TableName )
+ .AddColumn( DockTable.DockID )
+ .AddColumn( DockTable.DockName )
+ .AddColumn( DockTable.LocationId )
+ .AddColumn( DockTable.WaterService )
+ .AddColumn( DockTable.ElectricalService )
+ .AddColumn( LocationTable.Name )
+ .InnerJoinOn( LocationTable.ID, DockTable.LocationId )
+ .Where( DockTable.DockID, dockId ).Build( );
+ }
+ }
+
+ private class Map {
+ public static IDock From( IDatabaseRow row ) {
+ return new Dock(
+ row.From< long >( DockTable.DockID ),
+ row.From< string >( DockTable.DockName ),
+ new Location( row.From< string >( LocationTable.Name ) ),
+ GetEnabledUtilities( row )
+ );
+ }
+
+ private static IUtility GetEnabledUtilities( IDatabaseRow row ) {
+ return Utilities.For(
+ row.From< bool >( DockTable.WaterService ) ? Utilities.Water : null,
+ row.From< bool >( DockTable.ElectricalService ) ? Utilities.Electrical : null
+ );
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/IBoatDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/IBoatDataMapper.cs new file mode 100644 index 0000000..bcdb7d1 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/IBoatDataMapper.cs @@ -0,0 +1,12 @@ +using System.Collections.Generic;
+using Marina.Domain.Interfaces;
+
+namespace Marina.DataAccess.DataMappers {
+ public interface IBoatDataMapper {
+ IEnumerable< IBoat > AllBoatsFor( long customerId );
+
+ void Insert( IEnumerable< IBoat > boats, long forCustomerId );
+
+ void Update( IEnumerable< IBoat > boats, long forCustomerId );
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/ICustomerDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/ICustomerDataMapper.cs new file mode 100644 index 0000000..fc757e7 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/ICustomerDataMapper.cs @@ -0,0 +1,11 @@ +using Marina.Domain.Interfaces;
+
+namespace Marina.DataAccess.DataMappers {
+ public interface ICustomerDataMapper : IDataMapper< ICustomer > {
+ void Insert( ICustomer customer );
+
+ void Update( ICustomer customer );
+
+ ICustomer FindBy( string username );
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/IDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/IDataMapper.cs new file mode 100644 index 0000000..249eeb7 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/IDataMapper.cs @@ -0,0 +1,5 @@ +namespace Marina.DataAccess.DataMappers {
+ public interface IDataMapper< T > {
+ T FindBy( long id );
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/IDockDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/IDockDataMapper.cs new file mode 100644 index 0000000..5140eb1 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/IDockDataMapper.cs @@ -0,0 +1,6 @@ +using Marina.DataAccess.DataMappers;
+using Marina.Domain.Interfaces;
+
+namespace Marina.DataAccess.DataMappers {
+ public interface IDockDataMapper : IDataMapper< IDock > {}
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/ILeaseDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/ILeaseDataMapper.cs new file mode 100644 index 0000000..c7134d7 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/ILeaseDataMapper.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic;
+using Marina.Domain.Interfaces;
+
+namespace Marina.DataAccess.DataMappers {
+ public interface ILeaseDataMapper {
+ IEnumerable< ISlipLease > AllLeasesFor( long customerId );
+
+ void Insert( IEnumerable< ISlipLease > leases, long forCustomerId );
+
+ void Update( IEnumerable< ISlipLease > leases, long forCustomerId );
+
+ bool IsLeased( long slipId );
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/IRegistrationDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/IRegistrationDataMapper.cs new file mode 100644 index 0000000..a9f01dc --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/IRegistrationDataMapper.cs @@ -0,0 +1,11 @@ +using Marina.Domain.Interfaces;
+
+namespace Marina.DataAccess.DataMappers {
+ public interface IRegistrationDataMapper {
+ IRegistration For( long customerId );
+
+ void Insert( IRegistration registration, long forCustomerId );
+
+ void Update( IRegistration registration, long forCustomerId );
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/ISlipDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/ISlipDataMapper.cs new file mode 100644 index 0000000..99ff069 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/ISlipDataMapper.cs @@ -0,0 +1,9 @@ +using Marina.DataAccess.DataMappers;
+using Marina.Domain.Interfaces;
+using Marina.Infrastructure;
+
+namespace Marina.DataAccess.DataMappers {
+ public interface ISlipDataMapper : IDataMapper< ISlip > {
+ IRichEnumerable< ISlip > AllSlips();
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/LeaseDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/LeaseDataMapper.cs new file mode 100644 index 0000000..8214b05 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/LeaseDataMapper.cs @@ -0,0 +1,87 @@ +using System;
+using System.Collections.Generic;
+using Marina.DataAccess.Builders;
+using Marina.DataAccess.Schemas;
+using Marina.Domain;
+using Marina.Domain.Interfaces;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.DataMappers {
+ public class LeaseDataMapper : ILeaseDataMapper {
+ public LeaseDataMapper()
+ : this( Resolve.DependencyFor< IDatabaseGateway >( ), Resolve.DependencyFor< ISlipDataMapper >( ) ) {}
+
+ public LeaseDataMapper( IDatabaseGateway gateway, ISlipDataMapper slipMapper ) {
+ _gateway = gateway;
+ _slipMapper = slipMapper;
+ }
+
+ public IEnumerable< ISlipLease > AllLeasesFor( long customerId ) {
+ foreach ( IDatabaseRow row in _gateway.FindAllRowsMatching( Queries.SelectLeasesFor( customerId ) ) ) {
+ yield return
+ new SlipLease(
+ _slipMapper.FindBy( row.From< long >( LeaseTable.SlipID ) ),
+ LeaseDurations.FindFor( row.From< DateTime >( LeaseTable.StartDate ), row.From< DateTime >( LeaseTable.EndDate ) ),
+ row.From< DateTime >( LeaseTable.StartDate ),
+ row.From< DateTime >( LeaseTable.EndDate )
+ );
+ }
+ }
+
+ public void Insert( IEnumerable< ISlipLease > leases, long forCustomerId ) {
+ using ( IDatabaseTransaction transaction = new DatabaseTransaction( ) ) {
+ IList< IQuery > queries = new List< IQuery >( );
+ foreach ( ISlipLease lease in leases ) {
+ queries.Add(
+ DatabaseInsert.Into( LeaseTable.TableName )
+ .AddValue( LeaseTable.StartDate, lease.StartDate( ) )
+ .AddValue( LeaseTable.EndDate, lease.ExpiryDate( ) )
+ .AddValue( LeaseTable.SlipID, lease.Slip( ).ID( ) )
+ .AddValue( LeaseTable.CustomerID, forCustomerId )
+ .AddValue( LeaseTable.LeaseTypeID, lease.Duration( ).ID( ) ).Build( )
+ );
+ }
+ _gateway.Execute( queries );
+ transaction.Commit( );
+ }
+ }
+
+ public void Update( IEnumerable< ISlipLease > leases, long forCustomerId ) {
+ using ( IDatabaseTransaction transaction = new DatabaseTransaction( ) ) {
+ _gateway.Execute( DatabaseDelete.Where( LeaseTable.CustomerID, forCustomerId ) );
+ Insert( leases, forCustomerId );
+ transaction.Commit( );
+ }
+ }
+
+ public bool IsLeased( long slipId ) {
+ IQuery query =
+ DatabaseSelect.From( LeaseTable.TableName ).AddColumn( LeaseTable.SlipID ).Where( LeaseTable.SlipID, slipId ).Build( );
+
+ return !_gateway.LoadRowUsing( query ).Equals( DatabaseRow.Blank );
+ }
+
+ private readonly IDatabaseGateway _gateway;
+ private readonly ISlipDataMapper _slipMapper;
+
+ private static class Queries {
+ public static IQuery SelectLeasesFor( long customerId ) {
+ return DatabaseSelect
+ .From( LeaseTable.TableName )
+ .AddColumn( LeaseTable.EndDate )
+ .AddColumn( LeaseTable.ID )
+ .AddColumn( LeaseTable.LeaseTypeID )
+ .AddColumn( LeaseTable.SlipID )
+ .AddColumn( LeaseTable.StartDate )
+ .Where( LeaseTable.CustomerID, customerId ).Build( );
+ }
+
+ public static IQuery SelectLeaseFor( long slipId ) {
+ return DatabaseSelect.From( LeaseTable.TableName )
+ .AddColumn( LeaseTable.StartDate )
+ .AddColumn( LeaseTable.EndDate )
+ .Where( LeaseTable.SlipID, slipId ).Build( );
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/RegistrationDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/RegistrationDataMapper.cs new file mode 100644 index 0000000..5c2cd41 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/RegistrationDataMapper.cs @@ -0,0 +1,88 @@ +using Marina.DataAccess.Builders;
+using Marina.DataAccess.Schemas;
+using Marina.Domain;
+using Marina.Domain.Interfaces;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.DataMappers {
+ public class RegistrationDataMapper : IRegistrationDataMapper {
+ public RegistrationDataMapper() : this( Resolve.DependencyFor< IDatabaseGateway >( ) ) {}
+
+ public RegistrationDataMapper( IDatabaseGateway gateway ) {
+ _gateway = gateway;
+ }
+
+ public IRegistration For( long customerId ) {
+ return Mappers.From( _gateway.LoadRowUsing( Queries.SelectRegistrationFor( customerId ) ) );
+ }
+
+ public void Insert( IRegistration registration, long forCustomerId ) {
+ using ( IDatabaseTransaction unitOfWork = new DatabaseTransaction( ) ) {
+ _gateway.Execute( Queries.Insert( registration, forCustomerId ),
+ Queries.UpdateCustomer( registration, forCustomerId ) );
+ unitOfWork.Commit( );
+ }
+ }
+
+ public void Update( IRegistration registration, long forCustomerId ) {
+ using ( IDatabaseTransaction unitOfWork = new DatabaseTransaction( ) ) {
+ _gateway.Execute( Queries.UpdateCustomer( registration, forCustomerId ),
+ Queries.UpdateAuthorization( registration, forCustomerId ) );
+ unitOfWork.Commit( );
+ }
+ }
+
+ private readonly IDatabaseGateway _gateway;
+
+ private class Queries {
+ public static IQuery SelectRegistrationFor( long customerId ) {
+ return DatabaseSelect.From( CustomerTable.TableName )
+ .AddColumn( CustomerTable.FirstName )
+ .AddColumn( CustomerTable.LastName )
+ .AddColumn( CustomerTable.Phone )
+ .AddColumn( CustomerTable.City )
+ .AddColumn( AuthorizationTable.UserName )
+ .AddColumn( AuthorizationTable.Password )
+ .InnerJoinOn( AuthorizationTable.CustomerID, CustomerTable.CustomerID )
+ .Where( CustomerTable.CustomerID, customerId.ToString( ) ).Build( );
+ }
+
+ public static IQuery Insert( IRegistration registration, long forCustomerId ) {
+ return DatabaseInsert.Into( AuthorizationTable.TableName )
+ .AddValue( AuthorizationTable.CustomerID, forCustomerId.ToString( ) )
+ .AddValue( AuthorizationTable.UserName, registration.Username( ) )
+ .AddValue( AuthorizationTable.Password, registration.Password( ) )
+ .Build( );
+ }
+
+ public static IQuery UpdateCustomer( IRegistration registration, long forCustomerId ) {
+ return DatabaseUpdate.Where( CustomerTable.CustomerID, forCustomerId )
+ .Add( CustomerTable.FirstName, registration.FirstName( ) )
+ .Add( CustomerTable.LastName, registration.LastName( ) )
+ .Add( CustomerTable.Phone, registration.PhoneNumber( ) )
+ .Add( CustomerTable.City, registration.City( ) )
+ .Build( );
+ }
+
+ public static IQuery UpdateAuthorization( IRegistration registration, long forCustomerId ) {
+ return DatabaseUpdate.Where( AuthorizationTable.CustomerID, forCustomerId )
+ .Add( AuthorizationTable.UserName, registration.Username( ) )
+ .Add( AuthorizationTable.Password, registration.Password( ) )
+ .Build( );
+ }
+ }
+
+ private class Mappers {
+ public static IRegistration From( IDatabaseRow row ) {
+ return new CustomerRegistration(
+ row.From< string >( AuthorizationTable.UserName ),
+ row.From< string >( AuthorizationTable.Password ),
+ row.From< string >( CustomerTable.FirstName ),
+ row.From< string >( CustomerTable.LastName ),
+ row.From< string >( CustomerTable.Phone ),
+ row.From< string >( CustomerTable.City )
+ );
+ }
+ }
+ }
+}
\ No newline at end of file diff --git a/slips/src/app/Marina/DataAccess/DataMappers/SlipDataMapper.cs b/slips/src/app/Marina/DataAccess/DataMappers/SlipDataMapper.cs new file mode 100644 index 0000000..67f7d69 --- /dev/null +++ b/slips/src/app/Marina/DataAccess/DataMappers/SlipDataMapper.cs @@ -0,0 +1,97 @@ +using System.Collections.Generic;
+using Marina.DataAccess.Builders;
+using Marina.DataAccess.Schemas;
+using Marina.Domain;
+using Marina.Domain.Interfaces;
+using Marina.Infrastructure;
+using Marina.Infrastructure.Container;
+
+namespace Marina.DataAccess.DataMappers {
+ public class SlipDataMapper : ISlipDataMapper {
+ public SlipDataMapper( IDatabaseGateway gateway ) {
+ _gateway = gateway;
+ }
+
+ public ISlip FindBy( long slipId ) {
+ return Map.From( _gateway.LoadRowUsing( Queries.SelectSlipBy( slipId ) ) );
+ }
+
+ public IRichEnumerable< ISlip > AllSlips() {
+ return new RichEnumerable< ISlip >( FetchAllSlips( ) );
+ }
+
+ private IEnumerable< ISlip > FetchAllSlips() {
+ return Map.From( _gateway.FindAllRowsMatching( Queries.SelectAllSlips( ) ) );
+ }
+
+ private readonly IDatabaseGateway _gateway;
+
+ private static class Queries {
+ public static IQuery SelectAllSlips() {
+ return SelectAllColumns( ).Build( );
+ }
+
+ public static IQuery SelectSlipBy( long slipId ) {
+ return SelectAllColumns( ).Where( SlipTable.ID, slipId ).Build( );
+ }
+
+ private static ISelectQueryBuilder SelectAllColumns() {
+ return DatabaseSelect
+ .From( SlipTable.TableName )
+ .AddColumn( SlipTable.ID )
+ .AddColumn( SlipTable.Width )
+ .AddColumn( SlipTable.Length )
+ .AddColumn( SlipTable.DockID )
+ .AddColumn( DockTable.DockName )
+ .AddColumn( DockTable.WaterService )
+ .AddColumn( DockTable.ElectricalService )
+ .AddColumn( LocationTable.Name )
+ .InnerJoinOn( DockTable.DockID, SlipTable.DockID )
+ .InnerJoinOn( LocationTable.ID, DockTable.LocationId );
+ }
+ }
+
+ private static class Map {
+ public static IEnumerable< ISlip > From( IEnumerable< IDatabaseRow > rows ) {
+ return new EnumerableMapper< IDatabaseRow, ISlip >( new DatabaseRowToSlipMapper( ) ).MapFrom( rows );
+ }
+
+ public static ISlip From( IDatabaseRow row ) {
+ return new DatabaseRowToSlipMapper( ).MapFrom( row );
+ }
+
+ private class DatabaseRowToSlipMapper : IMapper< IDatabaseRow, ISlip > {
+ public DatabaseRowToSlipMapper()
+ : this( Resolve.DependencyFor< ILeaseDataMapper >( ) ) {}
+
+ public DatabaseRowToSlipMapper( ILeaseDataMapper leaseDataMapper ) {
+ this.leaseDataMapper = leaseDataMapper;
+ }
+
+ public ISlip MapFrom( IDatabaseRow row ) {
+ return new Slip(
+ row.From< long >( SlipTable.ID ),
+ CreateDockFrom( row ),
+ row.From< int >( SlipTable.Width ),
+ row.From< int >( SlipTable.Length ),
+ leaseDataMapper.IsLeased( row.From< long >( SlipTable.ID ) )
+ );
+ }
+
+ private static Dock CreateDockFrom( IDatabaseRow row ) {
+ return new Dock(
+ row.From< long >( DockTable.DockID ),
+ row.From< string >( DockTable.DockName ),
+ new Location( row.From< string >( LocationTable.Name ) ),
+ Utilities.For(
+ row.From< bool >( DockTable.WaterService ) ? Utilities.Water : null,
+ row.From< bool >( DockTable.ElectricalService ) ? Utilities.Electrical : null
+ )
+ );
+ }
+
+ private readonly ILeaseDataMapper leaseDataMapper;
+ }
+ }
+ }
+}
\ No newline at end of file |
