@@ -20,10 +20,12 @@ public ConnectionSettings(Uri uri = null)
2020 : this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) ) { }
2121
2222 public ConnectionSettings ( IConnectionPool connectionPool ) : this ( connectionPool , null , null ) { }
23+
2324 public ConnectionSettings ( IConnectionPool connectionPool , SourceSerializerFactory sourceSerializer )
2425 : this ( connectionPool , null , sourceSerializer ) { }
2526
2627 public ConnectionSettings ( IConnectionPool connectionPool , IConnection connection ) : this ( connectionPool , connection , null ) { }
28+
2729 public ConnectionSettings ( IConnectionPool connectionPool , IConnection connection , SourceSerializerFactory sourceSerializer )
2830 : this ( connectionPool , connection , sourceSerializer , null ) { }
2931
@@ -33,7 +35,6 @@ public ConnectionSettings(
3335 SourceSerializerFactory sourceSerializer ,
3436 IPropertyMappingProvider propertyMappingProvider )
3537 : base ( connectionPool , connection , sourceSerializer , propertyMappingProvider ) { }
36-
3738 }
3839
3940 /// <summary>
@@ -85,7 +86,7 @@ protected ConnectionSettingsBase(
8586 IConnection connection ,
8687 ConnectionSettings . SourceSerializerFactory sourceSerializerFactory ,
8788 IPropertyMappingProvider propertyMappingProvider
88- )
89+ )
8990 : base ( connectionPool , connection , null )
9091 {
9192 var defaultSerializer = new JsonNetSerializer ( this ) ;
@@ -113,7 +114,7 @@ IPropertyMappingProvider propertyMappingProvider
113114 public TConnectionSettings PluralizeTypeNames ( )
114115 {
115116 this . _defaultTypeNameInferrer = this . LowerCaseAndPluralizeTypeNameInferrer ;
116- return ( TConnectionSettings ) this ;
117+ return ( TConnectionSettings ) this ;
117118 }
118119
119120 /// <summary>
@@ -125,7 +126,7 @@ public TConnectionSettings PluralizeTypeNames()
125126 public TConnectionSettings DefaultIndex ( string defaultIndex )
126127 {
127128 this . _defaultIndex = defaultIndex ;
128- return ( TConnectionSettings ) this ;
129+ return ( TConnectionSettings ) this ;
129130 }
130131
131132 private string LowerCaseAndPluralizeTypeNameInferrer ( Type type )
@@ -143,7 +144,7 @@ private string LowerCaseAndPluralizeTypeNameInferrer(Type type)
143144 public TConnectionSettings DefaultFieldNameInferrer ( Func < string , string > fieldNameInferrer )
144145 {
145146 this . _defaultFieldNameInferrer = fieldNameInferrer ;
146- return ( TConnectionSettings ) this ;
147+ return ( TConnectionSettings ) this ;
147148 }
148149
149150 /// <summary>
@@ -155,17 +156,17 @@ public TConnectionSettings DefaultTypeNameInferrer(Func<Type, string> typeNameIn
155156 {
156157 typeNameInferrer . ThrowIfNull ( nameof ( typeNameInferrer ) ) ;
157158 this . _defaultTypeNameInferrer = typeNameInferrer ;
158- return ( TConnectionSettings ) this ;
159+ return ( TConnectionSettings ) this ;
159160 }
160161
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 )
169170 {
170171 objectPath . ThrowIfNull ( nameof ( objectPath ) ) ;
171172
@@ -175,17 +176,18 @@ private TConnectionSettings MapIdPropertyFor<TDocument>(Expression<Func<TDocumen
175176 if ( this . _idProperties . ContainsKey ( typeof ( TDocument ) ) )
176177 {
177178 if ( this . _idProperties [ typeof ( TDocument ) ] . Equals ( fieldName ) )
178- return ( TConnectionSettings ) this ;
179+ return ( TConnectionSettings ) this ;
179180
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.") ;
181183 }
182184
183185 this . _idProperties . Add ( typeof ( TDocument ) , fieldName ) ;
184186
185- return ( TConnectionSettings ) this ;
187+ return ( TConnectionSettings ) this ;
186188 }
187189
188- private void ApplyPropertyMappings < TDocument > ( IList < IClrTypePropertyMapping < TDocument > > mappings )
190+ private void ApplyPropertyMappings < TDocument > ( IList < IPocoPropertyMapping < TDocument > > mappings )
189191 where TDocument : class
190192 {
191193 foreach ( var mapping in mappings )
@@ -207,43 +209,86 @@ private void ApplyPropertyMappings<TDocument>(IList<IClrTypePropertyMapping<TDoc
207209 if ( mappedAs . IsNullOrEmpty ( ) && newName . IsNullOrEmpty ( ) )
208210 throw new ArgumentException ( $ "Property mapping '{ e } ' on type is already ignored") ;
209211 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") ;
211214 if ( newName . IsNullOrEmpty ( ) )
212215 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 } '") ;
214218 }
215219 _propertyMappings [ memberInfo ] = mapping . ToPropertyMapping ( ) ;
216220 }
217221 }
218222
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 )
227230 where TDocument : class
228231 {
229- var inferMapping = selector ( new ClrTypeMappingDescriptor < TDocument > ( ) ) ;
232+ var inferMapping = selector ( new PocoMappingDescriptor < TDocument > ( ) ) ;
230233 if ( ! inferMapping . IndexName . IsNullOrEmpty ( ) )
231- this . _defaultIndices . Add ( inferMapping . Type , inferMapping . IndexName ) ;
234+ this . _defaultIndices . Add ( inferMapping . ClrType , inferMapping . IndexName ) ;
232235
233236 if ( ! inferMapping . TypeName . IsNullOrEmpty ( ) )
234- this . _defaultTypeNames . Add ( inferMapping . Type , inferMapping . TypeName ) ;
237+ this . _defaultTypeNames . Add ( inferMapping . ClrType , inferMapping . TypeName ) ;
235238
236239 if ( ! inferMapping . RelationName . IsNullOrEmpty ( ) )
237- this . _defaultRelationNames . Add ( inferMapping . Type , inferMapping . RelationName ) ;
240+ this . _defaultRelationNames . Add ( inferMapping . ClrType , inferMapping . RelationName ) ;
238241
239242 if ( inferMapping . IdProperty != null )
240243 this . MapIdPropertyFor < TDocument > ( inferMapping . IdProperty ) ;
241244
242245 if ( inferMapping . Properties != null )
243246 this . ApplyPropertyMappings < TDocument > ( inferMapping . Properties ) ;
244247
245- return ( TConnectionSettings ) this ;
248+ return ( TConnectionSettings ) this ;
246249 }
247250
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+ }
248293 }
249294}
0 commit comments