Exemplo n.º 1
0
        /// <summary>
        /// Creates a <see cref="Event"/> instance out of <see cref="EventDocument"/> instance
        /// which can then be posted into IEventProducer. You can use this method to obtain raw event
        /// once in order to possibly re-try posting if post fails. The system maintains idempotency of
        /// Event posts, therefore a caller may hang to returned Event instance to retry failed posting
        /// </summary>
        /// <param name="producer">Producer to eventually post event into</param>
        /// <param name="evtDoc">EventDocument-derivative instance</param>
        /// <param name="headers">Event headers or null</param>
        /// <returns>
        ///   A tuple of (EventAttribute attr, ShardKey partition, Event evt) suitable for
        ///   making a call to<see cref="IEventProducer.PostAsync(Route, ShardKey, Event, DataLossMode)"/>
        /// </returns>
        public static (EventAttribute attr, ShardKey partition, Event evt) GetRawEventForDocument(this IEventProducer producer,
                                                                                                  EventDocument evtDoc,
                                                                                                  JsonDataMap headers = null)
        {
            var partition    = evtDoc.NonNull(nameof(evtDoc)).GetEventPartition();
            var eventHeaders = evtDoc.GetEventHeaders(headers);

            var rawHeaders = eventHeaders != null?JsonWriter.Write(eventHeaders, JsonWritingOptions.CompactRowsAsMap) : null;

            var rawJson = JsonWriter.WriteToBuffer(evtDoc, JsonWritingOptions.CompactRowsAsMap, EVENT_JSON_ENCODING);

            var rawEvent = producer.NonNull(nameof(producer))
                           .MakeNew(CONTENT_TYPE_JSON_DOC, rawJson, rawHeaders);

            var attr = EventAttribute.GetFor(evtDoc.GetType());

            return(attr, partition, rawEvent);
        }