public async Task Invoke(CRUDContext <TService> ctx) { IAuthorizer <TService> authorizer = ctx.ServiceContainer.GetAuthorizer(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); foreach (DbSet dbSet in ctx.Request.dbSets) { //methods on domain service which are attempted to be executed by client (SaveChanges triggers their execution) Dictionary <string, MethodInfoData> domainServiceMethods = new Dictionary <string, MethodInfoData>(); DbSetInfo dbInfo = metadata.DbSets[dbSet.dbSetName]; dbSet.rows.Aggregate <RowInfo, Dictionary <string, MethodInfoData> >(domainServiceMethods, (dict, rowInfo) => { MethodInfoData method = rowInfo.GetCRUDMethodInfo(metadata, dbInfo.dbSetName); if (method == null) { throw new DomainServiceException(string.Format(ErrorStrings.ERR_REC_CHANGETYPE_INVALID, dbInfo.GetEntityType().Name, rowInfo.changeType)); } string dicKey = string.Format("{0}:{1}", method.OwnerType.FullName, method.MethodInfo.Name); if (!dict.ContainsKey(dicKey)) { dict.Add(dicKey, method); } return(dict); }); await authorizer.CheckUserRightsToExecute(domainServiceMethods.Values); } await _next(ctx); }
private async Task Delete(CRUDContext <TService> ctx, RunTimeMetadata metadata, ChangeSetRequest changeSet, RowInfo rowInfo) { TService service = ctx.Service; IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); CheckRowInfo(rowInfo); using (RequestCallContext callContext = new RequestCallContext(CRUDContext <TService> .CreateRequestContext(service, changeSet, rowInfo))) { rowInfo.SetChangeState(new EntityChangeState()); await serviceHelper.DeleteEntity(metadata, rowInfo);; } }
private async Task Insert(CRUDContext <TService> ctx, RunTimeMetadata metadata, ChangeSetRequest changeSet, IChangeSetGraph graph, RowInfo rowInfo) { TService service = ctx.Service; IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); CheckRowInfo(rowInfo); using (RequestCallContext callContext = new RequestCallContext(CRUDContext <TService> .CreateRequestContext(service, changeSet, rowInfo))) { rowInfo.SetChangeState(new EntityChangeState { ParentRows = graph.GetParents(rowInfo) }); await serviceHelper.InsertEntity(metadata, rowInfo); } }
public async Task Invoke(CRUDContext <TService> ctx) { IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); RunTimeMetadata metadata = ctx.Service.GetMetadata(); ChangeSetRequest changeSet = ctx.Request; ChangeSetGraph graph = new ChangeSetGraph(ctx.Request, metadata); graph.Prepare(); ctx.Properties.Add(CRUDContext <TService> .CHANGE_GRAPH_KEY, graph); RowInfo currentRowInfo = null; try { foreach (RowInfo rowInfo in graph.InsertList) { currentRowInfo = rowInfo; await Insert(ctx, metadata, changeSet, graph, rowInfo); } foreach (RowInfo rowInfo in graph.UpdateList) { currentRowInfo = rowInfo; await Update(ctx, metadata, changeSet, rowInfo); } foreach (RowInfo rowInfo in graph.DeleteList) { currentRowInfo = rowInfo; await Delete(ctx, metadata, changeSet, rowInfo); } } catch (Exception ex) { if (currentRowInfo != null) { object dbEntity = currentRowInfo.GetChangeState()?.Entity; currentRowInfo.SetChangeState(new EntityChangeState { Entity = dbEntity, Error = ex }); } throw; } await _next(ctx); }
public async Task Invoke(CRUDContext <TService> ctx) { RunTimeMetadata metadata = ctx.Service.GetMetadata(); ChangeSetRequest changeSet = ctx.Request; IChangeSetGraph graph = ctx.Properties.Get <IChangeSetGraph>(CRUDContext <TService> .CHANGE_GRAPH_KEY) ?? throw new InvalidOperationException("Could not get Graph changes from properties"); if (!await ValidateRows(ctx, changeSet, metadata, graph.InsertList)) { throw new ValidationException(ErrorStrings.ERR_SVC_CHANGES_ARENOT_VALID); } if (!await ValidateRows(ctx, changeSet, metadata, graph.UpdateList)) { throw new ValidationException(ErrorStrings.ERR_SVC_CHANGES_ARENOT_VALID); } await _next(ctx); }
private async Task <bool> ValidateRows(CRUDContext <TService> ctx, ChangeSetRequest changeSet, RunTimeMetadata metadata, IEnumerable <RowInfo> rows) { TService service = ctx.Service; IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); foreach (RowInfo rowInfo in rows) { RequestContext req = CRUDContext <TService> .CreateRequestContext(service, changeSet, rowInfo); using (RequestCallContext callContext = new RequestCallContext(req)) { if (!await serviceHelper.ValidateEntity(metadata, req)) { rowInfo.invalid = rowInfo.GetChangeState().ValidationErrors; return(false); } } } return(true); }
public async Task Invoke(CRUDContext <TService> ctx) { IServiceOperationsHelper <TService> serviceHelper = ctx.ServiceContainer.GetServiceHelper(); IDataHelper <TService> dataHelper = ctx.ServiceContainer.GetDataHelper(); Metadata.RunTimeMetadata metadata = ctx.Service.GetMetadata(); ChangeSetRequest changeSet = ctx.Request; IChangeSetGraph graph = ctx.Properties.Get <IChangeSetGraph>(CRUDContext <TService> .CHANGE_GRAPH_KEY) ?? throw new InvalidOperationException("Could not get Graph changes from properties"); CRUDServiceMethods serviceMethods = ctx.Properties.Get <CRUDServiceMethods>(CRUDContext <TService> .CHANGE_METHODS_KEY) ?? throw new InvalidOperationException("Could not get CRUD Service methods from properties"); RequestContext req = CRUDContext <TService> .CreateRequestContext(ctx.Service, changeSet); using (RequestCallContext callContext = new RequestCallContext(req)) { await serviceMethods.ExecuteChangeSet(); await serviceHelper.AfterExecuteChangeSet(changeSet); await serviceMethods.AfterChangeSetExecuted(); foreach (RowInfo rowInfo in graph.AllList) { if (rowInfo.changeType != ChangeType.Deleted) { serviceHelper.UpdateRowInfoAfterUpdates(rowInfo); } } SubResultList subResults = new SubResultList(); await serviceHelper.AfterChangeSetCommited(changeSet, subResults); await serviceMethods.AfterChangeSetCommited(subResults); SubsetsGenerator subsetsGenerator = new SubsetsGenerator(metadata, dataHelper); ctx.Response.subsets = subsetsGenerator.CreateSubsets(subResults); } await _next(ctx); }