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(); if (_propertyMappings.TryGetValue(memberInfo, out IPropertyMapping 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 string 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); }