コード例 #1
0
        public void GetOrAddDescriptor_Should_ReturnDescriptorIfAlreadyExists()
        {
            //arrange
            IList <FilterFieldDescriptorBase> list = new List <FilterFieldDescriptorBase>();
            var descriptorShouldNotBeRemoved       =
                new StringFilterFieldDescriptor(
                    _descriptorContext, _propertyInfo, _filterConvention);
            var newDescriptorShouldNotHaveAnyEffect =
                new StringFilterFieldDescriptor(
                    _descriptorContext, _propertyInfo, _filterConvention);
            Func <StringFilterFieldDescriptor> valueFactory =
                () => newDescriptorShouldNotHaveAnyEffect;

            list.Add(descriptorShouldNotBeRemoved);

            //act
            StringFilterFieldDescriptor result =
                list.GetOrAddDescriptor(_propertyInfo, valueFactory);

            //assert
            Assert.Single(list);
            Assert.Same(descriptorShouldNotBeRemoved, list[0]);
            Assert.Same(descriptorShouldNotBeRemoved, result);
            Assert.NotSame(newDescriptorShouldNotHaveAnyEffect, result);
        }
コード例 #2
0
        private bool TryCreateImplicitFilter(
            PropertyInfo property,
            out FilterFieldDefintion definition)
        {
            if (property.PropertyType == typeof(string))
            {
                var field = new StringFilterFieldDescriptor(Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (property.PropertyType == typeof(bool) ||
                property.PropertyType == typeof(bool?))
            {
                var field = new BooleanFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (typeof(IComparable).IsAssignableFrom(property.PropertyType))
            {
                var field = new ComparableFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            definition = null;
            return(false);
        }
コード例 #3
0
 /// <summary>
 /// Create a new string filter operation descriptor.
 /// </summary>
 /// <param name="context">
 /// The descriptor context.
 /// </param>
 /// <param name="descriptor">
 /// The field descriptor on which this
 /// filter operation shall be applied.
 /// </param>
 /// <param name="name">
 /// The default name of the filter operation field.
 /// </param>
 /// <param name="type">
 /// The field type of this filter operation field.
 /// </param>
 /// <param name="operation">
 /// The filter operation info.
 /// </param>
 public static StringFilterOperationDescriptor New(
     IDescriptorContext context,
     StringFilterFieldDescriptor descriptor,
     NameString name,
     ITypeReference type,
     FilterOperation operation) =>
 new StringFilterOperationDescriptor(
     context, descriptor, name, type, operation);
コード例 #4
0
 protected StringFilterOperationDescriptor(
     IDescriptorContext context,
     StringFilterFieldDescriptor descriptor,
     NameString name,
     ITypeReference type,
     FilterOperation operation,
     IFilterConvention filterConventions)
     : base(context, name, type, operation, filterConventions)
 {
     _descriptor = descriptor
                   ?? throw new ArgumentNullException(nameof(descriptor));
 }
コード例 #5
0
        public IStringFilterFieldDescriptor Filter(
            Expression <Func <T, string> > property)
        {
            if (property.ExtractMember() is PropertyInfo p)
            {
                var field = new StringFilterFieldDescriptor(Context, p);
                Fields.Add(field);
                return(field);
            }

            throw new ArgumentException(
                      FilterResources.FilterInputTypeDescriptor_OnlyProperties,
                      nameof(property));
        }
コード例 #6
0
        public IStringFilterFieldDescriptor Filter(
            Expression <Func <T, string> > property)
        {
            if (property.ExtractMember() is PropertyInfo p)
            {
                var field = new StringFilterFieldDescriptor(Context, p);
                Fields.Add(field);
                return(field);
            }

            // TODO : resources
            throw new ArgumentException(
                      "Only properties are allowed for input types.",
                      nameof(property));
        }
コード例 #7
0
 protected StringFilterOperationDescriptor(
     IDescriptorContext context,
     StringFilterFieldDescriptor descriptor,
     NameString name,
     ITypeReference type,
     FilterOperation operation)
     : base(context)
 {
     Definition.Name = name.EnsureNotEmpty(nameof(name));
     Definition.Type = type
                       ?? throw new ArgumentNullException(nameof(type));
     Definition.Operation = operation
                            ?? throw new ArgumentNullException(nameof(operation));
     _descriptor = descriptor
                   ?? throw new ArgumentNullException(nameof(descriptor));
 }
コード例 #8
0
        public void GetOrAddDescriptor_Should_AddDescriptorIfNotExists()
        {
            //arrange
            IList <FilterFieldDescriptorBase> list = new List <FilterFieldDescriptorBase>();
            var descriptor = new StringFilterFieldDescriptor(_descriptorContext, _propertyInfo);
            Func <StringFilterFieldDescriptor> valueFactory = () => descriptor;

            //act
            StringFilterFieldDescriptor result =
                list.GetOrAddDescriptor(_propertyInfo, valueFactory);

            //assert
            Assert.Single(list);
            Assert.Same(descriptor, list[0]);
            Assert.Same(descriptor, result);
        }
コード例 #9
0
        public void GetOrAddDescriptor_Should_ReplaceDescriptorIfDifferentType()
        {
            //arrange
            IList <FilterFieldDescriptorBase> list = new List <FilterFieldDescriptorBase>();
            var descriptorToRemove =
                new IgnoredFilterFieldDescriptor(_descriptorContext, _propertyInfo);
            var descriptorToAdd =
                new StringFilterFieldDescriptor(_descriptorContext, _propertyInfo);
            Func <StringFilterFieldDescriptor> valueFactory = () => descriptorToAdd;

            list.Add(descriptorToRemove);

            //act
            StringFilterFieldDescriptor result =
                list.GetOrAddDescriptor(_propertyInfo, valueFactory);

            //assert
            Assert.Single(list);
            Assert.Same(descriptorToAdd, list[0]);
            Assert.Same(descriptorToAdd, result);
            Assert.NotSame(descriptorToRemove, result);
        }
コード例 #10
0
        private bool TryCreateImplicitFilter(
            PropertyInfo property,
            out FilterFieldDefintion definition)
        {
            Type type = property.PropertyType;

            if (type.IsGenericType && Nullable.GetUnderlyingType(type) is Type nullableType)
            {
                type = nullableType;
            }

            if (type == typeof(string))
            {
                var field = new StringFilterFieldDescriptor(Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (type == typeof(bool))
            {
                var field = new BooleanFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (IsComparable(property.PropertyType))
            {
                var field = new ComparableFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (DotNetTypeInfoFactory.IsListType(type))
            {
                if (!TypeInspector.Default.TryCreate(type, out Utilities.TypeInfo typeInfo))
                {
                    throw new ArgumentException(
                              FilterResources.FilterArrayFieldDescriptor_InvalidType,
                              nameof(property));
                }

                Type elementType = typeInfo.ClrType;
                ArrayFilterFieldDescriptor field;

                if (elementType == typeof(string) ||
                    elementType == typeof(bool) ||
                    typeof(IComparable).IsAssignableFrom(elementType))
                {
                    field = new ArrayFilterFieldDescriptor(
                        Context,
                        property,
                        typeof(ISingleFilter <>).MakeGenericType(elementType));
                }
                else
                {
                    field = new ArrayFilterFieldDescriptor(Context, property, elementType);
                }

                definition = field.CreateDefinition();
                return(true);
            }

            if (type.IsClass)
            {
                var field = new ObjectFilterFieldDescriptor(
                    Context, property, property.PropertyType);
                definition = field.CreateDefinition();
                return(true);
            }

            definition = null;
            return(false);
        }
コード例 #11
0
        private bool TryCreateImplicitFilter(
            PropertyInfo property,
            [NotNullWhen(true)] out FilterFieldDefintion?definition)
        {
            Type type = property.PropertyType;

            if (type.IsGenericType &&
                System.Nullable.GetUnderlyingType(type) is { } nullableType)
            {
                type = nullableType;
            }

            if (type == typeof(string))
            {
                var field = new StringFilterFieldDescriptor(Context, property);
                field.BindFilters(Definition.Fields.BindingBehavior);
                definition = field.CreateDefinition();
                return(true);
            }

            if (type == typeof(bool))
            {
                var field = new BooleanFilterFieldDescriptor(
                    Context,
                    property);
                field.BindFilters(Definition.Fields.BindingBehavior);
                definition = field.CreateDefinition();
                return(true);
            }

            if (IsComparable(property.PropertyType))
            {
                var field = new ComparableFilterFieldDescriptor(
                    Context,
                    property);
                field.BindFilters(Definition.Fields.BindingBehavior);
                definition = field.CreateDefinition();
                return(true);
            }

            if (Context.TypeInspector.TryCreateTypeInfo(type, out ITypeInfo? typeInfo) &&
                typeInfo.GetExtendedType()?.ElementType?.Source is { } elementType)
            {
                ArrayFilterFieldDescriptor field;

                if (elementType == typeof(string) ||
                    elementType == typeof(bool) ||
                    typeof(IComparable).IsAssignableFrom(elementType))
                {
                    field = new ArrayFilterFieldDescriptor(
                        Context,
                        property,
                        typeof(ISingleFilter <>).MakeGenericType(elementType));
                }
                else
                {
                    field = new ArrayFilterFieldDescriptor(Context, property, elementType);
                }

                field.BindFilters(Definition.Fields.BindingBehavior);
                definition = field.CreateDefinition();
                return(true);
            }

            if (type.IsClass)
            {
                var field = new ObjectFilterFieldDescriptor(
                    Context,
                    property,
                    property.PropertyType);
                field.BindFilters(Definition.Fields.BindingBehavior);
                definition = field.CreateDefinition();
                return(true);
            }

            definition = null;
            return(false);
        }
コード例 #12
0
        private bool TryCreateImplicitFilter(
            PropertyInfo property,
            out FilterFieldDefintion definition)
        {
            var type = property.PropertyType;

            if (type.IsGenericType && Nullable.GetUnderlyingType(type) is Type nullableType)
            {
                type = nullableType;
            }

            if (type == typeof(string))
            {
                var field = new StringFilterFieldDescriptor(Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (type == typeof(bool))
            {
                var field = new BooleanFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (IsComparable(property.PropertyType))
            {
                var field = new ComparableFilterFieldDescriptor(
                    Context, property);
                definition = field.CreateDefinition();
                return(true);
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                ArrayFilterFieldDescriptor field;

                var genericTypeArgument = type.GetGenericArguments()[0];

                if (genericTypeArgument.IsGenericType &&
                    Nullable.GetUnderlyingType(genericTypeArgument) is Type nullableEnumerableType)
                {
                    genericTypeArgument = nullableEnumerableType;
                }
                if (genericTypeArgument == typeof(string) ||
                    genericTypeArgument == typeof(bool) ||
                    genericTypeArgument == typeof(bool?) ||
                    typeof(IComparable).IsAssignableFrom(genericTypeArgument))
                {
                    field = new ArrayFilterFieldDescriptor(
                        Context,
                        property,
                        typeof(ISingleFilter <>).MakeGenericType(genericTypeArgument)
                        );
                }
                else
                {
                    field = new ArrayFilterFieldDescriptor(Context, property, genericTypeArgument);
                }
                definition = field.CreateDefinition();
                return(true);
            }

            if (type.IsClass)
            {
                var field = new ObjectFilterFieldDescriptor(
                    Context, property, property.PropertyType);
                definition = field.CreateDefinition();
                return(true);
            }

            definition = null;
            return(false);
        }