@@ -20,10 +20,12 @@ public ConnectionSettings(Uri uri = null)
20
20
: this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) ) { }
21
21
22
22
public ConnectionSettings ( IConnectionPool connectionPool ) : this ( connectionPool , null , null ) { }
23
+
23
24
public ConnectionSettings ( IConnectionPool connectionPool , SourceSerializerFactory sourceSerializer )
24
25
: this ( connectionPool , null , sourceSerializer ) { }
25
26
26
27
public ConnectionSettings ( IConnectionPool connectionPool , IConnection connection ) : this ( connectionPool , connection , null ) { }
28
+
27
29
public ConnectionSettings ( IConnectionPool connectionPool , IConnection connection , SourceSerializerFactory sourceSerializer )
28
30
: this ( connectionPool , connection , sourceSerializer , null ) { }
29
31
@@ -33,7 +35,6 @@ public ConnectionSettings(
33
35
SourceSerializerFactory sourceSerializer ,
34
36
IPropertyMappingProvider propertyMappingProvider )
35
37
: base ( connectionPool , connection , sourceSerializer , propertyMappingProvider ) { }
36
-
37
38
}
38
39
39
40
/// <summary>
@@ -85,7 +86,7 @@ protected ConnectionSettingsBase(
85
86
IConnection connection ,
86
87
ConnectionSettings . SourceSerializerFactory sourceSerializerFactory ,
87
88
IPropertyMappingProvider propertyMappingProvider
88
- )
89
+ )
89
90
: base ( connectionPool , connection , null )
90
91
{
91
92
var defaultSerializer = new JsonNetSerializer ( this ) ;
@@ -113,7 +114,7 @@ IPropertyMappingProvider propertyMappingProvider
113
114
public TConnectionSettings PluralizeTypeNames ( )
114
115
{
115
116
this . _defaultTypeNameInferrer = this . LowerCaseAndPluralizeTypeNameInferrer ;
116
- return ( TConnectionSettings ) this ;
117
+ return ( TConnectionSettings ) this ;
117
118
}
118
119
119
120
/// <summary>
@@ -125,7 +126,7 @@ public TConnectionSettings PluralizeTypeNames()
125
126
public TConnectionSettings DefaultIndex ( string defaultIndex )
126
127
{
127
128
this . _defaultIndex = defaultIndex ;
128
- return ( TConnectionSettings ) this ;
129
+ return ( TConnectionSettings ) this ;
129
130
}
130
131
131
132
private string LowerCaseAndPluralizeTypeNameInferrer ( Type type )
@@ -143,7 +144,7 @@ private string LowerCaseAndPluralizeTypeNameInferrer(Type type)
143
144
public TConnectionSettings DefaultFieldNameInferrer ( Func < string , string > fieldNameInferrer )
144
145
{
145
146
this . _defaultFieldNameInferrer = fieldNameInferrer ;
146
- return ( TConnectionSettings ) this ;
147
+ return ( TConnectionSettings ) this ;
147
148
}
148
149
149
150
/// <summary>
@@ -155,17 +156,17 @@ public TConnectionSettings DefaultTypeNameInferrer(Func<Type, string> typeNameIn
155
156
{
156
157
typeNameInferrer . ThrowIfNull ( nameof ( typeNameInferrer ) ) ;
157
158
this . _defaultTypeNameInferrer = typeNameInferrer ;
158
- return ( TConnectionSettings ) this ;
159
+ return ( TConnectionSettings ) this ;
159
160
}
160
161
161
- /// <summary>
162
- /// Specify which property on a given POCO should be used to infer the id of the document when
163
- /// indexed in Elasticsearch.
164
- /// </summary>
165
- /// <typeparam name="TDocument">The type of the document.</typeparam>
166
- /// <param name="objectPath">The object path.</param>
167
- /// <returns></returns>
168
- private TConnectionSettings MapIdPropertyFor < TDocument > ( Expression < Func < TDocument , object > > objectPath )
162
+ /// <summary>
163
+ /// Specify which property on a given POCO should be used to infer the id of the document when
164
+ /// indexed in Elasticsearch.
165
+ /// </summary>
166
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
167
+ /// <param name="objectPath">The object path.</param>
168
+ /// <returns></returns>
169
+ private TConnectionSettings MapIdPropertyFor < TDocument > ( Expression < Func < TDocument , object > > objectPath )
169
170
{
170
171
objectPath . ThrowIfNull ( nameof ( objectPath ) ) ;
171
172
@@ -175,17 +176,18 @@ private TConnectionSettings MapIdPropertyFor<TDocument>(Expression<Func<TDocumen
175
176
if ( this . _idProperties . ContainsKey ( typeof ( TDocument ) ) )
176
177
{
177
178
if ( this . _idProperties [ typeof ( TDocument ) ] . Equals ( fieldName ) )
178
- return ( TConnectionSettings ) this ;
179
+ return ( TConnectionSettings ) this ;
179
180
180
- throw new ArgumentException ( $ "Cannot map '{ fieldName } ' as the id property for type '{ typeof ( TDocument ) . Name } ': it already has '{ this . _idProperties [ typeof ( TDocument ) ] } ' mapped.") ;
181
+ throw new ArgumentException (
182
+ $ "Cannot map '{ fieldName } ' as the id property for type '{ typeof ( TDocument ) . Name } ': it already has '{ this . _idProperties [ typeof ( TDocument ) ] } ' mapped.") ;
181
183
}
182
184
183
185
this . _idProperties . Add ( typeof ( TDocument ) , fieldName ) ;
184
186
185
- return ( TConnectionSettings ) this ;
187
+ return ( TConnectionSettings ) this ;
186
188
}
187
189
188
- private void ApplyPropertyMappings < TDocument > ( IList < IClrTypePropertyMapping < TDocument > > mappings )
190
+ private void ApplyPropertyMappings < TDocument > ( IList < IPocoPropertyMapping < TDocument > > mappings )
189
191
where TDocument : class
190
192
{
191
193
foreach ( var mapping in mappings )
@@ -207,43 +209,86 @@ private void ApplyPropertyMappings<TDocument>(IList<IClrTypePropertyMapping<TDoc
207
209
if ( mappedAs . IsNullOrEmpty ( ) && newName . IsNullOrEmpty ( ) )
208
210
throw new ArgumentException ( $ "Property mapping '{ e } ' on type is already ignored") ;
209
211
if ( mappedAs . IsNullOrEmpty ( ) )
210
- throw new ArgumentException ( $ "Property mapping '{ e } ' on type { typeName } can not be mapped to '{ newName } ' it already has an ignore mapping") ;
212
+ throw new ArgumentException (
213
+ $ "Property mapping '{ e } ' on type { typeName } can not be mapped to '{ newName } ' it already has an ignore mapping") ;
211
214
if ( newName . IsNullOrEmpty ( ) )
212
215
throw new ArgumentException ( $ "Property mapping '{ e } ' on type { typeName } can not be ignored it already has a mapping to '{ mappedAs } '") ;
213
- throw new ArgumentException ( $ "Property mapping '{ e } ' on type { typeName } can not be mapped to '{ newName } ' already mapped as '{ mappedAs } '") ;
216
+ throw new ArgumentException (
217
+ $ "Property mapping '{ e } ' on type { typeName } can not be mapped to '{ newName } ' already mapped as '{ mappedAs } '") ;
214
218
}
215
219
_propertyMappings [ memberInfo ] = mapping . ToPropertyMapping ( ) ;
216
220
}
217
221
}
218
222
219
- /// <summary>
220
- /// Specify how the mapping is inferred for a given POCO type.
221
- /// Can be used to infer the index, type, id property and properties for the POCO.
222
- /// </summary>
223
- /// <typeparam name="TDocument">The type of the document.</typeparam>
224
- /// <param name="selector">The selector.</param>
225
- /// <returns></returns>
226
- public TConnectionSettings InferMappingFor < TDocument > ( Func < ClrTypeMappingDescriptor < TDocument > , IClrTypeMapping < TDocument > > selector )
223
+ /// <summary>
224
+ /// Specify how the mapping is inferred for a given POCO type. Can be used to infer the index, type and relation names.
225
+ /// The generic version also allows you to set a default id property and control serialization behavior for properties for the POCO.
226
+ /// </summary>
227
+ /// <typeparam name="TDocument">The type of the document.</typeparam>
228
+ /// <param name="selector">The selector.</param>
229
+ public TConnectionSettings InferMappingFor < TDocument > ( Func < PocoMappingDescriptor < TDocument > , IPocoMapping < TDocument > > selector )
227
230
where TDocument : class
228
231
{
229
- var inferMapping = selector ( new ClrTypeMappingDescriptor < TDocument > ( ) ) ;
232
+ var inferMapping = selector ( new PocoMappingDescriptor < TDocument > ( ) ) ;
230
233
if ( ! inferMapping . IndexName . IsNullOrEmpty ( ) )
231
- this . _defaultIndices . Add ( inferMapping . Type , inferMapping . IndexName ) ;
234
+ this . _defaultIndices . Add ( inferMapping . ClrType , inferMapping . IndexName ) ;
232
235
233
236
if ( ! inferMapping . TypeName . IsNullOrEmpty ( ) )
234
- this . _defaultTypeNames . Add ( inferMapping . Type , inferMapping . TypeName ) ;
237
+ this . _defaultTypeNames . Add ( inferMapping . ClrType , inferMapping . TypeName ) ;
235
238
236
239
if ( ! inferMapping . RelationName . IsNullOrEmpty ( ) )
237
- this . _defaultRelationNames . Add ( inferMapping . Type , inferMapping . RelationName ) ;
240
+ this . _defaultRelationNames . Add ( inferMapping . ClrType , inferMapping . RelationName ) ;
238
241
239
242
if ( inferMapping . IdProperty != null )
240
243
this . MapIdPropertyFor < TDocument > ( inferMapping . IdProperty ) ;
241
244
242
245
if ( inferMapping . Properties != null )
243
246
this . ApplyPropertyMappings < TDocument > ( inferMapping . Properties ) ;
244
247
245
- return ( TConnectionSettings ) this ;
248
+ return ( TConnectionSettings ) this ;
246
249
}
247
250
251
+ /// <summary>
252
+ /// Specify how the mapping is inferred for a given POCO type. Can be used to infer the index, type, and relation names.
253
+ /// </summary>
254
+ /// <param name="documentType">The type of the POCO you wish to configure</param>
255
+ /// <param name="selector">describe the POCO configuration</param>
256
+ public TConnectionSettings InferMappingFor ( Type documentType , Func < PocoMappingDescriptor , IPocoMapping > selector )
257
+ {
258
+ var inferMapping = selector ( new PocoMappingDescriptor ( documentType ) ) ;
259
+ if ( ! inferMapping . IndexName . IsNullOrEmpty ( ) )
260
+ this . _defaultIndices . Add ( inferMapping . ClrType , inferMapping . IndexName ) ;
261
+
262
+ if ( ! inferMapping . TypeName . IsNullOrEmpty ( ) )
263
+ this . _defaultTypeNames . Add ( inferMapping . ClrType , inferMapping . TypeName ) ;
264
+
265
+ if ( ! inferMapping . RelationName . IsNullOrEmpty ( ) )
266
+ this . _defaultRelationNames . Add ( inferMapping . ClrType , inferMapping . RelationName ) ;
267
+
268
+ return ( TConnectionSettings ) this ;
269
+ }
270
+
271
+ /// <summary>
272
+ /// Specify how the mapping is inferred for a given POCO type. Can be used to infer the index, type, and relation names.
273
+ /// </summary>
274
+ /// <param name="documentType">The type of the POCO you wish to configure</param>
275
+ /// <param name="selector">describe the POCO configuration</param>
276
+ public TConnectionSettings InferMappings ( IEnumerable < PocoMapping > typeMappings )
277
+ {
278
+ if ( typeMappings == null ) return ( TConnectionSettings ) this ;
279
+ foreach ( var inferMapping in typeMappings )
280
+ {
281
+ if ( ! inferMapping . IndexName . IsNullOrEmpty ( ) )
282
+ this . _defaultIndices . Add ( inferMapping . ClrType , inferMapping . IndexName ) ;
283
+
284
+ if ( ! inferMapping . TypeName . IsNullOrEmpty ( ) )
285
+ this . _defaultTypeNames . Add ( inferMapping . ClrType , inferMapping . TypeName ) ;
286
+
287
+ if ( ! inferMapping . RelationName . IsNullOrEmpty ( ) )
288
+ this . _defaultRelationNames . Add ( inferMapping . ClrType , inferMapping . RelationName ) ;
289
+ }
290
+
291
+ return ( TConnectionSettings ) this ;
292
+ }
248
293
}
249
294
}
0 commit comments