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(); } }
/// <inheritdoc cref="IConnectionSettingsValues.RouteProperties"/> 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.TryGetValue(typeof(TDocument), out var routePropertyFieldName)) { if (routePropertyFieldName.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); }