Skip to content

Commit 6677a46

Browse files
committed
[DEVEX-222] Added automatic type resolution
1 parent ef604ff commit 6677a46

File tree

3 files changed

+77
-55
lines changed

3 files changed

+77
-55
lines changed

src/Kurrent.Client/Core/Serialization/MessageSerializer.cs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,6 @@ public interface IMessageSerializer {
1111
public bool TryDeserialize(EventRecord messageRecord, out object? deserialized);
1212
}
1313

14-
public interface IMessageTypeResolutionStrategy {
15-
string ResolveTypeName(object messageData);
16-
17-
#if NET48
18-
bool TryResolveClrType(EventRecord messageRecord, out Type? type);
19-
#else
20-
bool TryResolveClrType(EventRecord messageRecord, [NotNullWhen(true)] out Type? type);
21-
#endif
22-
}
23-
24-
public class DefaultMessageTypeResolutionStrategy(IMessageTypeMapper messageTypeMapper) : IMessageTypeResolutionStrategy {
25-
public string ResolveTypeName(object messageData) {
26-
return messageTypeMapper.GetTypeName(messageData.GetType())!;
27-
}
28-
29-
#if NET48
30-
public bool TryResolveClrType(EventRecord messageRecord, out Type? type){
31-
#else
32-
public bool TryResolveClrType(EventRecord messageRecord, [NotNullWhen(true)] out Type? type) {
33-
#endif
34-
type = messageTypeMapper.GetClrType(messageRecord.EventType);
35-
36-
return type != null;
37-
}
38-
}
39-
4014
public record MessageDefaultMetadata(
4115
[property: JsonPropertyName("$clrTypeAssemblyQualifiedName")]
4216
string? MessageTypeAssemblyQualifiedName,

src/Kurrent.Client/Core/Serialization/MessageTypeMapper.cs

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,64 @@ namespace Kurrent.Client.Tests.Streams.Serialization;
66
// The scanning part and registration seems to be more robust there
77
// I used this for simplicity
88
public interface IMessageTypeMapper {
9-
void AddType<T>(string eventTypeName);
10-
void AddType(Type eventType, string eventTypeName);
9+
void AddType<T>(string messageTypeName);
10+
void AddType(Type messageType, string eventTypeName);
1111
string? GetTypeName<TEventType>();
12-
string? GetTypeName(Type eventType);
13-
Type? GetClrType(string eventTypeName);
12+
string? GetTypeName(Type messageType);
13+
string GetOrAddTypeName(Type clrType, Func<Type, string> getTypeName);
14+
Type? GetClrType(string messageTypeName);
15+
Type? GetOrAddClrType(string messageTypeName, Func<string, Type?> getClrType);
1416
}
1517

1618
public class MessageTypeMapper : IMessageTypeMapper {
1719
public static readonly MessageTypeMapper Instance = new MessageTypeMapper();
1820
readonly ConcurrentDictionary<string, Type?> _typeMap = new();
1921
readonly ConcurrentDictionary<Type, string> _typeNameMap = new();
2022

21-
public void AddType<T>(string eventTypeName) => AddType(typeof(T), eventTypeName);
23+
public void AddType<T>(string messageTypeName) => AddType(typeof(T), messageTypeName);
2224

23-
public void AddType(Type eventType, string eventTypeName) {
24-
_typeNameMap.AddOrUpdate(eventType, eventTypeName, (_, typeName) => typeName);
25-
_typeMap.AddOrUpdate(eventTypeName, eventType, (_, type) => type);
25+
public void AddType(Type messageType, string eventTypeName) {
26+
_typeNameMap.AddOrUpdate(messageType, eventTypeName, (_, typeName) => typeName);
27+
_typeMap.AddOrUpdate(eventTypeName, messageType, (_, type) => type);
2628
}
2729

28-
public string? GetTypeName<TEventType>() => GetTypeName(typeof(TEventType));
30+
public string? GetTypeName<TMessageType>() => GetTypeName(typeof(TMessageType));
2931

30-
public string? GetTypeName(Type eventType) =>
32+
public string? GetTypeName(Type messageType) =>
3133
#if NET48
32-
_typeNameMap.TryGetValue(eventType, out var typeName) ? typeName : null;
34+
_typeNameMap.TryGetValue(messageType, out var typeName) ? typeName : null;
3335
#else
34-
_typeNameMap.GetValueOrDefault(eventType);
36+
_typeNameMap.GetValueOrDefault(messageType);
3537
#endif
38+
39+
public string GetOrAddTypeName(Type clrType, Func<Type, string> getTypeName) =>
40+
_typeNameMap.GetOrAdd(clrType,
41+
_ => {
42+
var typeName = getTypeName(clrType);
43+
44+
_typeMap.TryAdd(typeName, clrType);
3645

37-
public Type? GetClrType(string eventTypeName) =>
46+
return typeName;
47+
});
48+
49+
50+
public Type? GetClrType(string messageTypeName) =>
3851
#if NET48
39-
_typeMap.TryGetValue(eventTypeName, out var clrType) ? clrType : null;
52+
_typeMap.TryGetValue(messageTypeName, out var clrType) ? clrType : null;
4053
#else
41-
_typeMap.GetValueOrDefault(eventTypeName);
54+
_typeMap.GetValueOrDefault(messageTypeName);
4255
#endif
4356

44-
public Type? GetClrTypeOrAdd(string eventTypeName, Func<string, Type?> getClrType) =>
45-
_typeMap.GetOrAdd(eventTypeName, getClrType);
46-
47-
// _ => {
48-
// var type = TypeProvider.GetFirstMatchingTypeFromCurrentDomainAssembly(eventTypeName);
49-
//
50-
// if (type == null)
51-
// return null;
52-
//
53-
// _typeNameMap.TryAdd(type, eventTypeName);
54-
//
55-
// return type;
56-
// }
57-
// );
57+
public Type? GetOrAddClrType(string messageTypeName, Func<string, Type?> getClrType) =>
58+
_typeMap.GetOrAdd(messageTypeName,
59+
_ => {
60+
var clrType = getClrType(messageTypeName);
61+
62+
if (clrType == null)
63+
return null;
64+
65+
_typeNameMap.TryAdd(clrType, messageTypeName);
66+
67+
return clrType;
68+
});
5869
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Kurrent.Client.Tests.Streams.Serialization;
3+
4+
namespace EventStore.Client.Serialization;
5+
6+
public interface IMessageTypeResolutionStrategy {
7+
string ResolveTypeName(object messageData);
8+
9+
#if NET48
10+
bool TryResolveClrType(EventRecord messageRecord, out Type? type);
11+
#else
12+
bool TryResolveClrType(EventRecord messageRecord, [NotNullWhen(true)] out Type? type);
13+
#endif
14+
}
15+
16+
public class DefaultMessageTypeResolutionStrategy(IMessageTypeMapper messageTypeMapper)
17+
: IMessageTypeResolutionStrategy {
18+
public string ResolveTypeName(object messageData) {
19+
return messageTypeMapper.GetOrAddTypeName(
20+
messageData.GetType(),
21+
clrType => clrType.FullName!
22+
)!;
23+
}
24+
25+
#if NET48
26+
public bool TryResolveClrType(EventRecord messageRecord, out Type? type) {
27+
#else
28+
public bool TryResolveClrType(EventRecord messageRecord, [NotNullWhen(true)] out Type? type) {
29+
#endif
30+
type = messageTypeMapper.GetOrAddClrType(
31+
messageRecord.EventType,
32+
TypeProvider.GetFirstMatchingTypeFromCurrentDomainAssembly
33+
);
34+
35+
return type != null;
36+
}
37+
}

0 commit comments

Comments
 (0)