public void Get_properties_for_type_with_cache_using_predicate()
        {
            // Arrange
            Get_properties_for_type_without_cache();

            // Act
            PropertyInfo property = PropertyCache.GetProperty <TypePoolFixture>(info => Attribute.IsDefined(info, typeof(AttributeFixture)));

            // Assert
            Assert.IsNotNull(property);
        }
Ejemplo n.º 2
0
    public void Property()
    {
        var target = new TargetForProperty
        {
            Member = "Value1"
        };

        var property = PropertyCache <TargetForProperty> .GetProperty("Member");

        var result = property.Func(target);

        Assert.Equal("Value1", result);
    }
        internal static Expression <Func <T, bool> > BuildSinglePredicate(string path, Comparison comparison, string value, StringComparison?stringComparison = null)
        {
            var property = PropertyCache <T> .GetProperty(path);

            if (property.PropertyType == typeof(string))
            {
                WhereValidator.ValidateSingleString(comparison, stringComparison);
                return(BuildStringCompare(comparison, value, property, stringComparison));
            }

            WhereValidator.ValidateSingleObject(property.PropertyType, comparison, stringComparison);
            return(BuildObjectCompare(comparison, value, property));
        }
Ejemplo n.º 4
0
        public void Get_attribute_with_cache()
        {
            // Arrange
            var type     = typeof(TypePoolFixture);
            var fixture  = new TypePoolFixture();
            var property = PropertyCache.GetProperty(type, p => p.Name == nameof(fixture.PropertyWithAttribute));
            IEnumerable <Attribute> attributes = AttributeCache.GetAttributes(type, property);

            // Act

            // Assert
            Assert.IsTrue(attributes.Count() == 3);
        }
Ejemplo n.º 5
0
    public void PropertyNested()
    {
        var target = new TargetForPropertyNested
        {
            Child = new()
            {
                Member = "Value1"
            }
        };

        var property = PropertyCache <TargetForPropertyNested> .GetProperty("Child.Member");

        var result = property.Func(target);

        Assert.Equal("Value1", result);
    }
Ejemplo n.º 6
0
        public void Get_attributes_with_property_info_using_predicate()
        {
            // Arrange
            var type     = typeof(TypePoolFixture);
            var fixture  = new TypePoolFixture();
            var property = PropertyCache.GetProperty(type, p => p.Name == nameof(fixture.PropertyWithAttribute));

            // Act
            IEnumerable <Attribute> attributes = AttributeCache.GetAttributes(
                type,
                property,
                attribute => attribute.GetType() == typeof(AttributeFixture));

            // Assert
            Assert.IsTrue(attributes.Count() == 2);
        }
        /// <summary>
        /// Process a list based item inside the property path
        /// </summary>
        static Expression ProcessList(string path, Comparison comparison, string?[]?values, StringComparison?stringComparison = null)
        {
            // Get the path pertaining to individual list items
            var listPath = Regex.Match(path, LIST_PROPERTY_PATTERN).Groups[1].Value;

            // Remove the part of the path that leads into list item properties
            path = Regex.Replace(path, LIST_PROPERTY_PATTERN, "");

            // Get the property on the current object up to the list member
            var property = PropertyCache <T> .GetProperty(path);

            // Get the list item type details
            var listItemType = property.PropertyType.GetGenericArguments().Single();

            // Generate the predicate for the list item type
            var subPredicate = typeof(ExpressionBuilder <>)
                               .MakeGenericType(listItemType)
                               .GetMethods(BindingFlags.Public | BindingFlags.Static)
                               .Single(m => m.Name.Equals("BuildPredicate") && m.GetParameters().Length == 5)
                               .Invoke(new object(), new object[] { listPath, comparison, values !, false, stringComparison ! }) as Expression;
        static IEnumerable <TItem> Order <TItem>(IEnumerable <TItem> queryable, Func <Type, string, object> getArguments)
        {
            var items    = queryable.ToList();
            var orderBys = ArgumentReader.ReadOrderBy(getArguments).ToList();
            IOrderedEnumerable <TItem> ordered;

            if (orderBys.Count > 0)
            {
                var orderBy      = orderBys.First();
                var propertyFunc = PropertyCache <TItem> .GetProperty(orderBy.Path).Func;

                if (orderBy.Descending)
                {
                    ordered = items.OrderByDescending(propertyFunc);
                }
                else
                {
                    ordered = items.OrderBy(propertyFunc);
                }
            }
            else
            {
                return(items);
            }

            foreach (var orderBy in orderBys.Skip(1))
            {
                var propertyFunc = PropertyCache <TItem> .GetProperty(orderBy.Path).Func;

                if (orderBy.Descending)
                {
                    ordered = ordered.ThenByDescending(propertyFunc);
                }
                else
                {
                    ordered = ordered.ThenBy(propertyFunc);
                }
            }

            return(ordered);
        }
Ejemplo n.º 9
0
        static IQueryable <TItem> Order <TItem>(IQueryable <TItem> queryable, Func <Type, string, object?> getArguments)
        {
            var orderBys = ArgumentReader.ReadOrderBy(getArguments).ToList();
            IOrderedQueryable <TItem> ordered;

            if (orderBys.Count > 0)
            {
                var orderBy  = orderBys.First();
                var property = PropertyCache <TItem> .GetProperty(orderBy.Path).Lambda;

                if (orderBy.Descending)
                {
                    ordered = queryable.OrderByDescending(property);
                }
                else
                {
                    ordered = queryable.OrderBy(property);
                }
            }
            else
            {
                return(queryable);
            }

            foreach (var orderBy in orderBys.Skip(1))
            {
                var property = PropertyCache <TItem> .GetProperty(orderBy.Path).Lambda;

                if (orderBy.Descending)
                {
                    ordered = ordered.ThenByDescending(property);
                }
                else
                {
                    ordered = ordered.ThenBy(property);
                }
            }

            return(ordered);
        }
Ejemplo n.º 10
0
    public static Func <TInput, bool> BuildPredicate(string path, Comparison comparison, string[] values, StringComparison?stringComparison = null)
    {
        var propertyFunc = PropertyCache <TInput> .GetProperty(path);

        if (propertyFunc.PropertyType == typeof(string))
        {
            WhereValidator.ValidateString(comparison, stringComparison);
            var stringComparisonValue = stringComparison.GetValueOrDefault(StringComparison.OrdinalIgnoreCase);
            switch (comparison)
            {
            case Comparison.In:
                return(BuildStringIn(propertyFunc, values, stringComparisonValue));

            case Comparison.NotIn:
                return(BuildStringIn(propertyFunc, values, stringComparisonValue, true));

            default:
                var value = values?.Single();
                return(target => BuildStringCompare(comparison, value, propertyFunc, target, stringComparisonValue));
            }
        }
        else
        {
            WhereValidator.ValidateObject(propertyFunc.PropertyType, comparison, stringComparison);
            switch (comparison)
            {
            case Comparison.In:
                return(BuildObjectIn(propertyFunc, values));

            case Comparison.NotIn:
                return(BuildObjectIn(propertyFunc, values, true));

            default:
                var value = values?.Single();
                return(target => BuildObjectCompare(comparison, value, propertyFunc, target));
            }
        }
    }
        internal static Expression <Func <T, bool> > BuildPredicate(string path, Comparison comparison, string[] values, StringComparison?stringComparison = null)
        {
            var property = PropertyCache <T> .GetProperty(path);

            if (property.PropertyType == typeof(string))
            {
                WhereValidator.ValidateString(comparison, stringComparison);
                switch (comparison)
                {
                case Comparison.In:
                    return(BuildStringIn(values, property, stringComparison));

                case Comparison.NotIn:
                    return(BuildStringIn(values, property, stringComparison, true));

                default:
                    var value = values?.Single();
                    return(BuildStringCompare(comparison, value, property, stringComparison));
                }
            }
            else
            {
                WhereValidator.ValidateObject(property.PropertyType, comparison, stringComparison);
                switch (comparison)
                {
                case Comparison.In:
                    return(BuildObjectIn(values, property));

                case Comparison.NotIn:
                    return(BuildObjectIn(values, property, true));

                default:
                    var value = values?.Single();
                    return(BuildObjectCompare(comparison, value, property));
                }
            }
        }
        public void Performance_test_get_properties_for_type_without_cache_from_large_cache_pool_using_predicate()
        {
            // Arrange
            // Pre-load all of the Types so we can test against a Pool containing existing objects.
            IEnumerable <Type> types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes());

            types.AsParallel().ForAll(type => PropertyCache.GetPropertiesForType(type));
            const int _iterations = 1000;
            var       times       = new List <double>();

            // Act
            for (int count = 0; count < _iterations; count++)
            {
                TypeCache.ClearTypeFromPool <TypePoolFixture>();
                var timer = new Stopwatch();
                timer.Start();
                var results = PropertyCache.GetProperty <TypePoolFixture>(
                    property => Attribute.IsDefined(property, typeof(AttributeFixture)));
                timer.Stop();
                times.Add(timer.Elapsed.TotalMilliseconds);
            }

            Debug.WriteLine($"The average time to fetch an uncached collection of filtered properties from a large pool over {_iterations} iterations was {times.Sum() / times.Count}ms");
        }