internal static async Task <TEntity[]> UpdateEntities <TEntity>(this IBulkRequest request, ICrudRequestConfig config, IEnumerable <Tuple <object, TEntity> > items, CancellationToken token) where TEntity : class { var updator = config.GetUpdatorFor <TEntity>(); try { var entities = await Task.WhenAll(items.Select(x => updator(request, x.Item1, x.Item2, token))).Configure(); token.ThrowIfCancellationRequested(); return(entities); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CrudUpdateEntityFailedException(GenericUpdateEntityError, e) { ItemProperty = items.Select(x => x.Item1).ToArray(), EntityProperty = items.Select(x => x.Item2).ToArray() }; } }
internal static async Task <T> RunResultHooks <T>(this ICrudRequest request, ICrudRequestConfig config, T result, CancellationToken ct) { var hooks = config.GetResultHooks(); foreach (var hook in hooks) { try { if (typeof(T).IsAssignableFrom(hook.ResultType)) { result = (T)await hook.Run(request, result, ct).Configure(); } else { result = await ResultHookAdapter.Adapt(hook, request, result, ct).Configure(); } ct.ThrowIfCancellationRequested(); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CrudHookFailedException(GenericHookError("result"), e) { HookProperty = hook }; } } return(result); }
internal static async Task <TEntity> UpdateEntity <TEntity>(this ICrudRequest request, ICrudRequestConfig config, object item, TEntity entity, CancellationToken token) where TEntity : class { var updator = config.GetUpdatorFor <TEntity>(); try { entity = await updator(request, item, entity, token).Configure(); token.ThrowIfCancellationRequested(); return(entity); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CrudUpdateEntityFailedException(GenericUpdateEntityError, e) { ItemProperty = item, EntityProperty = entity }; } }
internal static async Task RunEntityHooks <TEntity>(this ICrudRequest request, ICrudRequestConfig config, object entity, CancellationToken ct) where TEntity : class { var hooks = config.GetEntityHooksFor <TEntity>(); foreach (var hook in hooks) { try { await hook.Run(request, entity, ct).Configure(); ct.ThrowIfCancellationRequested(); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CrudHookFailedException(GenericHookError("entity"), e) { HookProperty = hook }; } } }
internal static async Task <object[]> RunItemHooks <TEntity>(this IBulkRequest request, ICrudRequestConfig config, object[] items, CancellationToken ct) where TEntity : class { var hooks = config.GetItemHooksFor <TEntity>(); foreach (var hook in hooks) { for (var i = 0; i < items.Length; ++i) { try { items[i] = await hook.Run(request, items[i], ct).Configure(); ct.ThrowIfCancellationRequested(); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CrudHookFailedException(GenericHookError("item"), e) { HookProperty = hook }; } } } return(items); }
internal static IQueryable <TEntity> SortWith <TEntity>(this IQueryable <TEntity> entities, object request, ICrudRequestConfig config) where TEntity : class { var sorter = config.GetSorterFor <TEntity>(); return(sorter?.Sort(request, entities).Cast <TEntity>() ?? entities); }
internal static IQueryable <TEntity> SelectWith <TEntity>(this IQueryable <TEntity> entities, object request, ICrudRequestConfig config) where TEntity : class { var selector = config.GetSelectorFor <TEntity>().Get <TEntity>(); return(entities.Where(selector(request))); }
public override BulkOperation <TOperationEntity> Apply <TOperationEntity>( ICrudRequestConfig config, BulkOperation <TOperationEntity> operation) { operation = base.Apply(config, operation); operation.CaseSensitive = _keyCaseSensitivity; return(operation); }
protected CrudRequestHandler(IEntityContext context, CrudConfigManager profileManager) { Context = context; RequestConfig = profileManager.GetRequestConfigFor <TRequest>(); DataContext = new DataContext <TEntity>(RequestConfig); var errorHandler = RequestConfig.ErrorConfig.GetErrorHandlerFor <TEntity>(); ErrorDispatcher = new CrudErrorDispatcher(errorHandler); }
internal static IQueryable <TEntity> FilterWith <TEntity>(this IQueryable <TEntity> entities, object request, ICrudRequestConfig config) where TEntity : class { var filters = config.GetFiltersFor <TEntity>(); return(filters.Aggregate(entities, (current, filter) => filter.Filter(request, current).Cast <TEntity>())); }
protected Expression <Func <TEntity, object> > GetDefaultKeyMapping(ICrudRequestConfig config) { var entityKey = config.GetKeyFor <TEntity>(); if (entityKey != null && entityKey.KeyExpression.Body is MemberExpression memberExpression) { return(MakeColumnExpression <TEntity>(memberExpression.Member)); } return(null); }
public override BulkOperation <TOperationEntity> Apply <TOperationEntity>( ICrudRequestConfig config, BulkOperation <TOperationEntity> operation) { operation = base.Apply(config, operation); if (IgnoredColumns.Count > 0) { if (operation.IgnoreOnInsertExpression != null) { foreach (var member in ((NewExpression)operation.IgnoreOnInsertExpression.Body).Members) { IgnoredColumns.Add(member); } } operation.IgnoreOnInsertExpression = CreateNewExpression <TOperationEntity>(IgnoredColumns); } return(operation); }
internal static async Task <TResult[]> CreateResults <TEntity, TResult>(this TEntity[] entities, ICrudRequestConfig config, CancellationToken token) where TEntity : class { var createResult = config.GetResultCreatorFor <TEntity, TResult>(); try { var results = await Task.WhenAll(entities.Select(x => createResult(x, token))).Configure(); token.ThrowIfCancellationRequested(); return(results); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CrudCreateResultFailedException(GenericCreateResultError, e) { EntityProperty = entities }; } }
internal static async Task RunRequestHooks(this ICrudRequest request, ICrudRequestConfig config, CancellationToken ct) { var hooks = config.GetRequestHooks(); foreach (var hook in hooks) { try { await hook.Run(request, ct).Configure(); ct.ThrowIfCancellationRequested(); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CrudHookFailedException(GenericHookError("request"), e) { HookProperty = hook }; } } }
public override BulkOperation <TOperationEntity> Apply <TOperationEntity>( ICrudRequestConfig config, BulkOperation <TOperationEntity> operation) { operation = base.Apply(config, operation); operation.AllowDuplicateKeys = _allowDuplicateKeys; operation.CaseSensitive = _keyCaseSensitivity; if (IgnoredColumns.Count > 0) { if (operation.IgnoreOnUpdateExpression != null) { foreach (var member in ((NewExpression)operation.IgnoreOnUpdateExpression.Body).Members) { IgnoredColumns.Add(member); } } operation.IgnoreOnUpdateExpression = CreateNewExpression <TOperationEntity>(IgnoredColumns); } return(operation); }
public UniversalRequestDecorator(CrudConfigManager profileManager, Func <IRequestHandler <TRequest> > decorateeFactory) { _decorateeFactory = decorateeFactory; _requestConfig = profileManager.GetRequestConfigFor <TRequest>(); }
public virtual BulkOperation <TOperationEntity> Apply <TOperationEntity>( ICrudRequestConfig config, BulkOperation <TOperationEntity> operation) where TOperationEntity : class { var keyColumn = operation.ColumnMappings.Find(x => x.IsPrimaryKey); if (KeyColumnMapping != null) { if (keyColumn != null) { operation.ColumnMappings.Remove(keyColumn); } } else { KeyColumnMapping = keyColumn != null ? UpcastExpression(keyColumn.SourceExpression) : GetDefaultKeyMapping(config); } if (keyColumn == null) { if (KeyColumnMapping == null) { var message = $"No key column has been declared for bulk request '{config.RequestType}' " + $"and entity '{typeof(TOperationEntity)}'."; throw new BadCrudConfigurationException(message); } operation.ColumnMappings.Add(DowncastExpression <TOperationEntity>(KeyColumnMapping), true); } if (ColumnMappings.Count == 0) { AddDefaultColumnMappings(operation.ColumnMappings); } foreach (var mapping in ColumnMappings) { operation.ColumnMappings.Add(DowncastExpression <TOperationEntity>(mapping), false); } if (OutputExpression == null) { OutputExpression = KeyColumnMapping; if (operation.ColumnOutputExpression == null) { operation.ColumnOutputExpression = DowncastExpression <TOperationEntity>(OutputExpression); } } if (BatchSize.HasValue) { operation.BatchSize = BatchSize.Value; } if (BatchTimeout.HasValue) { operation.BatchTimeout = BatchTimeout.Value; } return(operation); }
public DataContext(EntitySet <TEntity> entitySet, ICrudRequestConfig config) { EntitySet = entitySet; Configuration = config; }
public DataContext(ICrudRequestConfig config) : this(null, config) { }