File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using System ;
2+
3+ namespace Shuttle . Core . Data . Tests
4+ {
5+ public class BasicMapping
6+ {
7+ public Guid Id { get ; set ; }
8+ public string Name { get ; set ; }
9+ public int Age { get ; set ; }
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ using System . Linq ;
2+ using NUnit . Framework ;
3+
4+ namespace Shuttle . Core . Data . Tests
5+ {
6+ [ TestFixture ]
7+ public class QueryMapperFixture : Fixture
8+ {
9+ [ Test ]
10+ public void Should_be_able_to_do_basic_mapping ( )
11+ {
12+ var mapper = new QueryMapper ( new DatabaseGateway ( ) ) ;
13+
14+ var queryRow = RawQuery . Create ( @"
15+ select top 1
16+ Id,
17+ Name,
18+ Age
19+ from
20+ BasicMapping
21+ " ) ;
22+
23+ var queryRows = RawQuery . Create ( @"
24+ select
25+ Id,
26+ Name,
27+ Age
28+ from
29+ BasicMapping
30+ " ) ;
31+
32+ using ( GetDatabaseContext ( ) )
33+ {
34+ var item = mapper . MapObject < BasicMapping > ( queryRow ) ;
35+ var items = mapper . MapObjects < BasicMapping > ( queryRows ) ;
36+
37+ Assert . AreEqual ( 2 , items . Count ( ) ) ;
38+
39+ var mappedRow = mapper . MapRow < BasicMapping > ( queryRow ) ;
40+ var mappedRows = mapper . MapRows < BasicMapping > ( queryRows ) ;
41+
42+ Assert . AreEqual ( 2 , mappedRows . Count ( ) ) ;
43+ }
44+ }
45+ }
46+ }
Original file line number Diff line number Diff line change 7272 <Compile Include =" MappedDataTests.cs" />
7373 <Compile Include =" ProcedureQueryTests.cs" />
7474 <Compile Include =" Properties\AssemblyInfo.cs" />
75+ <Compile Include =" QueryMapper\BasicMapping.cs" />
76+ <Compile Include =" QueryMapper\QueryMapperFixture.cs" />
7577 <Compile Include =" RawQueryTests.cs" />
7678 <Compile Include =" Fakes\Order.cs" />
7779 <Compile Include =" Fakes\OrderAssembler.cs" />
Original file line number Diff line number Diff line change 1+ using System . Collections . Generic ;
2+
3+ namespace Shuttle . Core . Data
4+ {
5+ public interface IQueryMapper
6+ {
7+ MappedRow < T > MapRow < T > ( IQuery query ) where T : new ( ) ;
8+ IEnumerable < MappedRow < T > > MapRows < T > ( IQuery query ) where T : new ( ) ;
9+ T MapObject < T > ( IQuery query ) where T : new ( ) ;
10+ IEnumerable < T > MapObjects < T > ( IQuery query ) where T : new ( ) ;
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ using System . Collections . Generic ;
2+ using System . Data ;
3+ using System . Linq ;
4+ using System . Reflection ;
5+ using Shuttle . Core . Infrastructure ;
6+
7+ namespace Shuttle . Core . Data
8+ {
9+ public class QueryMapper : IQueryMapper
10+ {
11+ private readonly IDatabaseGateway _databaseGateway ;
12+
13+ public QueryMapper ( IDatabaseGateway databaseGateway )
14+ {
15+ Guard . AgainstNull ( databaseGateway , "databaseGateway" ) ;
16+
17+ _databaseGateway = databaseGateway ;
18+ }
19+
20+ public MappedRow < T > MapRow < T > ( IQuery query ) where T : new ( )
21+ {
22+ var row = _databaseGateway . GetSingleRowUsing ( query ) ;
23+
24+ return new MappedRow < T > ( row , Map < T > ( row ) ) ;
25+ }
26+
27+ private T Map < T > ( DataRow row ) where T : new ( )
28+ {
29+ var result = new T ( ) ;
30+ var type = typeof ( T ) ;
31+
32+ foreach ( PropertyInfo pi in type . GetProperties ( ) )
33+ {
34+ var value = row [ pi . Name ] ;
35+
36+ if ( value == null )
37+ {
38+ continue ;
39+ }
40+
41+ try
42+ {
43+ pi . SetValue ( result , value , null ) ;
44+ }
45+ catch
46+ {
47+ }
48+ }
49+
50+ return result ;
51+ }
52+
53+ public IEnumerable < MappedRow < T > > MapRows < T > ( IQuery query ) where T : new ( )
54+ {
55+ return _databaseGateway . GetRowsUsing ( query ) . Select ( row => new MappedRow < T > ( row , Map < T > ( row ) ) ) ;
56+ }
57+
58+ public T MapObject < T > ( IQuery query ) where T : new ( )
59+ {
60+ return Map < T > ( _databaseGateway . GetSingleRowUsing ( query ) ) ;
61+ }
62+
63+ public IEnumerable < T > MapObjects < T > ( IQuery query ) where T : new ( )
64+ {
65+ return _databaseGateway . GetRowsUsing ( query ) . Select ( row => Map < T > ( row ) ) ;
66+ }
67+ }
68+ }
Original file line number Diff line number Diff line change 7575 </ItemGroup >
7676 <ItemGroup >
7777 <Compile Include =" ConnectionStringService.cs" />
78+ <Compile Include =" QueryMapper.cs" />
79+ <Compile Include =" IQueryMapper.cs" />
7880 <Compile Include =" IQueryParameter.cs" />
7981 <Compile Include =" DatabaseContext.cs" />
8082 <Compile Include =" ThreadStaticDatabaseContextCache.cs" />
You can’t perform that action at this time.
0 commit comments