示例#1
0
 public void LimitPage_Null()
 {
     var actual = PaginationTools.LimitPage(null);
     var expected = 1;
     Assert.AreEqual(expected, actual);
 }
示例#2
0
        /// <summary>
        /// Gets the specified operations.
        /// </summary>
        /// <param name="operations">The operations.</param>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public Results <TModel> Get(Operations <TModel> operations, int?page, int?pageSize)
        {
            var visitor = new AggregationWhereVisitor <TModel>(Mappings.Cast <Mapping>().ToList());
            var results = new Results <TModel>();

            Mappings = Mappings.OrderBy(x => x.IsPrimaryKey).ToList();

            #region WHERE
            var where = operations.Where;
            foreach (var map in Mappings)
            {
                Expression whereExpression = null;
                if (where != null)
                {
                    var paramType = map.ModelMember.Type;
                    if (TypeTools.IsList(paramType))
                    {
                        paramType = TypeTools.GetListType(paramType);
                    }
                    var param = Expression.Parameter(paramType, where.Parameters.Single().Name);
                    whereExpression = visitor.VisitLambda(where, param);
                }
                var service        = GetType().GetProperty(map.EntityMember.Member.Name).GetValue(this);
                var getQueryMethod = service.GetType().GetMethods().Single(x => x.Name == ServiceBase.GetQueryMethodName && x.GetParameters().Any());
                var getQueryMethodWithoutParameter = service.GetType().GetMethods().Single(x => x.Name == ServiceBase.GetQueryMethodName && !x.GetParameters().Any());
                if (whereExpression != null)
                {
                    var queryTemp = getQueryMethod.Invoke(service, new[] { whereExpression });
                    map.AttachedQuery = (IQueryable)queryTemp;
                }
                else
                {
                    var queryTemp = getQueryMethodWithoutParameter.Invoke(service, null);
                    map.AttachedQuery = (IQueryable)queryTemp;
                }
                map.AttachedService = service as ServiceBase;
                map.AttachedWhere   = whereExpression as LambdaExpression;
            }
            #endregion

            var query = JoinQuery();

            #region ORDER BY

            query = ApplyOrderBy(query, operations);

            #endregion

            #region PAGINATION
            results.Count = query.Count();

            page     = PaginationTools.LimitPage(page);
            pageSize = PaginationTools.LimitPageSize(pageSize);

            query = query.Pagine(page.Value, pageSize);

            #endregion

            #region SELECT

            var queryParameter    = Expression.Parameter(query.ElementType, query.ElementType.Name);
            var ctorGlobal        = Expression.New(typeof(TModel));
            var assignmentsGlobal = new List <MemberAssignment>();
            foreach (var map in Mappings)
            {
                var        propertyGlobal      = typeof(TModel).GetProperty(map.ModelMember.Member.Name);
                var        service             = GetType().GetProperty(map.EntityMember.Member.Name).GetValue(this);
                var        serviceMappings     = service.GetType().GetProperty(nameof(ServiceBase.Mappings)).GetValue(service) as List <Mapping>;
                var        propertyOfQueryType = Expression.MakeMemberAccess(queryParameter, query.ElementType.GetProperty(map.ModelMember.Member.Name));
                var        serviceModelType    = map.ModelMember.Type;
                var        serviceEntityType   = map.AttachedQuery.ElementType;
                Expression subQueryParameter   = null;
                if (TypeTools.IsList(map.ModelMember.Type))
                {
                    serviceModelType  = TypeTools.GetListType(serviceModelType);
                    subQueryParameter = Expression.Parameter(map.AttachedQuery.ElementType, map.AttachedQuery.ElementType.Name);
                }
                else
                {
                    subQueryParameter = propertyOfQueryType;
                }

                var ctor        = Expression.New(serviceModelType);
                var assignments = new List <MemberAssignment>();
                foreach (var serviceMap in serviceMappings)
                {
                    var property          = serviceModelType.GetProperty(serviceMap.ModelMember.Member.Name);
                    var propertyTarget    = serviceEntityType.GetProperty(serviceMap.EntityMember.Member.Name);
                    var propertyTargetExp = Expression.MakeMemberAccess(subQueryParameter, propertyTarget);
                    assignments.Add(Expression.Bind(property, propertyTargetExp));
                }

                var init = Expression.MemberInit(ctor, assignments.ToArray());

                if (TypeTools.IsList(map.ModelMember.Type))
                {
                    var selectMethod = typeof(System.Linq.Enumerable)
                                       .GetMethods(BindingFlags.Static | BindingFlags.Public)
                                       .Single(x => x.Name == "Select" &&
                                               x.GetParameters()[1].ParameterType.GetGenericArguments().Count() == 2)
                                       .MakeGenericMethod(map.AttachedQuery.ElementType, serviceModelType);
                    var lambdaSelect = Expression.Lambda(init, subQueryParameter as ParameterExpression);
                    var callSelect   = Expression.Call(null, selectMethod, propertyOfQueryType, lambdaSelect);
                    var binding      = Expression.Bind(propertyGlobal, callSelect);
                    assignmentsGlobal.Add(binding);
                }
                else
                {
                    assignmentsGlobal.Add(Expression.Bind(propertyGlobal, init));
                }
            }

            var initGlobal = Expression.MemberInit(ctorGlobal, assignmentsGlobal.ToArray());
            var select     = Expression.Lambda <Func <TJoinResult, TModel> >(initGlobal, queryParameter);
            results.Result = query.Select(select).ToList();
            #endregion

            return(results);
        }