22using System . Collections . ObjectModel ;
33using System . ComponentModel ;
44using System . Linq ;
5+ using System . Linq . Expressions ;
6+ using System . Reflection ;
57using Elasticsearch . Net . Connection ;
68using Elasticsearch . Net . ConnectionPool ;
79using Nest . Resolvers ;
@@ -25,7 +27,7 @@ public class ConnectionSettings : ConnectionSettings<ConnectionSettings>
2527 /// <para>You can also specify specific default index/alias names for types using .SetDefaultTypeIndices(</para>
2628 /// <para>If you do not specify this, NEST might throw a runtime exception if an explicit indexname was not provided for a call</para>
2729 /// </param>
28- public ConnectionSettings ( Uri uri = null , string defaultIndex = null )
30+ public ConnectionSettings ( Uri uri = null , string defaultIndex = null )
2931 : base ( uri , defaultIndex )
3032 {
3133 }
@@ -39,18 +41,19 @@ public ConnectionSettings(Uri uri = null, string defaultIndex = null)
3941 /// <para>You can also specify specific default index/alias names for types using .SetDefaultTypeIndices(</para>
4042 /// <para>If you do not specify this, NEST might throw a runtime exception if an explicit indexname was not provided for a call</para>
4143 /// </param>
42- public ConnectionSettings ( IConnectionPool connectionPool , string defaultIndex = null ) : base ( connectionPool , defaultIndex )
44+ public ConnectionSettings ( IConnectionPool connectionPool , string defaultIndex = null )
45+ : base ( connectionPool , defaultIndex )
4346 {
44-
47+
4548 }
4649 }
4750 /// <summary>
4851 /// Control how NEST's behaviour.
4952 /// </summary>
5053 [ Browsable ( false ) ]
5154 [ EditorBrowsable ( EditorBrowsableState . Never ) ]
52- public class ConnectionSettings < T > : ConnectionConfiguration < T > , IConnectionSettingsValues
53- where T : ConnectionSettings < T >
55+ public class ConnectionSettings < T > : ConnectionConfiguration < T > , IConnectionSettingsValues
56+ where T : ConnectionSettings < T >
5457 {
5558 private string _defaultIndex ;
5659 string IConnectionSettingsValues . DefaultIndex
@@ -87,24 +90,28 @@ string IConnectionSettingsValues.DefaultIndex
8790 private ReadOnlyCollection < Func < Type , JsonConverter > > _contractConverters ;
8891 ReadOnlyCollection < Func < Type , JsonConverter > > IConnectionSettingsValues . ContractConverters { get { return _contractConverters ; } }
8992
90- public ConnectionSettings ( IConnectionPool connectionPool , string defaultIndex ) : base ( connectionPool )
93+ private FluentDictionary < MemberInfo , string > _propertyNames = new FluentDictionary < MemberInfo , string > ( ) ;
94+ FluentDictionary < MemberInfo , string > IConnectionSettingsValues . PropertyNames { get { return _propertyNames ; } }
95+
96+ public ConnectionSettings ( IConnectionPool connectionPool , string defaultIndex )
97+ : base ( connectionPool )
9198 {
9299 if ( ! defaultIndex . IsNullOrEmpty ( ) )
93100 this . SetDefaultIndex ( defaultIndex ) ;
94-
95- this . _defaultTypeNameInferrer = ( t => t . Name . ToLowerInvariant ( ) ) ;
96- this . _defaultPropertyNameInferrer = ( p => p . ToCamelCase ( ) ) ;
101+
102+ this . _defaultTypeNameInferrer = ( t => t . Name . ToLowerInvariant ( ) ) ;
103+ this . _defaultPropertyNameInferrer = ( p => p . ToCamelCase ( ) ) ;
97104 this . _defaultIndices = new FluentDictionary < Type , string > ( ) ;
98105 this . _defaultTypeNames = new FluentDictionary < Type , string > ( ) ;
99106
100107 this . _modifyJsonSerializerSettings = ( j ) => { } ;
101108 this . _contractConverters = Enumerable . Empty < Func < Type , JsonConverter > > ( ) . ToList ( ) . AsReadOnly ( ) ;
102109 this . _inferrer = new ElasticInferrer ( this ) ;
103110 }
104- public ConnectionSettings ( Uri uri , string defaultIndex )
111+ public ConnectionSettings ( Uri uri , string defaultIndex )
105112 : this ( new SingleNodeConnectionPool ( uri ?? new Uri ( "http://localhost:9200" ) ) , defaultIndex )
106113 {
107-
114+
108115 }
109116
110117 /// <summary>
@@ -197,5 +204,34 @@ public T MapDefaultTypeNames(Action<FluentDictionary<Type, string>> mappingSelec
197204 mappingSelector ( this . _defaultTypeNames ) ;
198205 return ( T ) this ;
199206 }
207+
208+ public T MapPropertyNamesFor < TDocument > ( Action < FluentDictionary < Expression < Func < TDocument , object > > , string > > propertiesSelector )
209+ {
210+ propertiesSelector . ThrowIfNull ( "propertiesSelector" ) ;
211+ var properties = new FluentDictionary < Expression < Func < TDocument , object > > , string > ( ) ;
212+ propertiesSelector ( properties ) ;
213+ foreach ( var p in properties )
214+ {
215+ var e = p . Key ;
216+ var memberInfoResolver = new MemberInfoResolver ( this , e ) ;
217+ if ( memberInfoResolver . Members . Count > 1 )
218+ throw new ArgumentException ( "MapPropertyNameFor can only map direct properties" ) ;
219+
220+ if ( memberInfoResolver . Members . Count < 1 )
221+ throw new ArgumentException ( "Expression {0} does contain any member access" . F ( e ) ) ;
222+
223+ var memberInfo = memberInfoResolver . Members . Last ( ) ;
224+ if ( _propertyNames . ContainsKey ( memberInfo ) )
225+ {
226+ var mappedAs = _propertyNames [ memberInfo ] ;
227+ var typeName = typeof ( TDocument ) . Name ;
228+ throw new ArgumentException ( "Property mapping '{0}' on type {3} can not be mapped to '{1}' already mapped as '{2}'"
229+ . F ( e , p . Value , mappedAs , typeName ) ) ;
230+ }
231+ _propertyNames . Add ( memberInfo , p . Value ) ;
232+
233+ }
234+ return ( T ) this ;
235+ }
200236 }
201237}
0 commit comments