public static EventData CreateEventData(this ItemWithType @event)
        {
            var metadata = EventMetadata_V1.From(@event);

            var eventAsJson = JObject.FromObject(@event.instance);

            eventAsJson.Remove(CanEditJsonFieldName);

            var eventData = new EventData(
                Guid.NewGuid(),
                @event.type.Name,
                true,
                System.Text.Encoding.UTF8.GetBytes(eventAsJson.ToString()),
                MetadataSerializer.Serialize(metadata));

            return(eventData);
        }
Example #2
0
        private ItemWithType GetEventFromEventData(ResolvedEvent resolvedEvent)
        {
            var  jobject = JObject.Parse(System.Text.Encoding.UTF8.GetString(resolvedEvent.Event.Data));
            Type type;

            (IHoldMetadata metadata, int version)metadata;

            if (resolvedEvent.Event.Metadata == null || resolvedEvent.Event.Metadata.Length == 0)
            {
                type = Type.GetType(resolvedEvent.Event.EventType);
            }
            else
            {
                metadata = MetadataSerializer.DeserializeMetadata(resolvedEvent.Event.Metadata);
                type     = AppDomain.CurrentDomain.GetAssemblies()
                           .Select(x => x.GetType(metadata.metadata.EventTypeFQN))
                           .FirstOrDefault(x => x != null);
            }

            object @event;

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

                var canEdit = jobject.Property("canEdit");
                canEdit.Remove();

                switchable.CanEdit = true;
                var reader     = jobject.CreateReader();
                var serializer = new JsonSerializer();
                serializer.Populate(reader, @event);
                switchable.CanEdit = false;
            }
            else
            {
                @event = jobject.ToObject(type);
            }

            return(new ItemWithType(@event, type));
        }