Пример #1
0
        protected IDictionary <object, object> FieldNameAsKeyFormat(object field, object fieldValue, Action <FluentDictionary <object, object> > moreProperties = null)
        {
            var dict = new FluentDictionary <object, object> {
                { field, fieldValue },
            };

            if (moreProperties != null)
            {
                moreProperties(dict);
            }

            var fb = (IFilter)this;

            if (fb.Cache.HasValue)
            {
                dict.Add("_cache", fb.Cache);
            }
            if (!fb.FilterName.IsNullOrEmpty())
            {
                dict.Add("_name", fb.FilterName);
            }
            if (!fb.CacheKey.IsNullOrEmpty())
            {
                dict.Add("_cache_key", fb.CacheKey);
            }
            return(dict);
        }
        /// <summary>
        /// Specify how the mapping is inferred for a given CLR type.
        /// The mapping can infer the index, type, id and relation name for a given CLR type, as well as control
        /// serialization behaviour for CLR properties.
        /// </summary>
        public TConnectionSettings DefaultMappingFor <TDocument>(Func <ClrTypeMappingDescriptor <TDocument>, IClrTypeMapping <TDocument> > selector)
            where TDocument : class
        {
            var inferMapping = selector(new ClrTypeMappingDescriptor <TDocument>());

            if (!inferMapping.IndexName.IsNullOrEmpty())
            {
                _defaultIndices.Add(inferMapping.ClrType, inferMapping.IndexName);
            }

            if (!inferMapping.TypeName.IsNullOrEmpty())
            {
                _defaultTypeNames.Add(inferMapping.ClrType, inferMapping.TypeName);
            }

            if (!inferMapping.RelationName.IsNullOrEmpty())
            {
                _defaultRelationNames.Add(inferMapping.ClrType, inferMapping.RelationName);
            }

            if (!string.IsNullOrWhiteSpace(inferMapping.IdPropertyName))
            {
                _idProperties[inferMapping.ClrType] = inferMapping.IdPropertyName;
            }

            if (inferMapping.IdProperty != null)
            {
                MapIdPropertyFor(inferMapping.IdProperty);
            }

            if (inferMapping.RoutingProperty != null)
            {
                MapRoutePropertyFor(inferMapping.RoutingProperty);
            }

            if (inferMapping.Properties != null)
            {
                ApplyPropertyMappings(inferMapping.Properties);
            }

            if (inferMapping.DisableIdInference)
            {
                _disableIdInference.Add(inferMapping.ClrType);
            }
            else
            {
                _disableIdInference.Remove(inferMapping.ClrType);
            }

            return(UpdateId());
        }
Пример #3
0
        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());
            }
        }
Пример #4
0
        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);
        }