public ResultService Update(T obj) { try { this._repository.Update(obj); return(ResultService.Ok(obj)); } catch (Exception ex) { return(ResultService.Fail(ex)); } }
public ResultService Update(T obj) { try { this._repository.Update(obj); return(ResultService.Ok(obj)); } catch (Exception ex) { _log.LogError(ex, "Failed to update object"); return(ResultService.Fail(ex)); } }
public ResultService Insert(T obj) { try { this._repository.Insert(obj); return(ResultService.Ok()); } catch (Exception ex) { return(ResultService.Fail(ex)); } }
public ResultService Insert(T obj) { try { this._repository.Insert(obj); return(ResultService.Ok()); } catch (Exception ex) { _log.LogError(ex, "Failed inserting object to DB"); return(ResultService.Fail(ex)); } }
public ResultService <Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > > GetOrderBy <TEntity>(OrderByStatement <TEntity> orderByStatement) { try { if (orderByStatement == null || !orderByStatement.IsValid) { return(ResultService.Fail <Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > >(ErrorCode.EntityNotValid)); } //1. Detemine method name. string methodName = orderByStatement.Direction == OrderByDirection.Asc ? "OrderBy" : "OrderByDescending"; //2. Build a outer expression Type typeQueryable = typeof(IQueryable <TEntity>); ParameterExpression argQueryable = Expression.Parameter(typeQueryable, "p"); //p LambdaExpression outerLambdaExpression = Expression.Lambda(argQueryable, argQueryable); // p => p Expression outerExpression = outerLambdaExpression.Body; //3. Build a TEntity parameter expression Type type = typeof(TEntity); ParameterExpression arg = Expression.Parameter(type, "x"); //4. Build a Memeber expression PropertyInfo pi = type.GetProperty(orderByStatement.PropertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); MemberExpression expr = Expression.Property(arg, pi); Type memberType = pi.PropertyType; //5. Build inner expression: LambdaExpression lambda = Expression.Lambda(expr, arg); Expression innerExpression = Expression.Quote(lambda); MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TEntity), memberType }, outerExpression, innerExpression); var finalLambda = Expression.Lambda(resultExp, argQueryable); Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > result = (Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> >)finalLambda.Compile(); return(ResultService.Ok(result)); } catch (Exception ex) { return(ResultService.Fail <Func <IQueryable <TEntity>, IOrderedQueryable <TEntity> > >(ex)); } }
public ResultService <T> GetById(object id) { try { var entity = this._repository.GetById(id); if (entity == null) { return(ResultService.Fail <T>(ErrorCode.NotFound)); } return(ResultService.Ok <T>(entity)); } catch (Exception ex) { return(ResultService.Fail <T>(ex)); } }
public ResultService <T> GetById(object id) { try { var entity = this._repository.GetById(id); if (entity == null) { return(ResultService.Fail <T>(ErrorCode.NotFound)); } return(ResultService.Ok <T>(entity)); } catch (Exception ex) { _log.LogError(ex, "Failed to get object with id: {0} from DB", id); return(ResultService.Fail <T>(ex)); } }
public ResultService Delete(object id) { try { var entity = this._repository.GetById(id); if (entity == null) { return(ResultService.Fail(ErrorCode.NotFound)); } this._repository.Remove(id); return(ResultService.Ok()); } catch (Exception ex) { return(ResultService.Fail(ex)); } }
public ResultService <IEnumerable <T> > Get(IEnumerable <FilterStatement <T> > filterStatements, OrderByStatement <T> orderByStatement = null, string includeProperties = "") { try { Expression <Func <T, bool> > filters = null; if (filterStatements != null && filterStatements.Any()) { var filterService = new FiltersService <T>(filterStatements); var filtersResult = filterService.BuildExpression(); if (!filtersResult.Success) { return(ResultService.Fail <IEnumerable <T> >(filtersResult.ErrorCode)); } filters = filtersResult.Value as Expression <Func <T, bool> >; } Func <IQueryable <T>, IOrderedQueryable <T> > orderBy = null; if (orderByStatement != null) { var orderByService = new OrderByService(); var orderByResult = orderByService.GetOrderBy <T>(orderByStatement); if (!orderByResult.Success) { return(ResultService.Fail <IEnumerable <T> >(orderByResult.ErrorCode)); } orderBy = orderByResult.Value as Func <IQueryable <T>, IOrderedQueryable <T> >; } var result = this._repository.Get(filters, orderBy, includeProperties); if (result == null) { return(ResultService.Fail <IEnumerable <T> >(ErrorCode.NotFound)); } return(ResultService.Ok <IEnumerable <T> >(result)); } catch (Exception ex) { _log.LogError(ex, "Failed to get objects from DB"); return(ResultService.Fail <IEnumerable <T> >(ex)); } }
public ResultService Delete(object id) { try { var entity = this._repository.GetById(id); if (entity == null) { return(ResultService.Fail(ErrorCode.NotFound)); } this._repository.Remove(id); return(ResultService.Ok()); } catch (Exception ex) { _log.LogError(ex, "Failed to delete object with id: {0} from DB", id); return(ResultService.Fail(ex)); } }
public ResultService <Expression <Func <T, bool> > > BuildExpression() { try { ParameterExpression parameterExpression = Expression.Parameter(typeof(T), "x"); Expression finalExpression = Expression.Constant(true); foreach (var statement in _filterStatements) { if (!statement.IsValid) { return(ResultService.Fail <Expression <Func <T, bool> > >(ErrorCode.EntityNotValid)); } Type propType = typeof(T).GetProperty(statement.PropertyName).PropertyType; TypeConverter converter = TypeDescriptor.GetConverter(propType); object convertedObject = converter.ConvertFrom(statement.Value); var member = Expression.Property(parameterExpression, statement.PropertyName); var constant = Expression.Constant(convertedObject); Expression expression = null; switch (statement.Operation) { case FilterOperation.Equal: expression = Expression.Equal(member, constant); break; case FilterOperation.Contains: throw new NotImplementedException(); //break; case FilterOperation.StartsWith: throw new NotImplementedException(); //break; case FilterOperation.EndsWith: throw new NotImplementedException(); //break; case FilterOperation.NotEqual: expression = Expression.NotEqual(member, constant); break; case FilterOperation.GreaterThan: expression = Expression.GreaterThan(member, constant); break; case FilterOperation.GreaterThanOrEqual: expression = Expression.GreaterThanOrEqual(member, constant); break; case FilterOperation.LessThan: expression = Expression.LessThan(member, constant); break; case FilterOperation.LessThanOrEqual: expression = Expression.LessThanOrEqual(member, constant); break; default: throw new NotImplementedException(); //break; } ; finalExpression = Expression.AndAlso(finalExpression, expression); } Expression <Func <T, bool> > result = Expression.Lambda <Func <T, bool> >(finalExpression, parameterExpression); return(ResultService.Ok <Expression <Func <T, bool> > >(result)); } catch (Exception ex) { return(ResultService.Fail <Expression <Func <T, bool> > >(ex)); } }