Skip to content

Commit 7e765ee

Browse files
committed
[DEVEX-222] Added nested message type mapping
1 parent dba4342 commit 7e765ee

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

src/KurrentDB.Client/Core/KurrentDBClientSerializationSettings.cs

+59-10
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ public class KurrentDBClientSerializationSettings {
4949
/// <example>
5050
/// <code>
5151
/// var settings = KurrentDBClientSerializationSettings.Default(options => {
52-
/// options.RegisterMessageType&lt;UserCreated&gt;("user-created");
53-
/// options.RegisterMessageType&lt;UserUpdated&gt;("user-updated");
54-
/// options.RegisterMessageTypeForCategory&lt;UserCreated&gt;("user");
52+
/// options.MessageTypeMapping.Register&lt;UserRegistered&gt;("user_registered");
53+
/// options.MessageTypeMapping.Register&lt;RoleAssigned&gt;("role_assigned");
54+
/// options.MessageTypeMapping.RegisterForCategory&lt;UserRegistered&gt;("user_onboarding");
5555
/// });
5656
/// </code>
5757
/// </example>
@@ -196,7 +196,7 @@ internal KurrentDBClientSerializationSettings Clone() {
196196
BytesSerializer = BytesSerializer,
197197
JsonSerializer = JsonSerializer,
198198
DefaultContentType = DefaultContentType,
199-
MessageTypeMapping = MessageTypeMapping.Clone(),
199+
MessageTypeMapping = MessageTypeMapping.Clone(),
200200
MessageTypeNamingStrategy = MessageTypeNamingStrategy
201201
};
202202
}
@@ -264,6 +264,25 @@ public enum AutomaticDeserialization {
264264
Enabled = 1
265265
}
266266

267+
/// <summary>
268+
/// Controls whether the KurrentDB client should automatically register
269+
/// message type names based on the CLR type using naming convention.
270+
/// By default, it's enabled.
271+
/// </summary>
272+
public enum AutomaticTypeMappingRegistration {
273+
/// <summary>
274+
/// Enables automatic type registration.
275+
/// The messages will be automatically discovered and resolved using registered naming resolution strategy.
276+
/// </summary>
277+
Enabled = 0,
278+
279+
/// <summary>
280+
/// Disables automatic type registration. If you use this setting, you need to register all type mappings manually.
281+
/// If the type mapping is not registered for the specific message, the exception will be thrown.
282+
/// </summary>
283+
Disabled = 1
284+
}
285+
267286
/// <summary>
268287
/// Represents message type mapping settings
269288
/// </summary>
@@ -277,7 +296,7 @@ public class MessageTypeMappingSettings {
277296
/// Registers CLR message types that can be appended to the specific stream category.
278297
/// Types will have message type names resolved based on the used <see cref="KurrentDB.Client.Core.Serialization.IMessageTypeNamingStrategy"/>
279298
/// </summary>
280-
public IDictionary<string, Type[]> CategoryTypesMap { get; set; } = new Dictionary<string, Type[]>();
299+
internal IDictionary<string, Type[]> CategoryTypesMap { get; set; } = new Dictionary<string, Type[]>();
281300

282301
/// <summary>
283302
/// Specifies the CLR type that should be used when deserializing metadata for all events.
@@ -286,6 +305,13 @@ public class MessageTypeMappingSettings {
286305
/// </summary>
287306
public Type? DefaultMetadataType { get; set; }
288307

308+
/// <summary>
309+
/// Controls whether the KurrentDB client should automatically register
310+
/// message type names based on the CLR type using naming convention.
311+
/// By default, it's enabled.
312+
/// </summary>
313+
public AutomaticTypeMappingRegistration? AutomaticTypeMappingRegistration { get; set; }
314+
289315
/// <summary>
290316
/// Associates a message type with a specific stream category to enable automatic deserialization.
291317
/// In event sourcing, streams are often prefixed with a category (e.g., "user-123", "order-456").
@@ -297,9 +323,9 @@ public class MessageTypeMappingSettings {
297323
/// <example>
298324
/// <code>
299325
/// // Register event types that can appear in user streams
300-
/// settings.RegisterMessageTypeForCategory&lt;UserCreated&gt;("user")
301-
/// .RegisterMessageTypeForCategory&lt;UserUpdated&gt;("user")
302-
/// .RegisterMessageTypeForCategory&lt;UserDeleted&gt;("user");
326+
/// settings.RegisterForCategory&lt;UserRegistered&gt;("user")
327+
/// .RegisterForCategory&lt;RoleAssigned&gt;("user")
328+
/// .RegisterForCategory&lt;UserDeleted&gt;("user");
303329
/// </code>
304330
/// </example>
305331
public MessageTypeMappingSettings RegisterForCategory<T>(string categoryName) =>
@@ -335,8 +361,8 @@ public MessageTypeMappingSettings RegisterForCategory(string categoryName, param
335361
/// <example>
336362
/// <code>
337363
/// // Register me types with their corresponding type identifiers
338-
/// settings.RegisterMessageType&lt;UserCreated&gt;("user-created-v1")
339-
/// .RegisterMessageType&lt;OrderPlaced&gt;("order-placed-v2");
364+
/// settings.Register&lt;UserRegistered&gt;("user_registered-v1")
365+
/// .Register&lt;OrderPlaced&gt;("order-placed-v2");
340366
/// </code>
341367
/// </example>
342368
public MessageTypeMappingSettings Register<T>(string typeName) =>
@@ -387,6 +413,29 @@ public MessageTypeMappingSettings UseMetadataType(Type type) {
387413

388414
return this;
389415
}
416+
417+
/// <summary>
418+
/// Disables automatic deserialization. Messages will be returned in their raw serialized form,
419+
/// requiring manual deserialization by the application. Use this when you need direct access to the raw data
420+
/// or when working with messages that don't have registered type mappings.
421+
/// </summary>
422+
/// <returns>The current instance for method chaining.</returns>
423+
public MessageTypeMappingSettings DisableAutomaticRegistration() {
424+
AutomaticTypeMappingRegistration = Client.AutomaticTypeMappingRegistration.Disabled;
425+
426+
return this;
427+
}
428+
429+
/// <summary>
430+
/// Disables automatic type registration. If you use this setting, you need to register all type mappings manually.
431+
/// If the type mapping is not registered for the specific message, the exception will be thrown.
432+
/// </summary>
433+
/// <returns>The current instance for method chaining.</returns>
434+
public MessageTypeMappingSettings EnableAutomaticRegistration() {
435+
AutomaticTypeMappingRegistration = Client.AutomaticTypeMappingRegistration.Enabled;
436+
437+
return this;
438+
}
390439

391440
internal MessageTypeMappingSettings Clone() =>
392441
new MessageTypeMappingSettings {

0 commit comments

Comments
 (0)