Пример #1
0
        public async Task AppendToStream(string streamId, IEnumerable <StorageEvent> events)
        {
            var documents = events
                            .Select(
                document => DocumentDbStorageEvent.FromStorageEvent(
                    document,
                    typeMap,
                    collectionOptions.DocumentTimeToLiveSeconds))
                            .ToList();

            try
            {
                var result = await client.ExecuteStoredProcedureAsync <dynamic>(
                    appendStoredProcedureLink,
                    new RequestOptions
                {
                    PartitionKey     = new PartitionKey(streamId),
                    ConsistencyLevel = collectionOptions.ConsistencyLevel
                },
                    documents);

                loggingOptions.OnSuccess(ResponseInformation.FromWriteResponse(nameof(AppendToStream), result));
            }
            catch (DocumentClientException exception)
            {
                if (exception.Error.Message.Contains(ConcurrencyConflictErrorKey))
                {
                    throw new ConcurrencyException(exception.Error.Message, exception);
                }

                throw;
            }
        }
        public static DocumentDbStorageEvent FromDocument(Document document)
        {
            var docDbEvent = new DocumentDbStorageEvent
            {
                Id           = document.GetPropertyValue <string>("id"),
                EventId      = document.GetPropertyValue <Guid>("eventId"),
                Body         = document.GetPropertyValue <JObject>("body"),
                BodyType     = document.GetPropertyValue <string>("bodyType"),
                Metadata     = document.GetPropertyValue <JObject>("metadata"),
                MetadataType = document.GetPropertyValue <string>("metadataType"),
                StreamId     = document.GetPropertyValue <string>("streamId"),
                EventNumber  = document.GetPropertyValue <int>("eventNumber")
            };

            return(docDbEvent);
        }
        public static DocumentDbStorageEvent FromStorageEvent(StorageEvent @event, ISerializationTypeMap typeMap)
        {
            var docDbEvent = new DocumentDbStorageEvent();

            docDbEvent.Id       = $"{@event.StreamId}:{@event.EventNumber}";
            docDbEvent.EventId  = @event.EventId;
            docDbEvent.Body     = JObject.FromObject(@event.EventBody);
            docDbEvent.BodyType = typeMap.GetNameFromType(@event.EventBody.GetType());
            if (@event.Metadata != null)
            {
                docDbEvent.Metadata     = JObject.FromObject(@event.Metadata);
                docDbEvent.MetadataType = typeMap.GetNameFromType(@event.Metadata.GetType());
            }
            docDbEvent.StreamId    = @event.StreamId;
            docDbEvent.EventNumber = @event.EventNumber;

            return(docDbEvent);
        }
        public static DocumentDbStorageEvent FromStorageEvent(StorageEvent @event, ISerializationTypeMap typeMap, int?documentTimeToLiveSeconds)
        {
            var docDbEvent = new DocumentDbStorageEvent
            {
                Id                = $"{@event.StreamId}:{@event.EventNumber}",
                EventId           = @event.EventId,
                Body              = JObject.FromObject(@event.EventBody),
                BodyType          = typeMap.GetNameFromType(@event.EventBody.GetType()),
                TimeToLiveSeconds = documentTimeToLiveSeconds
            };

            if (@event.Metadata != null)
            {
                docDbEvent.Metadata     = JObject.FromObject(@event.Metadata);
                docDbEvent.MetadataType = typeMap.GetNameFromType(@event.Metadata.GetType());
            }
            docDbEvent.StreamId    = @event.StreamId;
            docDbEvent.EventNumber = @event.EventNumber;

            return(docDbEvent);
        }