6
6
using NHibernate . Mapping . ByCode ;
7
7
using NUnit . Framework ;
8
8
9
- namespace NHibernate . Test . MappingByCode . ExpliticMappingTests
9
+ namespace NHibernate . Test . MappingByCode . ExplicitMappingTests
10
10
{
11
11
[ TestFixture ]
12
12
public class DynamicComponentMappingTests
@@ -33,6 +33,141 @@ public IDictionary<string, object> Info
33
33
}
34
34
}
35
35
36
+ private class PersonWithDynamicInfo
37
+ {
38
+ public int Id { get ; set ; }
39
+ public dynamic Info { get ; set ; }
40
+ }
41
+
42
+ [ Test ]
43
+ public void WhenMapDynCompoByDictionaryThenMapItAndItsProperties ( )
44
+ {
45
+ //NH-3704
46
+ var mapper = new ModelMapper ( ) ;
47
+ mapper . Class < Person > (
48
+ map =>
49
+ {
50
+ map . Id ( x => x . Id , idmap => { } ) ;
51
+ map . Component (
52
+ x => x . Info ,
53
+ new Dictionary < string , System . Type >
54
+ { { "MyInt" , typeof ( int ) } , { "MyDate" , typeof ( DateTime ) } } ,
55
+ z => { z . Property ( "MyInt" , pm => pm . Column ( "MY_COLUMN" ) ) ; } ) ;
56
+ } ) ;
57
+
58
+ var hbmMapping = mapper . CompileMappingFor ( new [ ] { typeof ( Person ) } ) ;
59
+ var hbmClass = hbmMapping . RootClasses [ 0 ] ;
60
+ var hbmDynamicComponent = hbmClass . Properties . OfType < HbmDynamicComponent > ( ) . SingleOrDefault ( ) ;
61
+ Assert . That ( hbmDynamicComponent , Is . Not . Null ) ;
62
+ Assert . That (
63
+ hbmDynamicComponent . Properties . Select ( x => x . Name ) ,
64
+ Is . EquivalentTo ( new [ ] { "MyInt" , "MyDate" } ) ) ;
65
+ }
66
+
67
+ [ Test ]
68
+ public void WhenMapDynCompoByDictionaryThenMapItAndItsPropertiesGeneric ( )
69
+ {
70
+ //NH-3704
71
+ var mapper = new ModelMapper ( ) ;
72
+ mapper . Class < PersonWithGenericInfo > (
73
+ map =>
74
+ {
75
+ map . Id ( x => x . Id , idmap => { } ) ;
76
+ map . Component (
77
+ x => x . Info ,
78
+ new Dictionary < string , System . Type >
79
+ { { "MyInt" , typeof ( int ) } , { "MyDate" , typeof ( DateTime ) } } ,
80
+ z => { z . Property ( "MyInt" , pm => pm . Column ( "MY_COLUMN" ) ) ; } ) ;
81
+ } ) ;
82
+
83
+ var hbmMapping = mapper . CompileMappingFor ( new [ ] { typeof ( PersonWithGenericInfo ) } ) ;
84
+ var hbmClass = hbmMapping . RootClasses [ 0 ] ;
85
+ var hbmDynamicComponent = hbmClass . Properties . OfType < HbmDynamicComponent > ( ) . SingleOrDefault ( ) ;
86
+ Assert . That ( hbmDynamicComponent , Is . Not . Null ) ;
87
+ Assert . That (
88
+ hbmDynamicComponent . Properties . Select ( x => x . Name ) ,
89
+ Is . EquivalentTo ( new [ ] { "MyInt" , "MyDate" } ) ) ;
90
+ }
91
+
92
+ [ Test ]
93
+ public void WhenMapDynCompoByDictionaryThenMapItAndItsPropertiesDynamic ( )
94
+ {
95
+ //NH-3704
96
+ var mapper = new ModelMapper ( ) ;
97
+ mapper . Class < PersonWithDynamicInfo > (
98
+ map =>
99
+ {
100
+ map . Id ( x => x . Id , idmap => { } ) ;
101
+ map . Component (
102
+ nameof ( PersonWithDynamicInfo . Info ) ,
103
+ new Dictionary < string , System . Type >
104
+ { { "MyInt" , typeof ( int ) } , { "MyDate" , typeof ( DateTime ) } } ,
105
+ z =>
106
+ {
107
+ z . Property ( "MyInt" , pm => pm . Column ( "MY_COLUMN" ) ) ;
108
+ z . Component < DateTime > ( "MyDate" ) ;
109
+ } ) ;
110
+ } ) ;
111
+
112
+ var hbmMapping = mapper . CompileMappingFor ( new [ ] { typeof ( PersonWithDynamicInfo ) } ) ;
113
+ var hbmClass = hbmMapping . RootClasses [ 0 ] ;
114
+ var hbmDynamicComponent = hbmClass . Properties . OfType < HbmDynamicComponent > ( ) . SingleOrDefault ( ) ;
115
+ Assert . That ( hbmDynamicComponent , Is . Not . Null ) ;
116
+ Assert . That (
117
+ hbmDynamicComponent . Properties . Select ( x => x . Name ) ,
118
+ Is . EquivalentTo ( new [ ] { "MyInt" , "MyDate" } ) ) ;
119
+ }
120
+
121
+ [ Test ]
122
+ public void WhenMapPrivateDynCompoByDictionaryThenMapItAndItsProperties ( )
123
+ {
124
+ //NH-3704
125
+ var mapper = new ModelMapper ( ) ;
126
+ mapper . Class < Person > (
127
+ map =>
128
+ {
129
+ map . Id ( x => x . Id , idmap => { } ) ;
130
+ map . Component (
131
+ "Info" ,
132
+ new Dictionary < string , System . Type >
133
+ { { "MyInt" , typeof ( int ) } , { "MyDate" , typeof ( DateTime ) } } ,
134
+ z => { z . Property ( "MyInt" , pm => pm . Column ( "MY_COLUMN" ) ) ; } ) ;
135
+ } ) ;
136
+
137
+ var hbmMapping = mapper . CompileMappingFor ( new [ ] { typeof ( Person ) } ) ;
138
+ var hbmClass = hbmMapping . RootClasses [ 0 ] ;
139
+ var hbmDynamicComponent = hbmClass . Properties . OfType < HbmDynamicComponent > ( ) . SingleOrDefault ( ) ;
140
+ Assert . That ( hbmDynamicComponent , Is . Not . Null ) ;
141
+ Assert . That (
142
+ hbmDynamicComponent . Properties . Select ( x => x . Name ) ,
143
+ Is . EquivalentTo ( new [ ] { "MyInt" , "MyDate" } ) ) ;
144
+ }
145
+
146
+ [ Test ]
147
+ public void WhenMapPrivateDynCompoByDictionaryThenMapItAndItsPropertiesGeneric ( )
148
+ {
149
+ //NH-3704
150
+ var mapper = new ModelMapper ( ) ;
151
+ mapper . Class < PersonWithGenericInfo > (
152
+ map =>
153
+ {
154
+ map . Id ( x => x . Id , idmap => { } ) ;
155
+ map . Component (
156
+ "Info" ,
157
+ new Dictionary < string , System . Type >
158
+ { { "MyInt" , typeof ( int ) } , { "MyDate" , typeof ( DateTime ) } } ,
159
+ z => { z . Property ( "MyInt" , pm => pm . Column ( "MY_COLUMN" ) ) ; } ) ;
160
+ } ) ;
161
+
162
+ var hbmMapping = mapper . CompileMappingFor ( new [ ] { typeof ( PersonWithGenericInfo ) } ) ;
163
+ var hbmClass = hbmMapping . RootClasses [ 0 ] ;
164
+ var hbmDynamicComponent = hbmClass . Properties . OfType < HbmDynamicComponent > ( ) . SingleOrDefault ( ) ;
165
+ Assert . That ( hbmDynamicComponent , Is . Not . Null ) ;
166
+ Assert . That (
167
+ hbmDynamicComponent . Properties . Select ( x => x . Name ) ,
168
+ Is . EquivalentTo ( new [ ] { "MyInt" , "MyDate" } ) ) ;
169
+ }
170
+
36
171
[ Test ]
37
172
public void WhenMapDynCompoThenMapItAndItsProperties ( )
38
173
{
0 commit comments