Beispiel #1
0
        public static OeOperationMessage Create(OeMessageContext context, ODataBatchReader reader)
        {
            ODataBatchOperationRequestMessage batchRequest = reader.CreateOperationRequestMessage();
            var operation = new OeOperationMessage(batchRequest);

            if (batchRequest.Method == ODataConstants.MethodDelete)
            {
                operation.EntityItem = operation.ReadEntityFromUrl(context);
            }
            else
            {
                using (Stream stream = batchRequest.GetStream())
                    operation.EntityItem = operation.ReadEntityFromStream(context, stream);
            }
            return(operation);
        }
        private async Task ExecuteOperation(OeOperationMessage operation, CancellationToken cancellationToken)
        {
            Object dataContext = null;

            try
            {
                dataContext = _dataAdapter.CreateDataContext();
                AddToEntitySet(dataContext, operation);
                await _dataAdapter.SaveChangesAsync(_model, dataContext, cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                if (dataContext != null)
                {
                    _dataAdapter.CloseDataContext(dataContext);
                }
            }
        }
        private void AddToEntitySet(Object dataContext, OeOperationMessage operation)
        {
            Db.OeEntitySetAdapter entitySetAdapter = _dataAdapter.GetEntitySetAdapter(operation.EntityItem.EntitySet.Name);
            switch (operation.Method)
            {
            case ODataConstants.MethodDelete:
                entitySetAdapter.RemoveEntity(dataContext, operation.EntityItem.Entity);
                break;

            case ODataConstants.MethodPatch:
                entitySetAdapter.AttachEntity(dataContext, operation.EntityItem.Entity);
                break;

            case ODataConstants.MethodPost:
                entitySetAdapter.AddEntity(dataContext, operation.EntityItem.Entity);
                break;

            default:
                throw new NotImplementedException(operation.Method);
            }
        }
        public static OeBatchMessage CreateBatchMessage(OeMessageContext context, Stream requestStream, String contentType)
        {
            IODataRequestMessage requestMessage = new OeInMemoryMessage(requestStream, contentType);
            var settings = new ODataMessageReaderSettings()
            {
                EnableMessageStreamDisposal = false
            };

            using (var messageReader = new ODataMessageReader(requestMessage, settings))
            {
                var batchMessage             = new List <OeBatchMessage>();
                ODataBatchReader batchReader = messageReader.CreateODataBatchReader();
                while (batchReader.Read())
                {
                    if (batchReader.State == ODataBatchReaderState.ChangesetStart)
                    {
                        var operations = new List <OeOperationMessage>();
                        while (batchReader.Read() && batchReader.State != ODataBatchReaderState.ChangesetEnd)
                        {
                            if (batchReader.State == ODataBatchReaderState.Operation)
                            {
                                OeOperationMessage operation = OeOperationMessage.Create(context, batchReader);
                                operations.Add(operation);
                            }
                        }
                        return(new OeBatchMessage(contentType, operations));
                    }
                    else if (batchReader.State == ODataBatchReaderState.Operation)
                    {
                        OeOperationMessage operation = OeOperationMessage.Create(context, batchReader);
                        return(new OeBatchMessage(contentType, operation));
                    }
                }
            }

            throw new InvalidOperationException("batch not found");
        }
 private OeBatchMessage(String contentType, OeOperationMessage operation)
 {
     _contentType = contentType;
     _operation   = operation;
 }