-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathStreamNameMapper.cs
46 lines (36 loc) · 1.84 KB
/
StreamNameMapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
namespace Core.Events;
public class StreamNameMapper
{
private static readonly StreamNameMapper Instance = new();
private readonly ConcurrentDictionary<Type, string> TypeNameMap = new();
public static void AddCustomMap<TStream>(string mappedStreamName) =>
AddCustomMap(typeof(TStream), mappedStreamName);
public static void AddCustomMap(Type streamType, string mappedStreamName)
{
Instance.TypeNameMap.AddOrUpdate(streamType, mappedStreamName, (_, _) => mappedStreamName);
}
public static string ToStreamPrefix<TStream>() => ToStreamPrefix(typeof(TStream));
public static string ToStreamPrefix(Type streamType) => Instance.TypeNameMap.GetOrAdd(streamType, _ =>
{
var modulePrefix = streamType.Namespace!.Split('.').First();
return $"{modulePrefix}_{streamType.Name}";
});
public static string ToStreamId<TStream>(object aggregateId, object? tenantId = null) =>
ToStreamId(typeof(TStream), aggregateId);
/// <summary>
/// Generates a stream id in the canonical `{category}-{aggregateId}` format.
/// It can be expanded to the `{module}_{streamType}-{tenantId}_{aggregateId}` format
/// </summary>
/// <param name="streamType"></param>
/// <param name="aggregateId"></param>
/// <param name="tenantId"></param>
/// <returns></returns>
public static string ToStreamId(Type streamType, object aggregateId, object? tenantId = null)
{
var tenantPrefix = tenantId != null ? $"{tenantId}_" : "";
var streamCategory = ToStreamPrefix(streamType);
// (Out-of-the box, the category projection treats anything before a `-` separator as the category name)
// For this reason, we place the "{tenantId}_" bit (if present) on the right hand side of the '-'
return $"{streamCategory}-{tenantPrefix}{aggregateId}";
}
}