.NET 6中的EventStore和EventMetadata

发布于 2025-02-11 15:05:38 字数 1159 浏览 2 评论 0原文

当我将事件附加到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 6eventStore.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2025-02-18 15:05:39

元数据只是一个JSON对象。您可以按照事件数据进行序列化。

我不确定为什么您会使用EventMetadataSystem.diagenostics.eventing.reader类型,因为它与EventStoredB客户端无关。

您不一定需要将事件类型存储在元数据中,因为它已经在已解决的事件中,eventType type string的属性。

如果您不需要事件元,则可以避免阅读。如果您需要读取功能与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 from System.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 type string.

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 for null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文