Ejemplo n.º 1
0
        public IEnumerable <TEntity> GetAll <TEntity>(string[] includePredicate = null) where TEntity : BaseEntity
        {
            try {
                if (includePredicate == null)
                {
                    //return GetQuery<TEntity>().AsEnumerable<TEntity>();
                    return(GetQueryActiveItems <TEntity>().AsEnumerable <TEntity>()); //Chnage by sanjeev for deleted items
                }
                else
                {
                    IQueryable <TEntity> result = GetQueryActiveItems <TEntity>();

                    foreach (string predicate in includePredicate)
                    {
                        result = result.Include(predicate);
                    }

                    return(result.AsEnumerable <TEntity>());
                }
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
                return(null);
            }
        }
Ejemplo n.º 2
0
        public IEnumerable <TEntity> Find <TEntity>(Expression <Func <TEntity, bool> > predicate, string[] includePredicate = null) where TEntity : BaseEntity
        {
            try {
                IQueryable <TEntity> result = GetQueryActiveItems <TEntity>().Where(predicate);

                if (includePredicate != null)
                {
                    foreach (string strpredicate in includePredicate)
                    {
                        result = result.Include(strpredicate);
                    }
                }

                return(result.AsEnumerable());
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
                return(null);
            }
        }
Ejemplo n.º 3
0
 public IEnumerable <TEntity> FindByPaging <TEntity>(
     out int totalRows,
     Expression <Func <TEntity, bool> > predicate,
     int page,
     int pageSize,
     string sortColumn,
     string sortType,
     string[] includePredicate         = null,
     IList <IFilterDescriptor> filters = null) where TEntity : BaseEntity
 {
     totalRows = 0;
     try {
         IQueryable <TEntity> query = GetQueryActiveItems <TEntity>();
         return(FindByPaging <TEntity>(query, out totalRows, predicate, page, pageSize, sortColumn, sortType, includePredicate, filters));
     }
     catch (Exception ex) {
         bool rethrow;
         rethrow = DataExceptionHandler.HandleException(ref ex);
         if (rethrow)
         {
             throw ex;
         }
         return(null);
     }
 }
Ejemplo n.º 4
0
        public IQueryable Sort <T>(IQueryable <T> source, string sortBy, string sortOrder)
        {
            //string[] sortParts = sortExpression.Split(' ');
            var param = Expression.Parameter(typeof(T), string.Empty);

            try {
                if (source.AsEnumerable().Count() <= 0 || (string.IsNullOrEmpty(sortBy)))
                {
                    return(from data in source
                           orderby sortBy ascending
                           select data);
                }
                var property   = Expression.Property(param, sortBy);
                var sortLambda = Expression.Lambda <Func <T, object> >(Expression.Convert(property, typeof(object)), param);

                if (sortOrder.Length > 1 && sortOrder.Equals("desc", StringComparison.OrdinalIgnoreCase))
                {
                    return(source.AsQueryable <T>().OrderByDescending <T, object>(sortLambda));
                }
                return(source.AsQueryable <T>().OrderBy <T, object>(sortLambda));
            }
            catch (ArgumentException) {
                return(source);
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
                return(null);
            }
        }
Ejemplo n.º 5
0
        private IQueryable <TEntity> FilterExpression <TEntity>(IQueryable <TEntity> query, IFilterDescriptor filter)
            where TEntity : class
        {
            try {
                Expression <Func <TEntity, bool> > filterExpression = null;

                if (filter is CompositeFilterDescriptor)
                {
                    foreach (IFilterDescriptor childFilter in ((CompositeFilterDescriptor)filter).FilterDescriptors)
                    {
                        query = FilterExpression <TEntity>(query, childFilter);
                    }
                }
                else
                {
                    FilterDescriptor    filterDescriptor = (FilterDescriptor)filter;
                    ParameterExpression paramExp         = Expression.Parameter(typeof(TEntity));

                    Expression exp = filterDescriptor.CreateFilterExpression(paramExp);

                    //MemberExpression fieldExp = Expression.PropertyOrField(paramExp, filterDescriptor.Member);
                    //ConstantExpression valueExp = Expression.Constant(filterDescriptor.Value, filterDescriptor.Value.GetType());

                    //Expression exp = null;

                    //if (fieldExp.Type == typeof(string))
                    //{
                    //    MethodInfo method = typeof(string).GetMethod(filterDescriptor.Operator.ToString(), new[] { typeof(string) });
                    //    exp = Expression.Call(fieldExp, method, valueExp);
                    //}
                    //else if (filterDescriptor.MemberType == typeof(int)
                    //    || filterDescriptor.MemberType == typeof(long))
                    //{
                    //    exp = Expression.LessThanOrEqual(fieldExp, valueExp);
                    //}
                    //else
                    //{
                    //    exp = Expression.Equal(fieldExp, valueExp);
                    //}

                    filterExpression = Expression.Lambda <Func <TEntity, bool> >(exp, paramExp);
                    query            = query.Where(filterExpression);
                }
                return(query);
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
                return(null);
            }
        }
Ejemplo n.º 6
0
        public void Modify <TEntity>(Expression <Func <TEntity, bool> > predicate, TEntity entity, string[] includePredicate = null, bool active_Ind = true) where TEntity : BaseEntity
        {
            try {
                entity.Active_Ind = active_Ind;

                TEntity originalEntity;
                if (includePredicate != null)//Added By Dharmendra to fill associated properties.
                {
                    IQueryable <TEntity> result = this.GetQuery <TEntity>();
                    foreach (var item in includePredicate)
                    {
                        result = result.Include(item);
                    }

                    originalEntity = result.SingleOrDefault(predicate);
                }
                else
                {
                    originalEntity = this.GetQuery <TEntity>().SingleOrDefault(predicate);
                }

                if (!String.IsNullOrEmpty(originalEntity.Created_By))
                {
                    entity.Created_By = originalEntity.Created_By;
                }
                if (originalEntity.Created_Date.HasValue)
                {
                    entity.Created_Date = originalEntity.Created_Date;
                }

                DbEntityEntry entry = _context.Entry <TEntity>(originalEntity);
                //ValidateDuplicatedKeys<TEntity>(entity);
                entry.CurrentValues.SetValues(entity);
                if (includePredicate != null)//Added By Dharmendra to fill associated properties.
                {
                    Type t = typeof(TEntity);
                    foreach (var item in includePredicate)
                    {
                        System.Reflection.PropertyInfo pInfo = t.GetProperty(item);
                        entry.Reference(item).CurrentValue = pInfo.GetValue(entity, null);
                    }
                }
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex, entity.Updated_By, entity.GetType().Name, entity.ID.ToString());
                if (rethrow)
                {
                    throw ex;
                }
            }
            //entry.Reference("Party").CurrentValue = (entity as SalesOrder).Party;
        }
Ejemplo n.º 7
0
        public DataTable GetAllByPagingAsDt(
            string tableName,
            out int totalRows,
            int page,
            int pageSize,
            string sortColumn,
            string sortType,
            IList <IFilterDescriptor> filters = null)
        {
            totalRows = 0;
            try {
                string sql = "Select * from" +
                             "(" +
                             " SELECT ROW_NUMBER() OVER (ORDER BY [t0]." + (string.IsNullOrEmpty(sortColumn) ? "ID" : sortColumn) + " ) AS [ROW_NUMBER]" +
                             " , * from " + tableName + " As [t0]" +
                             " ) AS [t1]" +
                             " WHERE [t1].[ROW_NUMBER] BETWEEN " + ((page - 1) * pageSize + 1) + " AND " + (page * pageSize);


                // Apply sorting
                if (!string.IsNullOrEmpty(sortColumn))
                {
                    if (sortType.Equals("DESC", StringComparison.OrdinalIgnoreCase))
                    {
                        sql += " order by " + sortColumn + " desc";
                    }
                    else
                    {
                        sql += "order by " + sortColumn + " asc";
                    }
                }

                //if (filters != null)
                //  result = FilterExpression(result, filters);


                // Apply pagging
                totalRows = (int)FetchData("Select Count(*) as Count from " + tableName).Rows[0]["Count"];

                return(FetchData(sql));
            }

            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
                return(null);
            }
        }
Ejemplo n.º 8
0
 private string GetEntityName <TEntity>() where TEntity : class
 {
     try {
         return(string.Format("ObjectContext.{0}", _pluralizer.Pluralize(typeof(TEntity).Name)));
     }
     catch (Exception ex) {
         bool rethrow;
         rethrow = DataExceptionHandler.HandleException(ref ex);
         if (rethrow)
         {
             throw ex;
         }
         return(null);
     }
 }
Ejemplo n.º 9
0
 public TEntity First <TEntity>(Expression <Func <TEntity, bool> > predicate) where TEntity : BaseEntity
 {
     try {
         return(GetQueryActiveItems <TEntity>().Where(predicate).FirstOrDefault());
     }
     catch (Exception ex) {
         bool rethrow;
         rethrow = DataExceptionHandler.HandleException(ref ex);
         if (rethrow)
         {
             throw ex;
         }
         return(null);
     }
 }
Ejemplo n.º 10
0
 public TEntity Add <TEntity>(TEntity entity) where TEntity : BaseEntity
 {
     try {
         entity.Created_Date = DateTime.Now;
         entity.Active_Ind   = true;
         //ValidateDuplicatedKeys<TEntity>(entity);
         TEntity newEntity = _context.Set <TEntity>().Add(entity);
         return(newEntity);
     }
     catch (Exception ex) {
         bool rethrow;
         rethrow = DataExceptionHandler.HandleException(ref ex, entity.Created_By, entity.GetType().Name, entity.ID.ToString());
         if (rethrow)
         {
             throw ex;
         }
         return(null);
     }
 }
Ejemplo n.º 11
0
        public void Delete <TEntity>(Expression <Func <TEntity, bool> > predicate, bool hardDelete = false) where TEntity : BaseEntity
        {
            try {
                IEnumerable <TEntity> originalEntities = this.GetQueryActiveItems <TEntity>().Where(predicate).AsEnumerable();
                if (originalEntities == null)
                {
                    return;
                }

                //_context.Set<TEntity>().Remove(originalEntity);

                if (hardDelete)
                {
                    foreach (TEntity originalEntity in originalEntities)
                    {
                        _context.Set <TEntity>().Remove(originalEntity);
                    }
                }
                else
                {
                    foreach (TEntity originalEntity in originalEntities)
                    {
                        originalEntity.Active_Ind        = false;
                        originalEntity.Last_Updated_Date = DateTime.Now;
                        originalEntity.Updated_By        = HttpContext.Current.User.Identity.Name;
                        DbEntityEntry entry = _context.Entry <TEntity>(originalEntity);
                        entry.CurrentValues.SetValues(originalEntity);
                    }
                }
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
            }
        }
Ejemplo n.º 12
0
        public IEnumerable <TEntity> FindByPaging <TEntity>(
            IQueryable <TEntity> query,
            out int totalRows,
            Expression <Func <TEntity, bool> > predicate,
            int page,
            int pageSize,
            string sortColumn,
            string sortType,
            string[] includePredicate         = null,
            IList <IFilterDescriptor> filters = null) where TEntity : BaseEntity
        {
            totalRows = 0;
            try {
                if (filters != null)
                {
                    query = FilterExpression(query, filters);
                }

                if (predicate != null)
                {
                    query = query.Where(predicate);
                }

                totalRows = query.Count();

                if (!string.IsNullOrEmpty(sortColumn))
                {
                    if (sortType.Equals("DESC", StringComparison.OrdinalIgnoreCase))
                    {
                        query = query.OrderByDescending <TEntity>(sortColumn);
                    }
                    else
                    {
                        query = query.OrderBy <TEntity>(sortColumn);
                    }
                }
                else
                {
                    query = (from data in query
                             orderby sortColumn ascending
                             select data);
                }

                if (includePredicate == null)
                {
                    return((from data in query
                            //orderby sortColumn
                            select data).Skip((page <= 0 ? 0 : page - 1) * pageSize).Take(pageSize).AsEnumerable <TEntity>());
                }
                else
                {
                    IQueryable <TEntity> result = (from data in query
                                                   //orderby sortColumn
                                                   select data).Skip((page <= 0 ? 0 : page - 1) * pageSize).Take(pageSize);

                    foreach (string strpredicate in includePredicate)
                    {
                        result = result.Include(strpredicate);
                    }

                    return(result.AsEnumerable <TEntity>());
                }
            }
            catch (Exception ex) {
                bool rethrow;
                rethrow = DataExceptionHandler.HandleException(ref ex);
                if (rethrow)
                {
                    throw ex;
                }
                return(null);
            }
        }