Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Whats the suggested way to declare strongly typed events? (And why is CloudEvent sealed?)

Aperta
#308 5 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
csharp, rabbitmq

Direzione di ricerca

Non sono stati identificati file del repository, test o punti di ingresso dell’implementazione. Inizia esaminando la CloudEvent API e il BaseEvent fortemente tipizzato, nonché l’approccio alla conversione mostrato nell’issue. Il lavoro è completato quando viene fornita una raccomandazione definitiva per le dichiarazioni fortemente tipizzate e vengono spiegate le scelte progettuali relative a sealed-class e class-versus-record.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hi all,

I have bee working in event-driven architectures using RabbitMQ for the last years, and the approach, for relatively small products, is that the project that owns an event is declaring the event in a separate project like (oversimplifying):

public record MyEvent(string topic, ..., MyPayload payload) : BaseEvent { ... }

A library receives the instance and serializes it all to publish it to RabbitMQ. And on the subscribing side, it uses the class definition to deserialize the string and pass it onto the subscription.

This allows all actors (publishers, subscribers, tests, etc) to use a single definition for a single event while freeing all the code from serialization, deserialization and casting.

I am looking to migrate this implementation to CloudEvents, so the first thing I thought about was:

public record BaseEvent<T> : CloudEvent
{
   // constructor...
   
   // json attribute
   public TPayload Payload {get:}
}

But CloudEvent is sealed :(

My first alternative is to declare my BaseEvent with operators to cast to and from CloudEvent:

using CloudNative.CloudEvents;
using System.Text.Json;

public abstract record BaseEvent<TPayload>
{
    public string Id { get; init; } = Guid.NewGuid().ToString();
    public string Type { get; init; }
    public Uri Source { get; init; }
    public string Subject { get; init; }
    public DateTimeOffset Time { get; init; } = DateTimeOffset.UtcNow;
    public TPayload Data { get; init; }

    protected BaseEvent(TPayload data, string source, string type, string subject = null)
    {
        Data = data;
        Source = new Uri(source);
        Type = type;
        Subject = subject;
    }

    public static implicit operator CloudEvent(BaseEvent<TPayload> baseEvent)
    {
        return new CloudEvent
        {
            Id = baseEvent.Id,
            Type = baseEvent.Type,
            Source = baseEvent.Source,
            Subject = baseEvent.Subject,
            Time = baseEvent.Time,
            Data = JsonSerializer.Serialize(baseEvent.Data),
            DataContentType = "application/json"
        };
    }

    public static implicit operator BaseEvent<TPayload>(CloudEvent cloudEvent)
    {
        var data = JsonSerializer.Deserialize<TPayload>(cloudEvent.Data.ToString());
        var baseEvent = Activator.CreateInstance(typeof(BaseEvent<TPayload>), data, cloudEvent.Source.ToString(), cloudEvent.Type, cloudEvent.Subject) as BaseEvent<TPayload>;
        baseEvent.Id = cloudEvent.Id;
        baseEvent.Time = cloudEvent.Time.Value;
        return baseEvent;
    }
}

But before I move forward, I am double checking:

  • What would be the proposed way to declare cloud events strongly typed?

An additionally:

  • Why is CloudEvent sealed?
  • Why is it a class and not a record?

Thanks

Lingua principale
C#
Stelle
334
Fork
88
Merge medio
6m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudevents/sdk-csharp

Tutte le issue di cloudevents/sdk-csharp

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.