Ejemplo n.º 1
0
 public Service(IRepository <T> repository)
 {
     try
     {
         // LoggerHelper.Instance.Warning(string.Format("开始创建[{0}]Service", EntityName));
         EntityName                = EntityExtensions.GetDescription(EType);
         this.repository           = repository;
         this.repository.Queryable = IncludeNavigationProperties(repository.Queryable)
                                     .Where(PreparePredicate(null));
         _UnitOfWork = repository.UnitOfWork as IQueryableUnitOfWork;
         // LoggerHelper.Instance.Warning(string.Format("成功创建[{0}]Service", EntityName));
     }
     catch (Exception ex)
     {
         ex = new BusinessException();
         HandleException(string.Format("创建[{0}]Service出错", EntityName), ex);
     }
 }
        public static string BuildSearchPagedEntitiesByQueryModelFunctionCode(Type etype)
        {
            try
            {
                List <PropertyInfo> fieldinfoList = etype.GetProperties()
                                                    .Where(
                    t =>
                    t.CanRead && t.CanWrite &&
                    CanQueryModelTypes.ContainsKey(t.PropertyType) &&
                    NeedGenerateProperty(t) &&
                    t.Name.LastIndexOf("Id") != 0
                    )
                                                    .ToList();
                StringBuilder psb = new StringBuilder();
                fieldinfoList.ForEach(propertyInfo =>
                {
                    if (!(propertyInfo.Name == "Deleted" || propertyInfo.Name == "DeleteTime"))
                    {
                        if (propertyInfo.PropertyType == typeof(int)
                            //|| propertyInfo.PropertyType == typeof(DateTime)
                            || propertyInfo.PropertyType == typeof(decimal)
                            )
                        {
                            psb.Append("                if(qModel." + propertyInfo.Name + "To==" +
                                       "qModel." + propertyInfo.Name + "From){//From==To执行==" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Equals(a => a." + propertyInfo.Name + ", qModel." +
                                       propertyInfo.Name + "From);" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);
                            psb.Append("                else if(qModel." + propertyInfo.Name + "To>qModel." +
                                       propertyInfo.Name + "From){//From<To执行Between" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Between(a => a." + propertyInfo.Name + ", qModel." +
                                       propertyInfo.Name + "From,qModel." + propertyInfo.Name + "To);" +
                                       Environment.NewLine);
                            psb.Append("                }//From>To不参与条件" + Environment.NewLine);
                        }
                        else if (propertyInfo.PropertyType == typeof(DateTime))
                        {
                            psb.Append("                if(qModel." + propertyInfo.Name + "To!=default(DateTime)&&" +
                                       "qModel." + propertyInfo.Name + "From!=default(DateTime)){" + Environment.NewLine);
                            psb.Append("                if(qModel." + propertyInfo.Name + "To==" +
                                       "qModel." + propertyInfo.Name + "From){//From==To执行==" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Equals(a => a." + propertyInfo.Name + ", qModel." +
                                       propertyInfo.Name + "From);" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);
                            psb.Append("                else if(qModel." + propertyInfo.Name + "To>qModel." +
                                       propertyInfo.Name + "From){//From<To执行Between" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Between(a => a." + propertyInfo.Name + ", qModel." +
                                       propertyInfo.Name + "From,qModel." + propertyInfo.Name + "To);" +
                                       Environment.NewLine);
                            psb.Append("                }//From>To不参与条件" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);
                        }
                        else if (propertyInfo.PropertyType == typeof(string))
                        {
                            psb.Append("                if(!string.IsNullOrEmpty(qModel." + propertyInfo.Name +
                                       ")){//非null且非empty参与like条件" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Like(a => a." + propertyInfo.Name + ", qModel." +
                                       propertyInfo.Name + ");" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);
                        }
                        else if (propertyInfo.PropertyType == typeof(bool))
                        {
                            psb.Append("                if(qModel.Query" + propertyInfo.Name + "){//enabeQuery参与查询条件" +
                                       Environment.NewLine);
                            psb.Append("                    queryBuilder.Equals(a => a." + propertyInfo.Name + ", qModel." +
                                       propertyInfo.Name + ");" + Environment.NewLine);
                            psb.Append("}" + Environment.NewLine);
                        }
                        else if (propertyInfo.PropertyType == typeof(Guid))
                        {
                            psb.Append("                if(qModel." + propertyInfo.Name + "!=default(Guid)){//非null且非empty参与like条件" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Equals(a => a." + propertyInfo.Name + ", qModel." +
                                       propertyInfo.Name + ");" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);
                        }
                    }
                });
                string pString = psb.ToString();
                pString = SearchPagedEntitiesByQueryModelFunctionCodeFormat
                          .Replace("{EntityName}", etype.Name)
                          .Replace("{CodeString}", pString)
                          .Replace("{EntityDescription}", EntityExtensions.GetDescription(etype));

                return(pString);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
        public static string BuildQueryPageFuctionString(Type etype)
        {
            try
            {
                List <PropertyInfo> fieldinfoList = etype.GetProperties()
                                                    .Where(
                    t =>
                    t.CanRead && t.CanWrite &&
                    CanQueryTypes.ContainsKey(t.PropertyType) &&
                    NeedGenerateProperty(t) &&
                    t.Name.LastIndexOf("Id") != t.Name.Length - 2
                    )
                                                    .ToList();
                StringBuilder sb  = new StringBuilder();
                StringBuilder psb = new StringBuilder();
                fieldinfoList.ForEach(propertyInfo =>
                {
                    if (!(propertyInfo.Name == "Deleted" || propertyInfo.Name == "DeleteTime"))
                    {
                        if (propertyInfo.PropertyType == typeof(int)
                            //|| propertyInfo.PropertyType == typeof (DateTime)
                            || propertyInfo.PropertyType == typeof(decimal)
                            )
                        {
                            sb.Append(CanQueryTypes[propertyInfo.PropertyType] + " " + propertyInfo.Name.ToLower() +
                                      "from," + CanQueryTypes[propertyInfo.PropertyType] + " " +
                                      propertyInfo.Name.ToLower() + "to,");

                            psb.Append("                if(" + propertyInfo.Name.ToLower() + "to==" +
                                       propertyInfo.Name.ToLower() + "from){//From==To执行==" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Equals(a => a." + propertyInfo.Name + ", " +
                                       propertyInfo.Name.ToLower() + "from);" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);
                            psb.Append("                else if(" + propertyInfo.Name.ToLower() + "to>" +
                                       propertyInfo.Name.ToLower() + "from){//From>To执行Between" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Between(a => a." + propertyInfo.Name + ", " +
                                       propertyInfo.Name.ToLower() + "from," + propertyInfo.Name.ToLower() + "to);" +
                                       Environment.NewLine);
                            psb.Append("                }//From<To不参与条件" + Environment.NewLine);
                        }
                        else if (propertyInfo.PropertyType == typeof(DateTime))
                        {
                            sb.Append(CanQueryTypes[propertyInfo.PropertyType] + " " + propertyInfo.Name.ToLower() +
                                      "from," + CanQueryTypes[propertyInfo.PropertyType] + " " +
                                      propertyInfo.Name.ToLower() + "to,");


                            psb.Append("                if(" + propertyInfo.Name.ToLower() + "to!=default(DateTime)&&" +
                                       propertyInfo.Name.ToLower() + "from!=default(DateTime)){//From==To执行==" + Environment.NewLine);

                            psb.Append("                if(" + propertyInfo.Name.ToLower() + "to==" +
                                       propertyInfo.Name.ToLower() + "from){//From==To执行==" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Equals(a => a." + propertyInfo.Name + ", " +
                                       propertyInfo.Name.ToLower() + "from);" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);
                            psb.Append("                else if(" + propertyInfo.Name.ToLower() + "to>" +
                                       propertyInfo.Name.ToLower() + "from){//From>To执行Between" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Between(a => a." + propertyInfo.Name + ", " +
                                       propertyInfo.Name.ToLower() + "from," + propertyInfo.Name.ToLower() + "to);" +
                                       Environment.NewLine);
                            psb.Append("                }}//From<To不参与条件" + Environment.NewLine);
                        }
                        else if (propertyInfo.PropertyType == typeof(string))
                        {
                            psb.Append("                if(!string.IsNullOrEmpty(" + propertyInfo.Name.ToLower() +
                                       ")){//非null且非empty参与like条件" + Environment.NewLine);
                            psb.Append("                    queryBuilder.Like(a => a." + propertyInfo.Name + ", " +
                                       propertyInfo.Name.ToLower() + ");" + Environment.NewLine);
                            psb.Append("                }" + Environment.NewLine);

                            sb.Append(CanQueryTypes[propertyInfo.PropertyType] + " " + propertyInfo.Name.ToLower() + ",");
                        }
                        else if (propertyInfo.PropertyType == typeof(bool))
                        {
                            sb.Append(CanQueryTypes[propertyInfo.PropertyType] + " " + propertyInfo.Name.ToLower() +
                                      ",bool query" + propertyInfo.Name.ToLower() + ",");

                            psb.Append("                if(query" + propertyInfo.Name.ToLower() + "){//enabeQuery参与查询条件" +
                                       Environment.NewLine);
                            psb.Append("                    queryBuilder.Equals(a => a." + propertyInfo.Name + ", " +
                                       propertyInfo.Name.ToLower() + ");" + Environment.NewLine);
                            psb.Append("}" + Environment.NewLine);
                        }
                    }
                });
                string pString = psb.ToString();
                pString = queryPageFuctionStringFormat.Replace("{EntityName}", etype.Name).Replace("{CodeString}", pString)
                          .Replace("{Parameters}", sb.ToString())
                          .Replace("{EntityDescription}", EntityExtensions.GetDescription(etype));
                return(pString);
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }