1- using System ;
2- using System . Collections . Generic ;
3- using System . Data ;
4- using System . Linq ;
5- using Shuttle . Core . Contract ;
6-
7- namespace Shuttle . Core . Data
8- {
9- public class DataRowMapper : IDataRowMapper
10- {
11- public MappedRow < T > MapRow < T > ( DataRow row ) where T : new ( )
12- {
13- Guard . AgainstNull ( row , nameof ( row ) ) ;
14-
15- return new MappedRow < T > ( row , Map < T > ( row ) ) ;
16- }
17-
18- public IEnumerable < MappedRow < T > > MapRows < T > ( IEnumerable < DataRow > rows ) where T : new ( )
19- {
20- return rows ? . Select ( row => new MappedRow < T > ( row , Map < T > ( row ) ) ) . ToList ( ) ?? new List < MappedRow < T > > ( ) ;
21- }
22-
23- public T MapObject < T > ( DataRow row ) where T : new ( )
24- {
25- return Map < T > ( row ) ;
26- }
27-
28- public IEnumerable < T > MapObjects < T > ( IEnumerable < DataRow > rows ) where T : new ( )
29- {
30- return rows ? . Select ( Map < T > ) . ToList ( ) ?? new List < T > ( ) ;
31- }
32-
33- public T MapValue < T > ( DataRow row )
34- {
35- return MapRowValue < T > ( row ) ;
36- }
37-
38- public IEnumerable < T > MapValues < T > ( IEnumerable < DataRow > rows )
39- {
40- return rows ? . Select ( MapRowValue < T > ) . ToList ( ) ?? new List < T > ( ) ;
41- }
42-
43- private T Map < T > ( DataRow row ) where T : new ( )
44- {
45- var result = new T ( ) ;
46- var type = typeof ( T ) ;
47-
48- foreach ( var pi in type . GetProperties ( ) )
49- {
50- try
51- {
52- var value = row . Table . Columns . Contains ( pi . Name ) ? row [ pi . Name ] : null ;
53-
54- if ( value == null )
55- {
56- continue ;
57- }
58-
59- pi . SetValue ( result , value , null ) ;
60- }
61- catch
62- {
63- // ignored
64- }
65- }
66-
67- return result ;
68- }
69-
70- private static T MapRowValue < T > ( DataRow row )
71- {
72- var underlyingSystemType = Nullable . GetUnderlyingType ( typeof ( T ) ) ?? typeof ( T ) ;
73-
74- return row ? [ 0 ] == null
75- ? default ( T )
76- : ( T ) Convert . ChangeType ( row [ 0 ] , underlyingSystemType ) ;
77- }
78- }
1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Data ;
4+ using System . Linq ;
5+ using Shuttle . Core . Contract ;
6+
7+ namespace Shuttle . Core . Data
8+ {
9+ public class DataRowMapper : IDataRowMapper
10+ {
11+ public MappedRow < T > MapRow < T > ( DataRow row ) where T : new ( )
12+ {
13+ Guard . AgainstNull ( row , nameof ( row ) ) ;
14+
15+ return new MappedRow < T > ( row , Map < T > ( row ) ) ;
16+ }
17+
18+ public IEnumerable < MappedRow < T > > MapRows < T > ( IEnumerable < DataRow > rows ) where T : new ( )
19+ {
20+ return rows ? . Select ( row => new MappedRow < T > ( row , Map < T > ( row ) ) ) . ToList ( ) ?? new List < MappedRow < T > > ( ) ;
21+ }
22+
23+ public T MapObject < T > ( DataRow row ) where T : new ( )
24+ {
25+ return Map < T > ( row ) ;
26+ }
27+
28+ public IEnumerable < T > MapObjects < T > ( IEnumerable < DataRow > rows ) where T : new ( )
29+ {
30+ return rows ? . Select ( Map < T > ) . ToList ( ) ?? new List < T > ( ) ;
31+ }
32+
33+ public T MapValue < T > ( DataRow row )
34+ {
35+ return MapRowValue < T > ( row ) ;
36+ }
37+
38+ public IEnumerable < T > MapValues < T > ( IEnumerable < DataRow > rows )
39+ {
40+ return rows ? . Select ( MapRowValue < T > ) . ToList ( ) ?? new List < T > ( ) ;
41+ }
42+
43+ private T Map < T > ( DataRow row ) where T : new ( )
44+ {
45+ if ( row == null )
46+ {
47+ return default ( T ) ;
48+ }
49+
50+ var result = new T ( ) ;
51+ var type = typeof ( T ) ;
52+
53+ foreach ( var pi in type . GetProperties ( ) )
54+ {
55+ try
56+ {
57+ var value = row . Table . Columns . Contains ( pi . Name ) ? row [ pi . Name ] : null ;
58+
59+ if ( value == null )
60+ {
61+ continue ;
62+ }
63+
64+ pi . SetValue ( result , value , null ) ;
65+ }
66+ catch
67+ {
68+ // ignored
69+ }
70+ }
71+
72+ return result ;
73+ }
74+
75+ private static T MapRowValue < T > ( DataRow row )
76+ {
77+ var underlyingSystemType = Nullable . GetUnderlyingType ( typeof ( T ) ) ?? typeof ( T ) ;
78+
79+ return row ? [ 0 ] == null
80+ ? default ( T )
81+ : ( T ) Convert . ChangeType ( row [ 0 ] , underlyingSystemType ) ;
82+ }
83+ }
7984}
0 commit comments