|
| 1 | +using System.Dynamic; |
| 2 | +using System.Text.Json; |
| 3 | +using MicroPlumberd.Utils; |
| 4 | + |
| 5 | +namespace MicroPlumberd; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Factory for creating <see cref="Metadata"/> instances with proper JSON schema. |
| 9 | +/// Centralizes metadata construction that was previously scattered across PlumberEngine, EventAggregator, and tests. |
| 10 | +/// </summary> |
| 11 | +public class MetadataFactory |
| 12 | +{ |
| 13 | + private readonly IReadOnlyConventions _conventions; |
| 14 | + |
| 15 | + /// <summary> |
| 16 | + /// Creates a factory with default conventions (Created timestamp, InvocationContext enrichers). |
| 17 | + /// </summary> |
| 18 | + public MetadataFactory() : this(new Conventions()) { } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Creates a factory with custom conventions. |
| 22 | + /// </summary> |
| 23 | + /// <param name="conventions">The conventions to use for metadata enrichment and stream naming.</param> |
| 24 | + public MetadataFactory(IReadOnlyConventions conventions) => _conventions = conventions; |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Creates metadata from raw EventStore fields. |
| 28 | + /// Used by PlumberEngine when reading events from EventStore. |
| 29 | + /// </summary> |
| 30 | + public Metadata Create( |
| 31 | + Guid id, |
| 32 | + string sourceStreamId, |
| 33 | + Guid eventId, |
| 34 | + long sourceStreamPosition, |
| 35 | + long? linkStreamPosition, |
| 36 | + JsonElement data) |
| 37 | + { |
| 38 | + return new Metadata(id, eventId, sourceStreamPosition, linkStreamPosition, sourceStreamId, data); |
| 39 | + } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Creates metadata from an event object. Conventions compute the sourceStreamId and enrich metadata. |
| 43 | + /// The <see cref="Metadata.Id"/> is extracted from the event's Id property via duck typing if present. |
| 44 | + /// Used by EventAggregator and other in-process event sources. |
| 45 | + /// </summary> |
| 46 | + public Metadata Create( |
| 47 | + OperationContext context, |
| 48 | + object evt, |
| 49 | + object? id = null, |
| 50 | + object? customMetadata = null, |
| 51 | + IAggregate? aggregate = null) |
| 52 | + { |
| 53 | + var sourceStreamId = _conventions.StreamNameFromEventConvention(context, evt.GetType(), id); |
| 54 | + var enriched = _conventions.GetMetadata(context, aggregate, evt, customMetadata); |
| 55 | + var data = JsonSerializer.SerializeToElement(enriched); |
| 56 | + var eventId = _conventions.GetEventIdConvention(context, aggregate, evt).ToGuid(); |
| 57 | + IdDuckTyping.Instance.TryGetGuidId(evt, out var metadataId); |
| 58 | + return new Metadata(metadataId, eventId, 0, null, sourceStreamId, data); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Creates metadata with explicit field values. |
| 63 | + /// Useful for tests and simple scenarios where you know the sourceStreamId. |
| 64 | + /// </summary> |
| 65 | + public Metadata Create( |
| 66 | + string sourceStreamId, |
| 67 | + DateTimeOffset? created = null, |
| 68 | + Guid? correlationId = null, |
| 69 | + Guid? causationId = null, |
| 70 | + string? userId = null) |
| 71 | + { |
| 72 | + var data = BuildData(created, correlationId, causationId, userId); |
| 73 | + return new Metadata(Guid.Empty, Guid.NewGuid(), 0, null, sourceStreamId, data); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Creates metadata for a specific event type and id. Conventions compute the sourceStreamId. |
| 78 | + /// The <see cref="Metadata.Id"/> and <see cref="Metadata.EventId"/> are extracted from the event's Id property via duck typing if present. |
| 79 | + /// </summary> |
| 80 | + public Metadata Create<TEvent, TId>( |
| 81 | + TId id, |
| 82 | + TEvent evt, |
| 83 | + DateTimeOffset? created = null, |
| 84 | + Guid? correlationId = null, |
| 85 | + Guid? causationId = null, |
| 86 | + string? userId = null) |
| 87 | + { |
| 88 | + var context = OperationContext.Create(Flow.Component); |
| 89 | + var sourceStreamId = _conventions.StreamNameFromEventConvention(context, typeof(TEvent), id); |
| 90 | + var data = BuildData(created, correlationId, causationId, userId); |
| 91 | + var eventId = _conventions.GetEventIdConvention(context, null, evt!).ToGuid(); |
| 92 | + IdDuckTyping.Instance.TryGetGuidId(evt!, out var metadataId); |
| 93 | + return new Metadata(metadataId, eventId, 0, null, sourceStreamId, data); |
| 94 | + } |
| 95 | + |
| 96 | + private static JsonElement BuildData( |
| 97 | + DateTimeOffset? created, |
| 98 | + Guid? correlationId, |
| 99 | + Guid? causationId, |
| 100 | + string? userId) |
| 101 | + { |
| 102 | + IDictionary<string, object> obj = new ExpandoObject(); |
| 103 | + if (created.HasValue) obj["Created"] = created.Value; |
| 104 | + if (correlationId.HasValue) obj["$correlationId"] = correlationId.Value; |
| 105 | + if (causationId.HasValue) obj["$causationId"] = causationId.Value; |
| 106 | + if (userId != null) obj["UserId"] = userId; |
| 107 | + return JsonSerializer.SerializeToElement(obj); |
| 108 | + } |
| 109 | +} |
0 commit comments