internal static async Task RunEntityHooks <TEntity>(this ICrudlessRequest request, IRequestConfig config, IServiceProvider provider, object entity, CancellationToken ct) where TEntity : class { var hooks = config.GetEntityHooksFor <TEntity>(provider); foreach (var hook in hooks) { try { await hook.Run(request, entity, ct).Configure(); ct.ThrowIfCancellationRequested(); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new HookFailedException(GenericHookError("entity"), e) { HookProperty = hook }; } } }
internal static async Task <TEntity[]> UpdateEntities <TRequest, TEntity>(this TRequest request, IRequestConfig config, IServiceProvider provider, IEnumerable <Tuple <object, TEntity> > items, CancellationToken token) where TRequest : IBulkRequest where TEntity : class { var updator = config.GetUpdatorFor <TEntity>(); var context = new RequestContext <TRequest> { Request = request, ServiceProvider = provider }.Box(); try { var entities = await Task.WhenAll(items.Select(x => updator(context, x.Item1, x.Item2, token))).Configure(); token.ThrowIfCancellationRequested(); return(entities); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new UpdateEntityFailedException(GenericUpdateEntityError, e) { ItemProperty = items.Select(x => x.Item1).ToArray(), EntityProperty = items.Select(x => x.Item2).ToArray() }; } }
internal static async Task <object[]> RunItemHooks <TEntity>(this IBulkRequest request, IRequestConfig config, IServiceProvider provider, object[] items, CancellationToken ct) where TEntity : class { var hooks = config.GetItemHooksFor <TEntity>(provider); 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 HookFailedException(GenericHookError("item"), e) { HookProperty = hook }; } } } return(items); }
internal static async Task <TEntity> UpdateEntity <TRequest, TEntity>(this TRequest request, IRequestConfig config, IServiceProvider provider, object item, TEntity entity, CancellationToken token) where TEntity : class { var updator = config.GetUpdatorFor <TEntity>(); var context = new RequestContext <TRequest> { Request = request, ServiceProvider = provider }.Box(); try { entity = await updator(context, item, entity, token).Configure(); token.ThrowIfCancellationRequested(); return(entity); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new UpdateEntityFailedException(GenericUpdateEntityError, e) { ItemProperty = item, EntityProperty = entity }; } }
internal static async Task <T> RunResultHooks <T>(this ICrudlessRequest request, IRequestConfig config, IServiceProvider provider, T result, CancellationToken ct) { var hooks = config.GetResultHooks(provider); 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 HookFailedException(GenericHookError("result"), e) { HookProperty = hook }; } } return(result); }
public UniversalRequestDecorator(CrudlessConfigManager profileManager, ServiceProviderContainer container, Func <IRequestHandler <TRequest> > decorateeFactory) { _container = container; _decorateeFactory = decorateeFactory; _requestConfig = profileManager.GetRequestConfigFor <TRequest>(); }
internal static IQueryable <TEntity> SelectWith <TEntity>(this IQueryable <TEntity> entities, object request, IRequestConfig config) where TEntity : class { var selector = config.GetSelectorFor <TEntity>().Get <TEntity>(); return(entities.Where(selector(request))); }
internal static IQueryable <TEntity> SortWith <TEntity>(this IQueryable <TEntity> entities, object request, IRequestConfig config, IServiceProvider provider) where TEntity : class { var sorter = config.GetSorterFor <TEntity>(provider); return(sorter?.Sort(request, entities).Cast <TEntity>() ?? entities); }
public override BulkOperation <TOperationEntity> Apply <TOperationEntity>( IRequestConfig config, BulkOperation <TOperationEntity> operation) { operation = base.Apply(config, operation); operation.CaseSensitive = _keyCaseSensitivity; return(operation); }
internal static IQueryable <TEntity> FilterWith <TEntity>(this IQueryable <TEntity> entities, object request, IRequestConfig config, IServiceProvider provider) where TEntity : class { var filters = config.GetFiltersFor <TEntity>(provider); return(filters.Aggregate(entities, (current, filter) => filter.Filter(request, current).Cast <TEntity>())); }
protected Expression <Func <TEntity, object> > GetDefaultKeyMapping(IRequestConfig config) { // TODO: Composite Keys var entityKey = config.GetKeysFor <TEntity>()[0]; if (entityKey != null && entityKey.KeyExpression.Body is MemberExpression memberExpression) { return(MakeColumnExpression <TEntity>(memberExpression.Member)); } return(null); }
public override BulkOperation <TOperationEntity> Apply <TOperationEntity>( IRequestConfig 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); }
public override BulkOperation <TOperationEntity> Apply <TOperationEntity>( IRequestConfig 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); }
internal static async Task RunRequestHooks(this ICrudlessRequest request, IRequestConfig config, IServiceProvider provider, CancellationToken ct) { var hooks = config.GetRequestHooks(provider); foreach (var hook in hooks) { try { await hook.Run(request, ct).Configure(); ct.ThrowIfCancellationRequested(); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new HookFailedException(GenericHookError("request"), e) { HookProperty = hook }; } } }
internal static async Task <TResult[]> CreateResults <TRequest, TEntity, TResult>(this TEntity[] entities, TRequest request, IRequestConfig config, IServiceProvider provider, CancellationToken token) where TRequest : ICrudlessRequest where TEntity : class { var createResult = config.GetResultCreatorFor <TEntity, TResult>(); var context = new RequestContext <TRequest> { Request = request, ServiceProvider = provider }.Box(); try { var results = await Task.WhenAll(entities.Select(x => createResult(context, x, token))).Configure(); token.ThrowIfCancellationRequested(); return(results); } catch (Exception e) when(IsNonCancellationFailure(e)) { throw new CreateResultFailedException(GenericCreateResultError, e) { EntityProperty = entities }; } }
public DataContext(EntitySet <TEntity> entitySet, IRequestConfig config) { EntitySet = entitySet; Configuration = config; }
public DataContext(IRequestConfig config) : this(null, config) { }
public virtual BulkOperation <TOperationEntity> Apply <TOperationEntity>( IRequestConfig 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 BadConfigurationException(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); }
internal static async Task RunAuditHooks <TEntity>(this ICrudlessRequest request, IRequestConfig config, IServiceProvider provider, IEnumerable <(TEntity, TEntity)> entities,
public ApiStringProvider(IRequestConfig requestConfig) { RequestConfig = requestConfig; }