public static IKernelEventEnvelope Create(KernelEvent @event) { var factory = _envelopeFactories.GetOrAdd( @event.GetType(), eventType => { var genericType = _envelopeTypesByEventTypeName[@event.GetType().Name]; var constructor = genericType.GetConstructors().Single(); var eventParameter = Expression.Parameter( typeof(KernelEvent), "e"); var newExpression = Expression.New( constructor, Expression.Convert(eventParameter, eventType)); var expression = Expression.Lambda <Func <KernelEvent, IKernelEventEnvelope> >( newExpression, eventParameter); return(expression.Compile()); }); var envelope = factory(@event); return(envelope); }
public void All_event_types_are_round_trip_serializable(KernelEvent @event) { var originalEnvelope = KernelEventEnvelope.Create(@event); var json = KernelEventEnvelope.Serialize(originalEnvelope); _output.WriteLine($"{Environment.NewLine}{@event.GetType().Name}: {Environment.NewLine}{json}"); var deserializedEnvelope = KernelEventEnvelope.Deserialize(json); // ignore these specific properties because they're not serialized var ignoredProperties = new HashSet <string> { $"{nameof(CommandFailed)}.{nameof(CommandFailed.Exception)}", $"{nameof(DisplayEvent)}.{nameof(DisplayEvent.Value)}" }; deserializedEnvelope .Should() .BeEquivalentToRespectingRuntimeTypes( originalEnvelope, o => o.Excluding(envelope => envelope.Event.Command.Properties) .Excluding(envelope => ignoredProperties.Contains($"{envelope.SelectedMemberInfo.DeclaringType.Name}.{envelope.SelectedMemberInfo.Name}")) ); }
public void Event_contract_has_not_been_broken(KernelEvent @event) { var _configuration = new Configuration() .UsingExtension($"{@event.GetType().Name}.json") .SetInteractive(Debugger.IsAttached); @event.Command?.SetToken("the-token"); var json = KernelEventEnvelope.Serialize(@event); this.Assent(json, _configuration); }
public void All_event_types_are_round_trip_serializable(KernelEvent @event) { var originalEnvelope = KernelEventEnvelope.Create(@event); var json = KernelEventEnvelope.Serialize(originalEnvelope); _output.WriteLine($"{Environment.NewLine}{@event.GetType().Name}: {Environment.NewLine}{json}"); var deserializedEnvelope = KernelEventEnvelope.Deserialize(json); deserializedEnvelope .Should() .BeEquivalentTo(originalEnvelope, o => o.Excluding(envelope => envelope.Event.Command.Properties)); }