public static ItemWithType ToItemWithType(this ResolvedEvent resolvedEvent, ICreateStateInstances stateFactory)
        {
            var serializedEvent = System.Text.Encoding.UTF8.GetString(resolvedEvent.Event.Data);

            Type type = ReadTypeFromMetadata(resolvedEvent);

            object @event;

            if (type.IsInterface)
            {
                @event = stateFactory.GetState(type);
                var switchable = @event as ICanSwitchBackAndToReadOnly;

                if (switchable != null)
                {
                    switchable.CanEdit = true;
                }

                JsonConvert.PopulateObject(serializedEvent, @event);

                if (switchable != null)
                {
                    switchable.CanEdit = false;
                }
            }
            else
            {
                @event = JsonConvert.DeserializeObject(serializedEvent, type);
            }

            return(new ItemWithType(@event, type));
        }
Exemple #2
0
        public static StoredEvent ToStoredEvent(this EventRecord resolvedEvent,
                                                ICreateStateInstances stateFactory)
        {
            var serializedEvent = System.Text.Encoding.UTF8
                                  .GetString(resolvedEvent.Data.Span);

            var(metadata, type) = ReadTypeFromMetadata(resolvedEvent);

            object @event;

            if (type.IsInterface)
            {
                @event = stateFactory.GetState(type);
                var switchable = @event as ICanSwitchBackAndToReadOnly;

                if (switchable != null)
                {
                    switchable.CanEdit = true;
                }

                JsonConvert.PopulateObject(serializedEvent, @event);

                if (switchable != null)
                {
                    switchable.CanEdit = false;
                }
            }
            else
            {
                @event = JsonConvert.DeserializeObject(serializedEvent, type);
            }

            return(new StoredEvent(@event, type, resolvedEvent.EventStreamId, metadata,
                                   resolvedEvent.EventNumber.ToInt64()));
        }
        private static StoredEvent ToStoredEvent(string streamId, long eventNumber, ReadOnlyMemory <byte> data, ReadOnlyMemory <byte> meta,
                                                 string eventType, ICreateStateInstances stateFactory)
        {
            var serializedEvent = System.Text.Encoding.UTF8.GetString(data.Span);

            var(metadata, type) = ReadTypeFromMetadata(eventType, meta);

            object @event;

            if (type.IsInterface)
            {
                @event = stateFactory.GetState(type);
                var switchable = @event as ICanSwitchBackAndToReadOnly;

                if (switchable != null)
                {
                    switchable.CanEdit = true;
                }

                JsonConvert.PopulateObject(serializedEvent, @event);

                if (switchable != null)
                {
                    switchable.CanEdit = false;
                }
            }
            else
            {
                @event = JsonConvert.DeserializeObject(serializedEvent, type);
            }

            return(new StoredEvent(@event, type, streamId, metadata, Convert.ToInt64(eventNumber)));
        }