private void ApplyPropertyMappings <TDocument>(IList <IClrPropertyMapping <TDocument> > mappings) where TDocument : class { foreach (var mapping in mappings) { var e = mapping.Property; var memberInfoResolver = new MemberInfoResolver(e); if (memberInfoResolver.Members.Count > 1) { throw new ArgumentException($"{nameof(ApplyPropertyMappings)} can only map direct properties"); } if (memberInfoResolver.Members.Count < 1) { throw new ArgumentException($"Expression {e} does contain any member access"); } var memberInfo = memberInfoResolver.Members.Last(); // memberInfo will be the declaringType, which may not be TDocument in the case of an inherited property. // Get the correct memberinfo if (typeof(TDocument) != memberInfo.DeclaringType) { var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; memberInfo = typeof(TDocument).GetMember(memberInfo.Name, bindingFlags).First(); } if (_propertyMappings.TryGetValue(memberInfo, out var propertyMapping)) { var newName = mapping.NewName; var mappedAs = propertyMapping.Name; var typeName = typeof(TDocument).Name; if (mappedAs.IsNullOrEmpty() && newName.IsNullOrEmpty()) { throw new ArgumentException($"Property mapping '{e}' on type is already ignored"); } if (mappedAs.IsNullOrEmpty()) { throw new ArgumentException( $"Property mapping '{e}' on type {typeName} can not be mapped to '{newName}' it already has an ignore mapping"); } if (newName.IsNullOrEmpty()) { throw new ArgumentException( $"Property mapping '{e}' on type {typeName} can not be ignored it already has a mapping to '{mappedAs}'"); } throw new ArgumentException( $"Property mapping '{e}' on type {typeName} can not be mapped to '{newName}' already mapped as '{mappedAs}'"); } _propertyMappings[memberInfo] = mapping.ToPropertyMapping(); } }
private void ApplyPropertyMappings <TDocument>(IList <IClrPropertyMapping <TDocument> > mappings) where TDocument : class { foreach (var mapping in mappings) { var e = mapping.Property; var memberInfoResolver = new MemberInfoResolver(e); if (memberInfoResolver.Members.Count > 1) { throw new ArgumentException($"{nameof(ApplyPropertyMappings)} can only map direct properties"); } if (memberInfoResolver.Members.Count == 0) { throw new ArgumentException($"Expression {e} does contain any member access"); } var memberInfo = memberInfoResolver.Members[0]; if (_propertyMappings.TryGetValue(memberInfo, out var propertyMapping)) { var newName = mapping.NewName; var mappedAs = propertyMapping.Name; var typeName = typeof(TDocument).Name; if (mappedAs.IsNullOrEmpty() && newName.IsNullOrEmpty()) { throw new ArgumentException($"Property mapping '{e}' on type is already ignored"); } if (mappedAs.IsNullOrEmpty()) { throw new ArgumentException( $"Property mapping '{e}' on type {typeName} can not be mapped to '{newName}' it already has an ignore mapping"); } if (newName.IsNullOrEmpty()) { throw new ArgumentException( $"Property mapping '{e}' on type {typeName} can not be ignored it already has a mapping to '{mappedAs}'"); } throw new ArgumentException( $"Property mapping '{e}' on type {typeName} can not be mapped to '{newName}' already mapped as '{mappedAs}'"); } _propertyMappings[memberInfo] = mapping.ToPropertyMapping(); } }
private void ApplyPropertyMappings <TDocument>(IList <IClrTypePropertyMapping <TDocument> > mappings) where TDocument : class { foreach (var mapping in mappings) { var e = mapping.Property; var memberInfoResolver = new MemberInfoResolver(e); if (memberInfoResolver.Members.Count > 1) { throw new ArgumentException("MapFieldNameFor can only map direct properties"); } if (memberInfoResolver.Members.Count < 1) { throw new ArgumentException("Expression {0} does contain any member access".F(e)); } var memberInfo = memberInfoResolver.Members.Last(); if (_propertyMappings.ContainsKey(memberInfo)) { var newName = mapping.NewName; var mappedAs = _propertyMappings[memberInfo].Name; var typeName = typeof(TDocument).Name; if (mappedAs.IsNullOrEmpty() && newName.IsNullOrEmpty()) { throw new ArgumentException("Property mapping '{0}' on type is already ignored" .F(e, newName, mappedAs, typeName)); } if (mappedAs.IsNullOrEmpty()) { throw new ArgumentException("Property mapping '{0}' on type {3} can not be mapped to '{1}' it already has an ignore mapping" .F(e, newName, mappedAs, typeName)); } if (newName.IsNullOrEmpty()) { throw new ArgumentException("Property mapping '{0}' on type {3} can not be ignored it already has a mapping to '{2}'" .F(e, newName, mappedAs, typeName)); } throw new ArgumentException("Property mapping '{0}' on type {3} can not be mapped to '{1}' already mapped as '{2}'" .F(e, newName, mappedAs, typeName)); } _propertyMappings.Add(memberInfo, mapping.ToPropertyMapping()); } }
private void MapRoutePropertyFor <TDocument>(Expression <Func <TDocument, object> > objectPath) { objectPath.ThrowIfNull(nameof(objectPath)); var memberInfo = new MemberInfoResolver(objectPath); var fieldName = memberInfo.Members.Single().Name; if (_routeProperties.ContainsKey(typeof(TDocument))) { if (_routeProperties[typeof(TDocument)].Equals(fieldName)) { return; } throw new ArgumentException( $"Cannot map '{fieldName}' as the route property for type '{typeof(TDocument).Name}': it already has '{_routeProperties[typeof(TDocument)]}' mapped."); } _routeProperties.Add(typeof(TDocument), fieldName); }
/// <summary> /// Specify which property on a given POCO should be used to infer the id of the document when /// indexed in Elasticsearch. /// </summary> private void MapIdPropertyFor <TDocument>(Expression <Func <TDocument, object> > objectPath) { objectPath.ThrowIfNull(nameof(objectPath)); var memberInfo = new MemberInfoResolver(objectPath); var fieldName = memberInfo.Members.Single().Name; if (_idProperties.TryGetValue(typeof(TDocument), out string idPropertyFieldName)) { if (idPropertyFieldName.Equals(fieldName)) { return; } throw new ArgumentException( $"Cannot map '{fieldName}' as the id property for type '{typeof(TDocument).Name}': it already has '{_idProperties[typeof(TDocument)]}' mapped."); } _idProperties.Add(typeof(TDocument), fieldName); }
/// <summary> /// Specify which property on a given POCO should be used to infer the id of the document when /// indexed in Elasticsearch. /// </summary> /// <typeparam name="TDocument">The type of the document.</typeparam> /// <param name="objectPath">The object path.</param> /// <returns></returns> private TConnectionSettings MapIdPropertyFor <TDocument>(Expression <Func <TDocument, object> > objectPath) { objectPath.ThrowIfNull(nameof(objectPath)); var memberInfo = new MemberInfoResolver(objectPath); var fieldName = memberInfo.Members.Single().Name; if (this._idProperties.ContainsKey(typeof(TDocument))) { if (this._idProperties[typeof(TDocument)].Equals(fieldName)) { return((TConnectionSettings)this); } throw new ArgumentException($"Cannot map '{fieldName}' as the id property for type '{typeof(TDocument).Name}': it already has '{this._idProperties[typeof(TDocument)]}' mapped."); } this._idProperties.Add(typeof(TDocument), fieldName); return((TConnectionSettings)this); }