/// <summary>
        /// Converts an <see cref="UncommittedEventStream" /> into its <see cref="BsonDocument" /> representation
        /// </summary>
        /// <param name="uncommittedEvents">The <see cref="UncommittedEventStream" /></param>
        /// <returns>A <see cref="BsonDocument" /> representation of the <see cref="UncommittedEventStream" /></returns>
        public static BsonDocument AsBsonCommit(this UncommittedEventStream uncommittedEvents)
        {
            var eventDocs = uncommittedEvents.Events.Select(e =>
            {
                return(new BsonDocument(new Dictionary <string, object>
                {
                    { Constants.ID, e.Id.Value },
                    { Constants.CORRELATION_ID, e.Metadata.CorrelationId.Value },
                    { EventConstants.EVENT_ARTIFACT, e.Metadata.Artifact.Id.Value },
                    { Constants.GENERATION, e.Metadata.Artifact.Generation.Value },
                    { Constants.EVENT_SOURCE_ARTIFACT, uncommittedEvents.Source.Artifact.Value },
                    { Constants.EVENTSOURCE_ID, e.Metadata.EventSourceId.Value },
                    { VersionConstants.COMMIT, e.Metadata.VersionedEventSource.Version.Commit },
                    { VersionConstants.SEQUENCE, e.Metadata.VersionedEventSource.Version.Sequence },
                    { EventConstants.OCCURRED, e.Metadata.Occurred.ToUnixTimeMilliseconds() },
                    { EventConstants.ORIGINAL_CONTEXT, e.Metadata.OriginalContext.AsBson() },
                    { EventConstants.EVENT, PropertyBagBsonSerializer.Serialize(e.Event) }
                }));
            });

            var doc = new BsonDocument(new Dictionary <string, object>
            {
                { Constants.ID, 0 },
                { Constants.CORRELATION_ID, uncommittedEvents.CorrelationId.Value },
                { CommitConstants.COMMIT_ID, uncommittedEvents.Id.Value },
                { CommitConstants.TIMESTAMP, uncommittedEvents.Timestamp.ToUnixTimeMilliseconds() },
                { Constants.EVENTSOURCE_ID, uncommittedEvents.Source.EventSource.Value },
                { Constants.EVENT_SOURCE_ARTIFACT, uncommittedEvents.Source.Artifact.Value },
                { VersionConstants.COMMIT, uncommittedEvents.Source.Version.Commit },
                { VersionConstants.SEQUENCE, uncommittedEvents.Source.Version.Sequence },
                { CommitConstants.EVENTS, new BsonArray(eventDocs) },
            });

            return(doc);
        }
Exemple #2
0
 /// <summary>
 /// Converts a <see cref="BsonValue" /> into a <see cref="PropertyBag" />
 /// </summary>
 /// <param name="value">The <see cref="BsonValue" /></param>
 /// <returns>The corresponding <see cref="PropertyBag" /></returns>
 public static PropertyBag ToPropertyBag(this BsonValue value)
 {
     return(PropertyBagBsonSerializer.Deserialize(value.AsBsonDocument));
 }