コード例 #1
0
        static Repository()
        {
            DeleteMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(DmlExtensionMethods),
                nameof(DmlExtensionMethods.DeleteAsync),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(CancellationToken), 1));

            ToListMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(LinqExtensionMethods),
                nameof(LinqExtensionMethods.ToListAsync),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(CancellationToken), 1));

            FirstOrDefaultMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(LinqExtensionMethods),
                nameof(LinqExtensionMethods.FirstOrDefaultAsync),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(CancellationToken), 1));

            CountMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(LinqExtensionMethods),
                nameof(LinqExtensionMethods.CountAsync),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(CancellationToken), 1));
        }
コード例 #2
0
        public void CanTranslateOrderByClause()
        {
            var session       = new Mock <ISession>();
            var executor      = new SpecificationExecutor();
            var specification = new WithOrderBy();
            var queryable     = executor.ExecuteSpecification(session.Object, specification);

            var orderByMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.OrderBy),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            var lambda = (MethodCallExpression)queryable.Expression;

            Assert.Equal(ExpressionType.Call, lambda.NodeType);
            Assert.Same(
                orderByMethodInfo.MakeGenericMethod(typeof(User), typeof(int)),
                lambda.Method);

            Assert.Same(
                specification.Expression,
                ((UnaryExpression)lambda.Arguments.ElementAt(1)).Operand);
        }
コード例 #3
0
        public void CanTranslateThenFetchCollectionClause()
        {
            var session       = new Mock <ISession>();
            var executor      = new SpecificationExecutor();
            var specification = new WithThenFetchCollection();
            var queryable     = executor.ExecuteSpecification(session.Object, specification);

            var thenFetchManyMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(EagerFetchingExtensionMethods),
                nameof(EagerFetchingExtensionMethods.ThenFetchMany),
                null,
                new ArgumentConstraint(typeof(INhFetchRequest <,>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            var lambda = (MethodCallExpression)queryable.Expression;

            Assert.Equal(ExpressionType.Call, lambda.NodeType);
            Assert.Same(
                thenFetchManyMethodInfo.MakeGenericMethod(typeof(User), typeof(Order), typeof(Good)),
                lambda.Method);

            Assert.Same(
                specification.Expression.Body,
                ((UnaryExpression)((LambdaExpression)((UnaryExpression)lambda.Arguments.ElementAt(1)).Operand).Body).Operand);
        }
コード例 #4
0
        public void CanTranslateTakeClause()
        {
            var session       = new Mock <ISession>();
            var executor      = new SpecificationExecutor();
            var specification = new WithTake();
            var queryable     = executor.ExecuteSpecification(session.Object, specification);

            var takeMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.Take),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(int), 1));

            var lambda = (MethodCallExpression)queryable.Expression;

            Assert.Equal(ExpressionType.Call, lambda.NodeType);
            Assert.Same(
                takeMethodInfo.MakeGenericMethod(typeof(User)),
                lambda.Method);

            Assert.Equal(
                5,
                ((ConstantExpression)lambda.Arguments.ElementAt(1)).Value);
        }
コード例 #5
0
        public void CanTranslateJoinClause()
        {
            var session       = new Mock <ISession>();
            var executor      = new SpecificationExecutor();
            var specification = new WithJoin();
            var queryable     = executor.ExecuteSpecification(session.Object, specification);
            var subQuery      = executor.CreateQueryable(session.Object, (Query)specification.SubQuery);

            var joinMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.Join),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(IEnumerable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <, ,>)), 2));

            var lambda = (MethodCallExpression)queryable.Expression;

            Assert.Equal(ExpressionType.Call, lambda.NodeType);
            Assert.Same(
                joinMethodInfo.MakeGenericMethod(
                    typeof(User),
                    typeof(User),
                    typeof(int),
                    typeof(WithJoin.Result)),
                lambda.Method);

            //Assert.AreEqual(
            //	subQuery.Expression,
            //	((LambdaExpression)((UnaryExpression)lambda.Arguments.ElementAt(1)).Operand).Body);

            Assert.Same(
                specification.OuterKeySelector.Body,
                ((LambdaExpression)((UnaryExpression)lambda.Arguments.ElementAt(2)).Operand).Body);

            Assert.Same(
                specification.InnerKeySelector.Body,
                ((LambdaExpression)((UnaryExpression)lambda.Arguments.ElementAt(3)).Operand).Body);

            Assert.Same(
                specification.ResultSelector.Body,
                ((LambdaExpression)((UnaryExpression)lambda.Arguments.ElementAt(4)).Operand).Body);
        }
コード例 #6
0
        static SpecificationExecutor()
        {
            QueryMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Public | BindingFlags.Instance,
                typeof(ISession),
                nameof(ISession.Query),
                null);

            WhereMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.Where),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            SelectMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.Select),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            OrderByMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.OrderBy),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            OrderByDescendingMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.OrderByDescending),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            ThenByMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.ThenBy),
                null,
                new ArgumentConstraint(typeof(IOrderedQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            ThenByDescendingMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.ThenByDescending),
                null,
                new ArgumentConstraint(typeof(IOrderedQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            SkipMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.Skip),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(int), 1));

            SkipWhileMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.SkipWhile),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            TakeMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.Take),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(int), 1));

            TakeWhileMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.TakeWhile),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            GroupByMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.GroupBy),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            FetchMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(EagerFetchingExtensionMethods),
                nameof(EagerFetchingExtensionMethods.Fetch),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            FetchManyMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(EagerFetchingExtensionMethods),
                nameof(EagerFetchingExtensionMethods.FetchMany),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            ThenFetchMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(EagerFetchingExtensionMethods),
                nameof(EagerFetchingExtensionMethods.ThenFetch),
                null,
                new ArgumentConstraint(typeof(INhFetchRequest <,>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            ThenFetchManyMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(EagerFetchingExtensionMethods),
                nameof(EagerFetchingExtensionMethods.ThenFetchMany),
                null,
                new ArgumentConstraint(typeof(INhFetchRequest <,>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2));

            JoinMethodInfo = ReflectionHelper.FindMethod(
                BindingFlags.Static | BindingFlags.Public,
                typeof(Queryable),
                nameof(Queryable.Join),
                null,
                new ArgumentConstraint(typeof(IQueryable <>), 1),
                new ArgumentConstraint(typeof(IEnumerable <>), 1),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <,>)), 2),
                new ArgumentConstraint(typeof(Expression <>).MakeGenericType(typeof(Func <, ,>)), 2));
        }