.NET 6中的EventStore和EventMetadata
当我将事件附加到EventsTream日志中时 ...
var myEvents = events.Select(@event => new EventData(
Guid.NewGuid(),
@event.GetType().Name,
true,
Serialize(@event),
null //metadata
)).ToArray();
return connection.AppendToStreamAsync(streamName, version, myEvents);
下面是一个示例,当我从商店中检索事件时,我如何对事件进行挑选,这是行不通的,因为没有元数据,我无法对其进行审理(我很难插入,这是无效的,因为我不知道如何插入它)。
我有一种从EventStore.Clientapi进行估算化resolvedevent的扩展方法。
using System.Diagnostics.Eventing.Reader
public static class EventDeserializer
{
public static object Deserialize(this ResolvedEvent resolvedEvent)
{
var meta = JsonConvert.DeserializeObject<EventMetadata>
(Encoding.UTF8.GetString(resolvedEvent.Event.Metadata));
var dataType = Type.GetType(meta.GetType().ToString());
var jsonData = Encoding.UTF8.GetString(resolvedEvent.Event.Data);
return JsonConvert.DeserializeObject(jsonData, dataType);
}
}
如果事项,我正在使用.net 6
和eventStore.client 5.0.12
。 只是为了提供信息,我可以将活动插入商店(无元数据)。这里的问题是当我加载事件并试图将其进行应有的态度时。
When I append events to the eventstream log I'm using
...
var myEvents = events.Select(@event => new EventData(
Guid.NewGuid(),
@event.GetType().Name,
true,
Serialize(@event),
null //metadata
)).ToArray();
return connection.AppendToStreamAsync(streamName, version, myEvents);
Below is an example how I deserialize the event when I retrieve the event from the store, this will not work because I cannot deserialize it without metadata (which I struggle to insert, it's null cause I don't know how to insert it).
I have an extension method for deserializing ResolvedEvent from EventStore.ClientAPI.
using System.Diagnostics.Eventing.Reader
public static class EventDeserializer
{
public static object Deserialize(this ResolvedEvent resolvedEvent)
{
var meta = JsonConvert.DeserializeObject<EventMetadata>
(Encoding.UTF8.GetString(resolvedEvent.Event.Metadata));
var dataType = Type.GetType(meta.GetType().ToString());
var jsonData = Encoding.UTF8.GetString(resolvedEvent.Event.Data);
return JsonConvert.DeserializeObject(jsonData, dataType);
}
}
if matters I'm using .net 6
and EventStore.Client 5.0.12
.
Just for the info, I'm able to insert the event in the store (without metadata). The issue here is when I load the event and trying to deseralize it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
元数据只是一个JSON对象。您可以按照事件数据进行序列化。
我不确定为什么您会使用
EventMetadata
从System.diagenostics.eventing.reader
类型,因为它与EventStoredB客户端无关。您不一定需要将事件类型存储在元数据中,因为它已经在已解决的事件中,
eventType
typestring
的属性。如果您不需要事件元,则可以避免阅读。如果您需要读取功能与
null
meta一起使用,则可以检查null
。Metadata is just a JSON object. You can serialize it the same way you do it with the event data.
I am not sure why you use the
EventMetadata
type fromSystem.Diagnostics.Eventing.Reader
as it has nothing to do with the EventStoreDB client.You don't necessarily need to store the event type in metadata as it's already in the resolved event, the
EventType
property of typestring
.If you don't need event meta, you can avoid reading it. If you need the reading function to work with
null
meta, you can check fornull
.