@@ -11,78 +11,53 @@ interface IMessageSerializer {
11
11
#else
12
12
public bool TryDeserialize ( EventRecord record , [ NotNullWhen ( true ) ] out Message ? deserialized ) ;
13
13
#endif
14
- }
15
14
16
- record MessageSerializationContext (
17
- string StreamName ,
18
- ContentType ContentType
19
- ) {
20
- public string CategoryName =>
21
- StreamName . Split ( '-' ) . FirstOrDefault ( ) ?? "no_stream_category" ;
15
+ public IMessageSerializer With ( OperationSerializationSettings ? operationSettings ) ;
22
16
}
23
17
18
+ record MessageSerializationContext ( MessageTypeNamingResolutionContext NamingResolution ) ;
19
+
24
20
static class MessageSerializerExtensions {
25
21
public static MessageData [ ] Serialize (
26
22
this IMessageSerializer serializer ,
27
23
IEnumerable < Message > messages ,
28
- MessageSerializationContext context
29
- ) {
30
- return messages . Select ( m => serializer . Serialize ( m , context ) ) . ToArray ( ) ;
31
- }
32
-
33
- public static IMessageSerializer With (
34
- this IMessageSerializer defaultMessageSerializer ,
35
- KurrentDBClientSerializationSettings defaultSettings ,
36
- OperationSerializationSettings ? operationSettings
37
- ) {
38
- if ( operationSettings == null )
39
- return defaultMessageSerializer ;
40
-
41
- if ( operationSettings . AutomaticDeserialization == AutomaticDeserialization . Disabled )
42
- return NullMessageSerializer . Instance ;
43
-
44
- if ( operationSettings . ConfigureSettings == null )
45
- return defaultMessageSerializer ;
46
-
47
- var settings = defaultSettings . Clone ( ) ;
48
- operationSettings . ConfigureSettings . Invoke ( settings ) ;
49
-
50
- return new MessageSerializer ( SchemaRegistry . From ( settings ) ) ;
51
- }
24
+ MessageSerializationContext serializationContext
25
+ ) =>
26
+ messages . Select ( m => serializer . Serialize ( m , serializationContext ) ) . ToArray ( ) ;
52
27
}
53
28
54
- class MessageSerializer ( SchemaRegistry schemaRegistry ) : IMessageSerializer {
29
+ class MessageSerializer ( SchemaRegistry schemaRegistry , KurrentDBClientSerializationSettings serializationSettings )
30
+ : IMessageSerializer {
55
31
readonly SystemTextJsonSerializer _metadataSerializer =
56
32
new SystemTextJsonSerializer (
57
33
new SystemTextJsonSerializationSettings { Options = KurrentDBClient . StreamMetadataJsonSerializerOptions }
58
34
) ;
59
35
60
- readonly IMessageTypeNamingStrategy _messageTypeNamingStrategy =
61
- schemaRegistry . MessageTypeNamingStrategy ;
36
+ readonly string _contentType = serializationSettings . DefaultContentType . ToMessageContentType ( ) ;
62
37
63
38
public MessageData Serialize ( Message message , MessageSerializationContext serializationContext ) {
64
39
var ( data , metadata , messageId ) = message ;
65
40
66
- var eventType = _messageTypeNamingStrategy
41
+ var messageType = schemaRegistry
67
42
. ResolveTypeName (
68
43
message . Data . GetType ( ) ,
69
- new MessageTypeNamingResolutionContext ( serializationContext . CategoryName )
44
+ serializationContext . NamingResolution
70
45
) ;
71
46
72
47
var serializedData = schemaRegistry
73
- . GetSerializer ( serializationContext . ContentType )
48
+ . GetSerializer ( serializationSettings . DefaultContentType )
74
49
. Serialize ( data ) ;
75
50
76
51
var serializedMetadata = metadata != null
77
52
? _metadataSerializer . Serialize ( metadata )
78
53
: ReadOnlyMemory < byte > . Empty ;
79
54
80
55
return new MessageData (
81
- eventType ,
56
+ messageType ,
82
57
serializedData ,
83
58
serializedMetadata ,
84
59
messageId ,
85
- serializationContext . ContentType . ToMessageContentType ( )
60
+ _contentType
86
61
) ;
87
62
}
88
63
@@ -91,7 +66,7 @@ public bool TryDeserialize(EventRecord record, out Message? deserialized) {
91
66
#else
92
67
public bool TryDeserialize ( EventRecord record , [ NotNullWhen ( true ) ] out Message ? deserialized ) {
93
68
#endif
94
- if ( ! TryResolveClrType ( record , out var clrType ) ) {
69
+ if ( ! schemaRegistry . TryResolveClrType ( record . EventType , out var clrType ) ) {
95
70
deserialized = null ;
96
71
return false ;
97
72
}
@@ -105,29 +80,36 @@ public bool TryDeserialize(EventRecord record, [NotNullWhen(true)] out Message?
105
80
return false ;
106
81
}
107
82
108
- object ? metadata = record . Metadata . Length > 0 && TryResolveClrMetadataType ( record , out var clrMetadataType )
83
+ object ? metadata = record . Metadata . Length > 0
84
+ && schemaRegistry . TryResolveClrMetadataType ( record . EventType , out var clrMetadataType )
109
85
? _metadataSerializer . Deserialize ( record . Metadata , clrMetadataType ! )
110
86
: null ;
111
87
112
88
deserialized = Message . From ( data , metadata , record . EventId ) ;
113
89
return true ;
114
90
}
115
91
116
- public static MessageSerializer From ( KurrentDBClientSerializationSettings ? settings = null ) {
117
- settings ??= KurrentDBClientSerializationSettings . Default ( ) ;
92
+ public IMessageSerializer With ( OperationSerializationSettings ? operationSettings ) {
93
+ if ( operationSettings == null )
94
+ return this ;
95
+
96
+ if ( operationSettings . AutomaticDeserialization == AutomaticDeserialization . Disabled )
97
+ return NullMessageSerializer . Instance ;
98
+
99
+ if ( operationSettings . ConfigureSettings == null )
100
+ return this ;
118
101
119
- return new MessageSerializer ( SchemaRegistry . From ( settings ) ) ;
102
+ var settings = serializationSettings . Clone ( ) ;
103
+ operationSettings . ConfigureSettings . Invoke ( settings ) ;
104
+
105
+ return new MessageSerializer ( SchemaRegistry . From ( settings ) , settings ) ;
120
106
}
121
107
122
- bool TryResolveClrType ( EventRecord record , out Type ? clrType ) =>
123
- schemaRegistry
124
- . MessageTypeNamingStrategy
125
- . TryResolveClrType ( record . EventType , out clrType ) ;
108
+ public static MessageSerializer From ( KurrentDBClientSerializationSettings ? settings = null ) {
109
+ settings ??= KurrentDBClientSerializationSettings . Get ( ) ;
126
110
127
- bool TryResolveClrMetadataType ( EventRecord record , out Type ? clrMetadataType ) =>
128
- schemaRegistry
129
- . MessageTypeNamingStrategy
130
- . TryResolveClrMetadataType ( record . EventType , out clrMetadataType ) ;
111
+ return new MessageSerializer ( SchemaRegistry . From ( settings ) , settings ) ;
112
+ }
131
113
}
132
114
133
115
class NullMessageSerializer : IMessageSerializer {
@@ -145,4 +127,8 @@ public bool TryDeserialize(EventRecord eventRecord, [NotNullWhen(true)] out Mess
145
127
deserialized = null ;
146
128
return false ;
147
129
}
130
+
131
+ public IMessageSerializer With ( OperationSerializationSettings ? operationSettings ) {
132
+ return this ;
133
+ }
148
134
}
0 commit comments