Example #1
0
        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);
        }
Example #2
0
        protected async Task <bool> ApplyChangeSet(ChangeSet changeSet)
        {
            AuthorizeChangeSet(changeSet);
            var metadata = MetadataHelper.GetInitializedMetadata(this);
            var graph    = new ChangeSetGraph(changeSet, metadata);

            graph.Prepare();

            foreach (var rowInfo in graph.insertList)
            {
                var dbSet = changeSet.dbSets.Where(d => d.dbSetName == rowInfo.dbSetInfo.dbSetName).Single();
                var req   = new RequestContext(this, changeSet: changeSet, dbSet: dbSet, rowInfo: rowInfo,
                                               operation: ServiceOperationType.SaveChanges);
                using (var callContext = new RequestCallContext(req))
                {
                    rowInfo.changeState = new EntityChangeState {
                        ParentRows = graph.GetParents(rowInfo)
                    };
                    ApplyChangesToEntity(rowInfo);
                }
            }

            foreach (var rowInfo in graph.updateList)
            {
                var dbSet = changeSet.dbSets.Where(d => d.dbSetName == rowInfo.dbSetInfo.dbSetName).Single();
                var req   = new RequestContext(this, changeSet: changeSet, dbSet: dbSet, rowInfo: rowInfo,
                                               operation: ServiceOperationType.SaveChanges);
                using (var callContext = new RequestCallContext(req))
                {
                    rowInfo.changeState = new EntityChangeState();
                    ApplyChangesToEntity(rowInfo);
                }
            }

            foreach (var rowInfo in graph.deleteList)
            {
                var dbSet = changeSet.dbSets.Where(d => d.dbSetName == rowInfo.dbSetInfo.dbSetName).Single();
                var req   = new RequestContext(this, changeSet: changeSet, dbSet: dbSet, rowInfo: rowInfo,
                                               operation: ServiceOperationType.SaveChanges);
                using (var callContext = new RequestCallContext(req))
                {
                    rowInfo.changeState = new EntityChangeState();
                    ApplyChangesToEntity(rowInfo);
                }
            }

            var hasErrors = false;

            //Validation step
            foreach (var rowInfo in graph.insertList)
            {
                var dbSet = changeSet.dbSets.Where(d => d.dbSetName == rowInfo.dbSetInfo.dbSetName).Single();
                var req   = new RequestContext(this, changeSet: changeSet, dbSet: dbSet, rowInfo: rowInfo,
                                               operation: ServiceOperationType.SaveChanges);
                using (var callContext = new RequestCallContext(req))
                {
                    if (!await _serviceHelper.ValidateEntity(metadata, req).ConfigureAwait(false))
                    {
                        rowInfo.invalid = rowInfo.changeState.ValidationErrors;
                        hasErrors       = true;
                    }
                }
            }

            //Validation step
            foreach (var rowInfo in graph.updateList)
            {
                var dbSet = changeSet.dbSets.Where(d => d.dbSetName == rowInfo.dbSetInfo.dbSetName).Single();
                var req   = new RequestContext(this, changeSet: changeSet, dbSet: dbSet, rowInfo: rowInfo,
                                               operation: ServiceOperationType.SaveChanges);
                using (var callContext = new RequestCallContext(req))
                {
                    if (!await _serviceHelper.ValidateEntity(metadata, req).ConfigureAwait(false))
                    {
                        rowInfo.invalid = rowInfo.changeState.ValidationErrors;
                        hasErrors       = true;
                    }
                }
            }

            if (hasErrors)
            {
                return(false);
            }

            var reqCntxt = new RequestContext(this, changeSet: changeSet, operation: ServiceOperationType.SaveChanges);

            using (var callContext = new RequestCallContext(reqCntxt))
            {
                await ExecuteChangeSet().ConfigureAwait(false);


                foreach (var rowInfo in graph.allList)
                {
                    if (rowInfo.changeType != ChangeType.Deleted)
                    {
                        _serviceHelper.UpdateRowInfoAfterUpdates(rowInfo);
                    }
                    else
                    {
                        rowInfo.values = null;
                    }
                }


                //Track changes step
                foreach (var rowInfo in graph.allList)
                {
                    TrackChangesToEntity(rowInfo);
                }
            }
            //OK, All updates are commited
            return(true);
        }