diff --git a/src/documentation/articles/kafkadbcontext.md b/src/documentation/articles/kafkadbcontext.md
index 4f02d3a5..22592f71 100644
--- a/src/documentation/articles/kafkadbcontext.md
+++ b/src/documentation/articles/kafkadbcontext.md
@@ -8,8 +8,8 @@ _description: Describe what is and how use KafkaDbContext class from Entity Fram
`KafkaDbContext` is a special class which helps to define the `DbContext` and use [Entity Framework Core](https://learn.microsoft.com/it-it/ef/core/) provider for [Apache Kafka](https://kafka.apache.org/):
- `KafkaDbContext` inherits from `DbContext`: to define the model, and/or creating the database, see [getting started](https://docs.microsoft.com/ef/core/get-started/) in the docs and [KEFCore usage](usage.md)
- `KafkaDbContext` defines the following properties:
- - **KeySerializationType**: the .NET type to be used to allocate an external serializer for Apache Kafka record key
- - **ValueSerializationType**: the .NET type to be used to allocate an external serializer for Apache Kafka record value
+ - **KeySerDesSelectorType**: the .NET type to be used to allocate an external serializer for Apache Kafka record key
+ - **ValueSerDesSelectorType**: the .NET type to be used to allocate an external serializer for Apache Kafka record value
- **ValueContainerType**: the .NET type to be used to allocate an external container class for Apache Kafka record value
- **UseNameMatching**: set to **false** to avoid Entity matching based on Name
- **BootstrapServers**: the server hosting the broker of Apache Kafka
@@ -19,10 +19,11 @@ _description: Describe what is and how use KafkaDbContext class from Entity Fram
- **DefaultReplicationFactor**: the replication factor to use when data are stored in Apache Kafka
- **DefaultConsumerInstances**: the consumer instances to be allocated when UseCompactedReplicator is **true**
- **UsePersistentStorage**: set to **true** to use a persistent storage between multiple application startup
+ - **UseEnumeratorWithPrefetch**: set to **true** to prefer enumerator instances able to do a prefetch on data speeding up execution, used if **UseKNetStreams** is **true** and **UseCompactedReplicator** is **false**
+ - **UseByteBufferDataTransfer**: set to **true** to prefer data exchange in serializer instances
- **UseDeletePolicyForTopic**: set to **true** to enable [delete cleanup policy](https://kafka.apache.org/documentation/#topicconfigs_cleanup.policy)
- **UseCompactedReplicator**: Use `KNetCompactedReplicator` instead of Apache Kafka Streams to manage data to or from topics
- **UseKNetStreams**: Use KNet version of Apache Kafka Streams instead of standard Apache Kafka Streams, used if **UseCompactedReplicator** is **false**
- - **UseEnumeratorWithPrefetch**: Setting this property to **true** the engine prefers to use enumerator instances able to do a prefetch on data speeding up execution, used if **UseKNetStreams** is **true** and **UseCompactedReplicator** is **false**
- **ConsumerConfig**: parameters to use for Producer
- **ProducerConfig**: parameters to use for Producer
- **StreamsConfig**: parameters to use for Apche Kafka Streams application
diff --git a/src/documentation/articles/serialization.md b/src/documentation/articles/serialization.md
index e0aa1fb5..8dfd6a4e 100644
--- a/src/documentation/articles/serialization.md
+++ b/src/documentation/articles/serialization.md
@@ -510,7 +510,7 @@ The extension converted this schema into code to speedup the exection of seriali
### How to use Protobuf
`KafkaDbContext` contains three properties can be used to override the default types:
-- **KeySerializationType**: set this value to `ProtobufKEFCoreSerDes..Key.BinaryRaw<>` or use `ProtobufKEFCoreSerDes.DefaultKeySerialization`, the type automatically manages simple or complex Primary Key
+- **KeySerializationType**: set this value to `ProtobufKEFCoreSerDes.Key.BinaryRaw<>` or use `ProtobufKEFCoreSerDes.DefaultKeySerialization`, the type automatically manages simple or complex Primary Key
- **ValueSerializationType**: set this value to `ProtobufKEFCoreSerDes.ValueContainer.BinaryRaw<>` or use `ProtobufKEFCoreSerDes.DefaultValueContainerSerialization`
- **ValueContainerType**: set this value to `ProtobufValueContainerRaw<>` or use `ProtobufKEFCoreSerDes.DefaultValueContainer`
diff --git a/src/net/KEFCore.SerDes.Avro.Compiler/KEFCore.SerDes.Avro.Compiler.csproj b/src/net/KEFCore.SerDes.Avro.Compiler/KEFCore.SerDes.Avro.Compiler.csproj
index f0549c95..274f9e4b 100644
--- a/src/net/KEFCore.SerDes.Avro.Compiler/KEFCore.SerDes.Avro.Compiler.csproj
+++ b/src/net/KEFCore.SerDes.Avro.Compiler/KEFCore.SerDes.Avro.Compiler.csproj
@@ -23,7 +23,7 @@
-
+
diff --git a/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs b/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs
index 67393692..5b5660d9 100644
--- a/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs
+++ b/src/net/KEFCore.SerDes.Avro/AvroKEFCoreSerDes.cs
@@ -29,558 +29,730 @@
namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro;
///
-/// Avro base class to define extensions of , for example
+/// Avro base class to define extensions of , for example
///
public static class AvroKEFCoreSerDes
{
///
/// Returns the default serializer for keys
///
- public static readonly Type DefaultKeySerialization = typeof(Key.BinaryRaw<>);
+ public static readonly Type DefaultKeySerialization = typeof(Key.Binary<>);
///
/// Returns the default serializer for value containers
///
- public static readonly Type DefaultValueContainerSerialization = typeof(ValueContainer.BinaryRaw<>);
+ public static readonly Type DefaultValueContainerSerialization = typeof(ValueContainer.Binary<>);
///
/// Returns the default for value containers
///
public static readonly Type DefaultValueContainer = typeof(AvroValueContainer<>);
///
- /// Base class to define key extensions of , for example
+ /// Base class to define key extensions of , for example
///
public static class Key
{
///
- /// Avro Key Binary encoder extension of , for example based on array
+ /// Base class to define key extensions of , for example
///
- ///
- public class BinaryRaw : SerDesRaw
+ public class Binary : ISerDesSelector
{
- readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(T).FullName!);
- readonly byte[] keySerDesName = Encoding.UTF8.GetBytes(typeof(BinaryRaw<>).ToAssemblyQualified());
- readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
- readonly ISerDesRaw _defaultSerDes = default!;
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Returns a new instance of
///
- public BinaryRaw()
- {
- if (KNetSerialization.IsInternalManaged())
+ /// The of
+ public static ISerDesSelector NewInstance() => new Binary();
+ ///
+ public static string SelectorTypeName => typeof(Binary<>).ToAssemblyQualified();
+ ///
+ public static Type ByteArraySerDes => typeof(BinaryRaw);
+ ///
+ public static Type ByteBufferSerDes => typeof(BinaryBuffered);
+ ///
+ public static ISerDes NewSerDes()
+ {
+ if (typeof(TJVM) == typeof(Java.Nio.ByteBuffer)) return (ISerDes)NewByteBufferSerDes();
+ return (ISerDes)NewByteArraySerDes();
+ }
+ ///
+ public static ISerDesRaw NewByteArraySerDes() { return new BinaryRaw(SelectorTypeName); }
+ ///
+ public static ISerDesBuffered NewByteBufferSerDes() { return new BinaryBuffered(SelectorTypeName); }
+
+ ///
+ string ISerDesSelector.SelectorTypeName => SelectorTypeName;
+ ///
+ Type ISerDesSelector.ByteArraySerDes => ByteArraySerDes;
+ ///
+ Type ISerDesSelector.ByteBufferSerDes => ByteBufferSerDes;
+ ///
+ ISerDes ISerDesSelector.NewSerDes() => NewSerDes();
+ ///
+ ISerDesRaw ISerDesSelector.NewByteArraySerDes() => NewByteArraySerDes();
+ ///
+ ISerDesBuffered ISerDesSelector.NewByteBufferSerDes() => NewByteBufferSerDes();
+
+ ///
+ /// Avro Key Binary encoder extension of , for example based on array
+ ///
+ ///
+ sealed class BinaryRaw : SerDesRaw
+ {
+ readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(TData).FullName!);
+ readonly byte[] keySerDesName ;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
+ readonly ISerDesRaw _defaultSerDes = default!;
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public BinaryRaw(string selectorName)
{
- _defaultSerDes = new SerDesRaw();
+ keySerDesName = Encoding.UTF8.GetBytes(selectorName);
+ if (KNetSerialization.IsInternalManaged())
+ {
+ _defaultSerDes = new SerDesRaw();
+ }
+ else if (!typeof(TData).IsArray)
+ {
+ throw new InvalidOperationException($"{typeof(BinaryRaw<>).ToAssemblyQualified()} cannot manage {typeof(TData).Name}, override or build a new serializaer");
+ }
}
- else if (!typeof(T).IsArray)
+
+ ///
+ public override byte[] Serialize(string topic, TData data)
{
- throw new InvalidOperationException($"{typeof(BinaryRaw<>).ToAssemblyQualified()} cannot manage {typeof(T).Name}, override or build a new serializaer");
+ return SerializeWithHeaders(topic, null!, data);
}
- }
-
- ///
- public override byte[] Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
- headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
+ ///
+ public override byte[] SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
+ headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
- if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
+ if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
- using MemoryStream memStream = new();
- BinaryEncoder encoder = new(memStream);
- var container = new AvroKeyContainer();
- if (data is object[] dataArray)
+ using MemoryStream memStream = new();
+ BinaryEncoder encoder = new(memStream);
+ var container = new AvroKeyContainer();
+ if (data is object[] dataArray)
+ {
+ container.PrimaryKey = new List(dataArray);
+ }
+ else throw new InvalidDataException($"Cannot manage inputs different from object[], input is {data?.GetType()}");
+ SpecificWriter.Write(container, encoder);
+ return memStream.ToArray();
+ }
+ ///
+ public override TData Deserialize(string topic, byte[] data)
{
- container.PrimaryKey = new List(dataArray);
+ return DeserializeWithHeaders(topic, null!, data);
}
- else throw new InvalidDataException($"Cannot manage inputs different from object[], input is {data?.GetType()}");
- SpecificWriter.Write(container, encoder);
- return memStream.ToArray();
- }
- ///
- public override T Deserialize(string topic, byte[] data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
- {
- if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data)
+ {
+ if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
- using MemoryStream memStream = new(data);
- BinaryDecoder decoder = new(memStream);
- AvroKeyContainer t = new AvroKeyContainer();
- t = SpecificReader.Read(t!, decoder);
- return (T)(object)(t.PrimaryKey.ToArray());
+ using MemoryStream memStream = new(data);
+ BinaryDecoder decoder = new(memStream);
+ AvroKeyContainer t = new AvroKeyContainer();
+ t = SpecificReader.Read(t!, decoder);
+ return (TData)(object)(t.PrimaryKey.ToArray());
+ }
}
- }
- ///
- /// Avro Key Binary encoder extension of , for example based on
- ///
- ///
- public class BinaryBuffered : SerDesBuffered
- {
- readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(T).FullName!);
- readonly byte[] keySerDesName = Encoding.UTF8.GetBytes(typeof(BinaryBuffered<>).ToAssemblyQualified());
- readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
- readonly ISerDesBuffered _defaultSerDes = default!;
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Avro Key Binary encoder extension of , for example based on
///
- public BinaryBuffered()
- {
- if (KNetSerialization.IsInternalManaged())
+ ///
+ sealed class BinaryBuffered : SerDesBuffered
+ {
+ readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(TData).FullName!);
+ readonly byte[] keySerDesName;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
+ readonly ISerDesBuffered _defaultSerDes = default!;
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public BinaryBuffered(string selectorName)
{
- _defaultSerDes = new SerDesBuffered();
+ keySerDesName = Encoding.UTF8.GetBytes(selectorName);
+ if (KNetSerialization.IsInternalManaged())
+ {
+ _defaultSerDes = new SerDesBuffered();
+ }
+ else if (!typeof(TData).IsArray)
+ {
+ throw new InvalidOperationException($"{typeof(BinaryBuffered<>).ToAssemblyQualified()} cannot manage {typeof(TData).Name}, override or build a new serializaer");
+ }
}
- else if (!typeof(T).IsArray)
+
+ ///
+ public override ByteBuffer Serialize(string topic, TData data)
{
- throw new InvalidOperationException($"{typeof(BinaryBuffered<>).ToAssemblyQualified()} cannot manage {typeof(T).Name}, override or build a new serializaer");
+ return SerializeWithHeaders(topic, null!, data);
}
- }
-
- ///
- public override ByteBuffer Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
- headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
+ ///
+ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
+ headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
- if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
+ if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
- MemoryStream memStream = new();
- BinaryEncoder encoder = new(memStream);
- var container = new AvroKeyContainer();
- if (data is object[] dataArray)
+ MemoryStream memStream = new();
+ BinaryEncoder encoder = new(memStream);
+ var container = new AvroKeyContainer();
+ if (data is object[] dataArray)
+ {
+ container.PrimaryKey = new List(dataArray);
+ }
+ else throw new InvalidDataException($"Cannot manage inputs different from object[], input is {data?.GetType()}");
+ SpecificWriter.Write(container, encoder);
+ return ByteBuffer.From(memStream);
+ }
+ ///
+ public override TData Deserialize(string topic, ByteBuffer data)
{
- container.PrimaryKey = new List(dataArray);
+ return DeserializeWithHeaders(topic, null!, data);
}
- else throw new InvalidDataException($"Cannot manage inputs different from object[], input is {data?.GetType()}");
- SpecificWriter.Write(container, encoder);
- return ByteBuffer.From(memStream);
- }
- ///
- public override T Deserialize(string topic, ByteBuffer data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
- {
- if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
+ {
+ if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
- BinaryDecoder decoder = new(data);
- AvroKeyContainer t = new AvroKeyContainer();
- t = SpecificReader.Read(t!, decoder);
- return (T)(object)(t.PrimaryKey.ToArray());
+ BinaryDecoder decoder = new(data);
+ AvroKeyContainer t = new AvroKeyContainer();
+ t = SpecificReader.Read(t!, decoder);
+ return (TData)(object)(t.PrimaryKey.ToArray());
+ }
}
}
///
- /// Avro Key Json encoder extension of , for example based on array
+ /// Base class to define key extensions of , for example
///
- ///
- public class JsonRaw : SerDesRaw
+ public class Json : ISerDesSelector
{
- readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(T).FullName!);
- readonly byte[] keySerDesName = Encoding.UTF8.GetBytes(typeof(JsonRaw<>).ToAssemblyQualified());
- readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
- readonly ISerDesRaw _defaultSerDes = default!;
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Returns a new instance of
///
- public JsonRaw()
- {
- if (KNetSerialization.IsInternalManaged())
+ /// The of
+ public static ISerDesSelector NewInstance() => new Json();
+ ///
+ public static string SelectorTypeName => typeof(Json<>).ToAssemblyQualified();
+ ///
+ public static Type ByteArraySerDes => typeof(JsonRaw);
+ ///
+ public static Type ByteBufferSerDes => typeof(JsonBuffered);
+ ///
+ public static ISerDes NewSerDes()
+ {
+ if (typeof(TJVM) == typeof(Java.Nio.ByteBuffer)) return (ISerDes)NewByteBufferSerDes();
+ return (ISerDes)NewByteArraySerDes();
+ }
+ ///
+ public static ISerDesRaw NewByteArraySerDes() { return new JsonRaw(SelectorTypeName); }
+ ///
+ public static ISerDesBuffered NewByteBufferSerDes() { return new JsonBuffered(SelectorTypeName); }
+
+ ///
+ string ISerDesSelector.SelectorTypeName => SelectorTypeName;
+ ///
+ Type ISerDesSelector.ByteArraySerDes => ByteArraySerDes;
+ ///
+ Type ISerDesSelector.ByteBufferSerDes => ByteBufferSerDes;
+ ///
+ ISerDes ISerDesSelector.NewSerDes() => NewSerDes();
+ ///
+ ISerDesRaw ISerDesSelector.NewByteArraySerDes() => NewByteArraySerDes();
+ ///
+ ISerDesBuffered ISerDesSelector.NewByteBufferSerDes() => NewByteBufferSerDes();
+
+ ///
+ /// Avro Key Json encoder extension of , for example based on array
+ ///
+ ///
+ sealed class JsonRaw : SerDesRaw
+ {
+ readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(TData).FullName!);
+ readonly byte[] keySerDesName;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
+ readonly ISerDesRaw _defaultSerDes = default!;
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public JsonRaw(string selectorName)
{
- _defaultSerDes = new SerDesRaw();
+ keySerDesName = Encoding.UTF8.GetBytes(selectorName);
+ if (KNetSerialization.IsInternalManaged())
+ {
+ _defaultSerDes = new SerDesRaw();
+ }
+ else if (!typeof(TData).IsArray)
+ {
+ throw new InvalidOperationException($"{typeof(JsonRaw<>).ToAssemblyQualified()} cannot manage {typeof(TData).Name}, override or build a new serializaer");
+ }
}
- else if (!typeof(T).IsArray)
+
+ ///
+ public override byte[] Serialize(string topic, TData data)
{
- throw new InvalidOperationException($"{typeof(JsonRaw<>).ToAssemblyQualified()} cannot manage {typeof(T).Name}, override or build a new serializaer");
+ return SerializeWithHeaders(topic, null!, data);
}
- }
-
- ///
- public override byte[] Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
- headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
+ ///
+ public override byte[] SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
+ headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
- if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
+ if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
- using MemoryStream memStream = new();
- JsonEncoder encoder = new(AvroKeyContainer._SCHEMA, memStream);
- SpecificWriter.Write(data, encoder);
- encoder.Flush();
- return memStream.ToArray();
- }
- ///
- public override T Deserialize(string topic, byte[] data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
- {
- if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
+ using MemoryStream memStream = new();
+ JsonEncoder encoder = new(AvroKeyContainer._SCHEMA, memStream);
+ SpecificWriter.Write(data, encoder);
+ encoder.Flush();
+ return memStream.ToArray();
+ }
+ ///
+ public override TData Deserialize(string topic, byte[] data)
+ {
+ return DeserializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data)
+ {
+ if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
- using MemoryStream memStream = new(data);
- JsonDecoder decoder = new(AvroKeyContainer._SCHEMA, memStream);
- T t = (T)Activator.CreateInstance(typeof(T))!;
- t = SpecificReader.Read(t!, decoder);
- return t;
+ using MemoryStream memStream = new(data);
+ JsonDecoder decoder = new(AvroKeyContainer._SCHEMA, memStream);
+ TData t = (TData)Activator.CreateInstance(typeof(TData))!;
+ t = SpecificReader.Read(t!, decoder);
+ return t;
+ }
}
- }
- ///
- /// Avro Key Json encoder extension of , for example based on
- ///
- ///
- public class JsonBuffered : SerDesBuffered
- {
- readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(T).FullName!);
- readonly byte[] keySerDesName = Encoding.UTF8.GetBytes(typeof(JsonBuffered<>).ToAssemblyQualified());
- readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
- readonly ISerDesBuffered _defaultSerDes = default!;
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Avro Key Json encoder extension of , for example based on
///
- public JsonBuffered()
- {
- if (KNetSerialization.IsInternalManaged())
+ ///
+ sealed class JsonBuffered : SerDesBuffered
+ {
+ readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(TData).FullName!);
+ readonly byte[] keySerDesName;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroKeyContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroKeyContainer._SCHEMA, AvroKeyContainer._SCHEMA);
+ readonly ISerDesBuffered _defaultSerDes = default!;
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public JsonBuffered(string selectorName)
{
- _defaultSerDes = new SerDesBuffered();
+ keySerDesName = Encoding.UTF8.GetBytes(selectorName);
+ if (KNetSerialization.IsInternalManaged())
+ {
+ _defaultSerDes = new SerDesBuffered();
+ }
+ else if (!typeof(TData).IsArray)
+ {
+ throw new InvalidOperationException($"{typeof(JsonBuffered<>).ToAssemblyQualified()} cannot manage {typeof(TData).Name}, override or build a new serializaer");
+ }
}
- else if (!typeof(T).IsArray)
+
+ ///
+ public override ByteBuffer Serialize(string topic, TData data)
{
- throw new InvalidOperationException($"{typeof(JsonBuffered<>).ToAssemblyQualified()} cannot manage {typeof(T).Name}, override or build a new serializaer");
+ return SerializeWithHeaders(topic, null!, data);
}
- }
-
- ///
- public override ByteBuffer Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
- headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
+ ///
+ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
+ headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
- if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
+ if (_defaultSerDes != null) return _defaultSerDes.SerializeWithHeaders(topic, headers, data);
- MemoryStream memStream = new();
- JsonEncoder encoder = new(AvroKeyContainer._SCHEMA, memStream);
- SpecificWriter.Write(data, encoder);
- encoder.Flush();
- return ByteBuffer.From(memStream);
- }
- ///
- public override T Deserialize(string topic, ByteBuffer data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
- {
- if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
+ MemoryStream memStream = new();
+ JsonEncoder encoder = new(AvroKeyContainer._SCHEMA, memStream);
+ SpecificWriter.Write(data, encoder);
+ encoder.Flush();
+ return ByteBuffer.From(memStream);
+ }
+ ///
+ public override TData Deserialize(string topic, ByteBuffer data)
+ {
+ return DeserializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
+ {
+ if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
- JsonDecoder decoder = new(AvroKeyContainer._SCHEMA, data);
- T t = (T)Activator.CreateInstance(typeof(T))!;
- t = SpecificReader.Read(t!, decoder);
- return t;
+ JsonDecoder decoder = new(AvroKeyContainer._SCHEMA, data);
+ TData t = (TData)Activator.CreateInstance(typeof(TData))!;
+ t = SpecificReader.Read(t!, decoder);
+ return t;
+ }
}
}
}
///
- /// Base class to define ValueContainer extensions of , for example
+ /// Base class to define ValueContainer extensions of , for example
///
public static class ValueContainer
{
///
- /// Avro ValueContainer Binary encoder extension of , for example based on array
+ /// Base class to define key extensions of , for example
///
- ///
- public class BinaryRaw : SerDesRaw
+ public class Binary : ISerDesSelector
{
- readonly byte[] valueContainerSerDesName = Encoding.UTF8.GetBytes(typeof(BinaryRaw<>).ToAssemblyQualified());
- readonly byte[] valueContainerName = null!;
- readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Returns a new instance of
///
- public BinaryRaw()
- {
- var tt = typeof(T);
- if (tt.IsGenericType)
+ /// The of
+ public static ISerDesSelector NewInstance() => new Binary();
+ ///
+ public static string SelectorTypeName => typeof(Binary<>).ToAssemblyQualified();
+ ///
+ public static Type ByteArraySerDes => typeof(BinaryRaw);
+ ///
+ public static Type ByteBufferSerDes => typeof(BinaryBuffered);
+ ///
+ public static ISerDes NewSerDes()
+ {
+ if (typeof(TJVM) == typeof(Java.Nio.ByteBuffer)) return (ISerDes)NewByteBufferSerDes();
+ return (ISerDes)NewByteArraySerDes();
+ }
+ ///
+ public static ISerDesRaw NewByteArraySerDes() { return new BinaryRaw(SelectorTypeName); }
+ ///
+ public static ISerDesBuffered NewByteBufferSerDes() { return new BinaryBuffered(SelectorTypeName); }
+
+ ///
+ string ISerDesSelector.SelectorTypeName => SelectorTypeName;
+ ///
+ Type ISerDesSelector.ByteArraySerDes => ByteArraySerDes;
+ ///
+ Type ISerDesSelector.ByteBufferSerDes => ByteBufferSerDes;
+ ///
+ ISerDes ISerDesSelector.NewSerDes() => NewSerDes();
+ ///
+ ISerDesRaw ISerDesSelector.NewByteArraySerDes() => NewByteArraySerDes();
+ ///
+ ISerDesBuffered ISerDesSelector.NewByteBufferSerDes() => NewByteBufferSerDes();
+
+ ///
+ /// Avro ValueContainer Binary encoder extension of , for example based on array
+ ///
+ ///
+ sealed class BinaryRaw : SerDesRaw
+ {
+ readonly byte[] valueContainerSerDesName;
+ readonly byte[] valueContainerName = null!;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public BinaryRaw(string selectorName)
{
- var keyT = tt.GetGenericArguments();
- if (keyT.Length != 1) { throw new ArgumentException($"{typeof(T).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
- var t = tt.GetGenericTypeDefinition();
- if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ valueContainerSerDesName = Encoding.UTF8.GetBytes(selectorName);
+ var tt = typeof(TData);
+ if (tt.IsGenericType)
{
- valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
- return;
+ var keyT = tt.GetGenericArguments();
+ if (keyT.Length != 1) { throw new ArgumentException($"{typeof(TData).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
+ var t = tt.GetGenericTypeDefinition();
+ if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ {
+ valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
+ return;
+ }
+ else throw new ArgumentException($"{typeof(TData).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
}
- else throw new ArgumentException($"{typeof(T).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
+ throw new ArgumentException($"{typeof(TData).Name} is not a generic type and cannot be used as a valid ValueContainer type");
}
- throw new ArgumentException($"{typeof(T).Name} is not a generic type and cannot be used as a valid ValueContainer type");
- }
- ///
- public override byte[] Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
- headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
+ ///
+ public override byte[] Serialize(string topic, TData data)
+ {
+ return SerializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override byte[] SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
+ headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
- using MemoryStream memStream = new();
- BinaryEncoder encoder = new(memStream);
- SpecificWriter.Write(data, encoder);
- return memStream.ToArray();
- }
- ///
- public override T Deserialize(string topic, byte[] data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
- {
- using MemoryStream memStream = new(data);
- BinaryDecoder decoder = new(memStream);
- T t = (T)Activator.CreateInstance(typeof(T))!;
- t = SpecificReader.Read(t!, decoder);
- return t;
+ using MemoryStream memStream = new();
+ BinaryEncoder encoder = new(memStream);
+ SpecificWriter.Write(data, encoder);
+ return memStream.ToArray();
+ }
+ ///
+ public override TData Deserialize(string topic, byte[] data)
+ {
+ return DeserializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data)
+ {
+ using MemoryStream memStream = new(data);
+ BinaryDecoder decoder = new(memStream);
+ TData t = (TData)Activator.CreateInstance(typeof(TData))!;
+ t = SpecificReader.Read(t!, decoder);
+ return t;
+ }
}
- }
- ///
- /// Avro ValueContainer Binary encoder extension of , for example based on
- ///
- ///
- public class BinaryBuffered : SerDesBuffered
- {
- readonly byte[] valueContainerSerDesName = Encoding.UTF8.GetBytes(typeof(BinaryBuffered<>).ToAssemblyQualified());
- readonly byte[] valueContainerName = null!;
- readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Avro ValueContainer Binary encoder extension of , for example based on
///
- public BinaryBuffered()
- {
- var tt = typeof(T);
- if (tt.IsGenericType)
+ ///
+ sealed class BinaryBuffered : SerDesBuffered
+ {
+ readonly byte[] valueContainerSerDesName;
+ readonly byte[] valueContainerName = null!;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public BinaryBuffered(string selectorName)
{
- var keyT = tt.GetGenericArguments();
- if (keyT.Length != 1) { throw new ArgumentException($"{typeof(T).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
- var t = tt.GetGenericTypeDefinition();
- if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ valueContainerSerDesName = Encoding.UTF8.GetBytes(selectorName);
+ var tt = typeof(TData);
+ if (tt.IsGenericType)
{
- valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
- return;
+ var keyT = tt.GetGenericArguments();
+ if (keyT.Length != 1) { throw new ArgumentException($"{typeof(TData).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
+ var t = tt.GetGenericTypeDefinition();
+ if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ {
+ valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
+ return;
+ }
+ else throw new ArgumentException($"{typeof(TData).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
}
- else throw new ArgumentException($"{typeof(T).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
+ throw new ArgumentException($"{typeof(TData).Name} is not a generic type and cannot be used as a valid ValueContainer type");
}
- throw new ArgumentException($"{typeof(T).Name} is not a generic type and cannot be used as a valid ValueContainer type");
- }
- ///
- public override ByteBuffer Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
- headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
+ ///
+ public override ByteBuffer Serialize(string topic, TData data)
+ {
+ return SerializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
+ headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
- MemoryStream memStream = new();
- BinaryEncoder encoder = new(memStream);
- SpecificWriter.Write(data, encoder);
- return ByteBuffer.From(memStream);
- }
- ///
- public override T Deserialize(string topic, ByteBuffer data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
- {
- BinaryDecoder decoder = new(data);
- T t = (T)Activator.CreateInstance(typeof(T))!;
- t = SpecificReader.Read(t!, decoder);
- return t;
+ MemoryStream memStream = new();
+ BinaryEncoder encoder = new(memStream);
+ SpecificWriter.Write(data, encoder);
+ return ByteBuffer.From(memStream);
+ }
+ ///
+ public override TData Deserialize(string topic, ByteBuffer data)
+ {
+ return DeserializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
+ {
+ BinaryDecoder decoder = new(data);
+ TData t = (TData)Activator.CreateInstance(typeof(TData))!;
+ t = SpecificReader.Read(t!, decoder);
+ return t;
+ }
}
}
///
- /// Avro ValueContainer Json encoder extension of , for example based on array
+ /// Base class to define key extensions of , for example
///
- ///
- public class JsonRaw : SerDesRaw
+ public class Json : ISerDesSelector
{
- readonly byte[] valueContainerSerDesName = Encoding.UTF8.GetBytes(typeof(JsonRaw<>).ToAssemblyQualified());
- readonly byte[] valueContainerName = null!;
- readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Returns a new instance of
///
- public JsonRaw()
- {
- var tt = typeof(T);
- if (tt.IsGenericType)
+ /// The of
+ public static ISerDesSelector NewInstance() => new Json();
+ ///
+ public static string SelectorTypeName => typeof(Json<>).ToAssemblyQualified();
+ ///
+ public static Type ByteArraySerDes => typeof(JsonRaw);
+ ///
+ public static Type ByteBufferSerDes => typeof(JsonBuffered);
+ ///
+ public static ISerDes NewSerDes()
+ {
+ if (typeof(TJVM) == typeof(Java.Nio.ByteBuffer)) return (ISerDes)NewByteBufferSerDes();
+ return (ISerDes)NewByteArraySerDes();
+ }
+ ///
+ public static ISerDesRaw NewByteArraySerDes() { return new JsonRaw(SelectorTypeName); }
+ ///
+ public static ISerDesBuffered NewByteBufferSerDes() { return new JsonBuffered(SelectorTypeName); }
+
+ ///
+ string ISerDesSelector.SelectorTypeName => SelectorTypeName;
+ ///
+ Type ISerDesSelector.ByteArraySerDes => ByteArraySerDes;
+ ///
+ Type ISerDesSelector.ByteBufferSerDes => ByteBufferSerDes;
+ ///
+ ISerDes ISerDesSelector.NewSerDes() => NewSerDes();
+ ///
+ ISerDesRaw ISerDesSelector.NewByteArraySerDes() => NewByteArraySerDes();
+ ///
+ ISerDesBuffered ISerDesSelector.NewByteBufferSerDes() => NewByteBufferSerDes();
+
+ ///
+ /// Avro ValueContainer Json encoder extension of , for example based on array
+ ///
+ ///
+ sealed class JsonRaw : SerDesRaw
+ {
+ readonly byte[] valueContainerSerDesName;
+ readonly byte[] valueContainerName = null!;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public JsonRaw(string selectorName)
{
- var keyT = tt.GetGenericArguments();
- if (keyT.Length != 1) { throw new ArgumentException($"{typeof(T).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
- var t = tt.GetGenericTypeDefinition();
- if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ valueContainerSerDesName = Encoding.UTF8.GetBytes(selectorName);
+ var tt = typeof(TData);
+ if (tt.IsGenericType)
{
- valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
- return;
+ var keyT = tt.GetGenericArguments();
+ if (keyT.Length != 1) { throw new ArgumentException($"{typeof(TData).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
+ var t = tt.GetGenericTypeDefinition();
+ if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ {
+ valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
+ return;
+ }
+ else throw new ArgumentException($"{typeof(TData).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
}
- else throw new ArgumentException($"{typeof(T).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
+ throw new ArgumentException($"{typeof(TData).Name} is not a generic type and cannot be used as a valid ValueContainer type");
}
- throw new ArgumentException($"{typeof(T).Name} is not a generic type and cannot be used as a valid ValueContainer type");
- }
- ///
- public override byte[] Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
- headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
-
- using MemoryStream memStream = new();
- JsonEncoder encoder = new(AvroValueContainer._SCHEMA, memStream);
- SpecificWriter.Write(data, encoder);
- encoder.Flush();
- return memStream.ToArray();
- }
- ///
- public override T Deserialize(string topic, byte[] data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
- {
- using MemoryStream memStream = new(data);
- JsonDecoder decoder = new(AvroValueContainer._SCHEMA, memStream);
- T t = (T)Activator.CreateInstance(typeof(T))!;
- t = SpecificReader.Read(t!, decoder);
- return t;
+ ///
+ public override byte[] Serialize(string topic, TData data)
+ {
+ return SerializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override byte[] SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
+ headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
+
+ using MemoryStream memStream = new();
+ JsonEncoder encoder = new(AvroValueContainer._SCHEMA, memStream);
+ SpecificWriter.Write(data, encoder);
+ encoder.Flush();
+ return memStream.ToArray();
+ }
+ ///
+ public override TData Deserialize(string topic, byte[] data)
+ {
+ return DeserializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data)
+ {
+ using MemoryStream memStream = new(data);
+ JsonDecoder decoder = new(AvroValueContainer._SCHEMA, memStream);
+ TData t = (TData)Activator.CreateInstance(typeof(TData))!;
+ t = SpecificReader.Read(t!, decoder);
+ return t;
+ }
}
- }
- ///
- /// Avro ValueContainer Json encoder extension of , for example based on
- ///
- ///
- public class JsonBuffered : SerDesBuffered
- {
- readonly byte[] valueContainerSerDesName = Encoding.UTF8.GetBytes(typeof(JsonBuffered<>).ToAssemblyQualified());
- readonly byte[] valueContainerName = null!;
- readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
- readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
- ///
- public override bool UseHeaders => true;
///
- /// Default initializer
+ /// Avro ValueContainer Json encoder extension of , for example based on
///
- public JsonBuffered()
- {
- var tt = typeof(T);
- if (tt.IsGenericType)
+ ///
+ sealed class JsonBuffered : SerDesBuffered
+ {
+ readonly byte[] valueContainerSerDesName;
+ readonly byte[] valueContainerName = null!;
+ readonly SpecificDefaultWriter SpecificWriter = new(AvroValueContainer._SCHEMA);
+ readonly SpecificDefaultReader SpecificReader = new(AvroValueContainer._SCHEMA, AvroValueContainer._SCHEMA);
+ ///
+ public override bool UseHeaders => true;
+ ///
+ /// Default initializer
+ ///
+ public JsonBuffered(string selectorName)
{
- var keyT = tt.GetGenericArguments();
- if (keyT.Length != 1) { throw new ArgumentException($"{typeof(T).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
- var t = tt.GetGenericTypeDefinition();
- if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ valueContainerSerDesName = Encoding.UTF8.GetBytes(selectorName);
+ var tt = typeof(TData);
+ if (tt.IsGenericType)
{
- valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
- return;
+ var keyT = tt.GetGenericArguments();
+ if (keyT.Length != 1) { throw new ArgumentException($"{typeof(TData).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
+ var t = tt.GetGenericTypeDefinition();
+ if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
+ {
+ valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
+ return;
+ }
+ else throw new ArgumentException($"{typeof(TData).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
}
- else throw new ArgumentException($"{typeof(T).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
+ throw new ArgumentException($"{typeof(TData).Name} is not a generic type and cannot be used as a valid ValueContainer type");
}
- throw new ArgumentException($"{typeof(T).Name} is not a generic type and cannot be used as a valid ValueContainer type");
- }
- ///
- public override ByteBuffer Serialize(string topic, T data)
- {
- return SerializeWithHeaders(topic, null!, data);
- }
- ///
- public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T data)
- {
- headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
- headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
-
- MemoryStream memStream = new();
- JsonEncoder encoder = new(AvroValueContainer._SCHEMA, memStream);
- SpecificWriter.Write(data, encoder);
- encoder.Flush();
- return ByteBuffer.From(memStream);
- }
- ///
- public override T Deserialize(string topic, ByteBuffer data)
- {
- return DeserializeWithHeaders(topic, null!, data);
- }
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
- {
- JsonDecoder decoder = new(AvroValueContainer._SCHEMA, data);
- T t = (T)Activator.CreateInstance(typeof(T))!;
- t = SpecificReader.Read(t!, decoder);
- return t;
+ ///
+ public override ByteBuffer Serialize(string topic, TData data)
+ {
+ return SerializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, TData data)
+ {
+ headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
+ headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
+
+ MemoryStream memStream = new();
+ JsonEncoder encoder = new(AvroValueContainer._SCHEMA, memStream);
+ SpecificWriter.Write(data, encoder);
+ encoder.Flush();
+ return ByteBuffer.From(memStream);
+ }
+ ///
+ public override TData Deserialize(string topic, ByteBuffer data)
+ {
+ return DeserializeWithHeaders(topic, null!, data);
+ }
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
+ {
+ JsonDecoder decoder = new(AvroValueContainer._SCHEMA, data);
+ TData t = (TData)Activator.CreateInstance(typeof(TData))!;
+ t = SpecificReader.Read(t!, decoder);
+ return t;
+ }
}
}
}
diff --git a/src/net/KEFCore.SerDes.Avro/AvroValueContainer.cs b/src/net/KEFCore.SerDes.Avro/AvroValueContainer.cs
index df43da7c..2fb6baed 100644
--- a/src/net/KEFCore.SerDes.Avro/AvroValueContainer.cs
+++ b/src/net/KEFCore.SerDes.Avro/AvroValueContainer.cs
@@ -21,6 +21,7 @@
#nullable enable
using Avro;
+using MASES.KNet.Serialization;
namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage;
@@ -44,7 +45,7 @@ public partial class AvroValueContainer : AvroValueContainer, IValueContai
public AvroValueContainer(IEntityType tName, object[] rData)
{
EntityName = tName.Name;
- ClrType = tName.ClrType.FullName!;
+ ClrType = tName.ClrType?.ToAssemblyQualified()!;
Data = new List();
foreach (var item in tName.GetProperties())
{
@@ -53,7 +54,7 @@ public AvroValueContainer(IEntityType tName, object[] rData)
{
PropertyIndex = index,
PropertyName = item.Name,
- ClrType = item.ClrType?.FullName,
+ ClrType = item.ClrType?.ToAssemblyQualified(),
Value = rData[index]
};
Data.Add(pRecord);
diff --git a/src/net/KEFCore.SerDes.Protobuf/Generated/GenericValue.cs b/src/net/KEFCore.SerDes.Protobuf/Generated/GenericValue.cs
index eb526c6f..b8e99fd4 100644
--- a/src/net/KEFCore.SerDes.Protobuf/Generated/GenericValue.cs
+++ b/src/net/KEFCore.SerDes.Protobuf/Generated/GenericValue.cs
@@ -772,7 +772,11 @@ public void MergeFrom(pb::CodedInputStream input) {
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
@@ -846,7 +850,11 @@ public void MergeFrom(pb::CodedInputStream input) {
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
break;
diff --git a/src/net/KEFCore.SerDes.Protobuf/Generated/KeyContainer.cs b/src/net/KEFCore.SerDes.Protobuf/Generated/KeyContainer.cs
index 39fca702..2082af44 100644
--- a/src/net/KEFCore.SerDes.Protobuf/Generated/KeyContainer.cs
+++ b/src/net/KEFCore.SerDes.Protobuf/Generated/KeyContainer.cs
@@ -190,7 +190,11 @@ public void MergeFrom(pb::CodedInputStream input) {
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
@@ -209,7 +213,11 @@ public void MergeFrom(pb::CodedInputStream input) {
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
break;
@@ -386,7 +394,11 @@ public void MergeFrom(pb::CodedInputStream input) {
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
@@ -408,7 +420,11 @@ public void MergeFrom(pb::CodedInputStream input) {
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
break;
diff --git a/src/net/KEFCore.SerDes.Protobuf/Generated/ValueContainer.cs b/src/net/KEFCore.SerDes.Protobuf/Generated/ValueContainer.cs
index 48698379..a4d70f89 100644
--- a/src/net/KEFCore.SerDes.Protobuf/Generated/ValueContainer.cs
+++ b/src/net/KEFCore.SerDes.Protobuf/Generated/ValueContainer.cs
@@ -294,7 +294,11 @@ public void MergeFrom(pb::CodedInputStream input) {
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
@@ -328,7 +332,11 @@ public void MergeFrom(pb::CodedInputStream input) {
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
break;
@@ -564,7 +572,11 @@ public void MergeFrom(pb::CodedInputStream input) {
#else
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
@@ -591,7 +603,11 @@ public void MergeFrom(pb::CodedInputStream input) {
void pb::IBufferMessage.InternalMergeFrom(ref pb::ParseContext input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
- switch(tag) {
+ if ((tag & 7) == 4) {
+ // Abort on any end group tag.
+ return;
+ }
+ switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, ref input);
break;
diff --git a/src/net/KEFCore.SerDes.Protobuf/KEFCore.SerDes.Protobuf.csproj b/src/net/KEFCore.SerDes.Protobuf/KEFCore.SerDes.Protobuf.csproj
index fcbfa0b1..dd17575a 100644
--- a/src/net/KEFCore.SerDes.Protobuf/KEFCore.SerDes.Protobuf.csproj
+++ b/src/net/KEFCore.SerDes.Protobuf/KEFCore.SerDes.Protobuf.csproj
@@ -42,8 +42,8 @@
-
-
+
+
all
runtime; build; native; contentfiles; analyzers; buildtransitive
diff --git a/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs b/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs
index 414c41fa..103dd90c 100644
--- a/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs
+++ b/src/net/KEFCore.SerDes.Protobuf/ProtobufKEFCoreSerDes.cs
@@ -28,60 +28,95 @@
namespace MASES.EntityFrameworkCore.KNet.Serialization.Protobuf;
///
-/// Protobuf base class to define extensions of , for example
+/// Protobuf base class to define extensions of , for example
///
public static class ProtobufKEFCoreSerDes
{
///
/// Returns the default serializer for keys
///
- public static readonly Type DefaultKeySerialization = typeof(Key.BinaryRaw<>);
+ public static readonly Type DefaultKeySerialization = typeof(Key<>);
///
/// Returns the default serializer for value containers
///
- public static readonly Type DefaultValueContainerSerialization = typeof(ValueContainer.BinaryRaw<>);
+ public static readonly Type DefaultValueContainerSerialization = typeof(ValueContainer<>);
///
/// Returns the default for value containers
///
public static readonly Type DefaultValueContainer = typeof(ProtobufValueContainer<>);
///
- /// Base class to define key extensions of , for example based on array
+ /// Base class to define key extensions of , for example
///
- public static class Key
+ public class Key : ISerDesSelector where T : class, IMessage
{
///
- /// Protobuf Key Binary encoder extension of , for example
+ /// Returns a new instance of
///
- ///
- public class BinaryRaw : SerDesRaw
+ /// The of
+ public static ISerDesSelector NewInstance() => new Key();
+ ///
+ public static string SelectorTypeName => typeof(Key<>).ToAssemblyQualified();
+ ///
+ public static Type ByteArraySerDes => typeof(BinaryRaw);
+ ///
+ public static Type ByteBufferSerDes => typeof(BinaryBuffered);
+ ///
+ public static ISerDes NewSerDes()
{
- readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(T).FullName!);
- readonly byte[] keySerDesName = Encoding.UTF8.GetBytes(typeof(BinaryRaw<>).ToAssemblyQualified());
- readonly ISerDesRaw _defaultSerDes = default!;
+ if (typeof(TJVM) == typeof(Java.Nio.ByteBuffer)) return (ISerDes)NewByteBufferSerDes();
+ return (ISerDes)NewByteArraySerDes();
+ }
+ ///
+ public static ISerDesRaw NewByteArraySerDes() { return new BinaryRaw(SelectorTypeName); }
+ ///
+ public static ISerDesBuffered NewByteBufferSerDes() { return new BinaryBuffered(SelectorTypeName); }
+
+ ///
+ string ISerDesSelector.SelectorTypeName => SelectorTypeName;
+ ///
+ Type ISerDesSelector.ByteArraySerDes => ByteArraySerDes;
+ ///
+ Type ISerDesSelector.ByteBufferSerDes => ByteBufferSerDes;
+ ///
+ ISerDes ISerDesSelector.NewSerDes() => NewSerDes();
+ ///
+ ISerDesRaw ISerDesSelector.NewByteArraySerDes() => NewByteArraySerDes();
+ ///
+ ISerDesBuffered ISerDesSelector.NewByteBufferSerDes() => NewByteBufferSerDes();
+ ///
+ /// Protobuf Key Binary encoder extension of , for example
+ ///
+ ///
+ sealed class BinaryRaw : SerDesRaw
+ {
+ readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(TData).FullName!);
+ readonly byte[] keySerDesName;
+ readonly ISerDesRaw _defaultSerDes = default!;
///
public override bool UseHeaders => true;
///
/// Default initializer
///
- public BinaryRaw()
+ public BinaryRaw(string selectorName)
{
- if (KNetSerialization.IsInternalManaged())
+ keySerDesName = Encoding.UTF8.GetBytes(selectorName);
+ if (KNetSerialization.IsInternalManaged())
{
- _defaultSerDes = new SerDesRaw();
+ _defaultSerDes = new SerDesRaw();
}
- else if (!typeof(T).IsArray)
+ else if (!typeof(TData).IsArray)
{
- throw new InvalidOperationException($"{typeof(BinaryRaw<>).ToAssemblyQualified()} cannot manage {typeof(T).Name}, override or build a new serializaer");
+ throw new InvalidOperationException($"{typeof(BinaryRaw<>).ToAssemblyQualified()} cannot manage {typeof(TData).Name}, override or build a new serializaer");
}
}
- ///
- public override byte[] Serialize(string topic, T data)
+ ///
+ public override byte[] Serialize(string topic, TData data)
{
return SerializeWithHeaders(topic, null!, data);
}
- ///
- public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
+ ///
+ public override byte[] SerializeWithHeaders(string topic, Headers headers, TData data)
{
headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
@@ -97,13 +132,13 @@ public override byte[] SerializeWithHeaders(string topic, Headers headers, T dat
keyContainer.WriteTo(stream);
return stream.ToArray();
}
- ///
- public override T Deserialize(string topic, byte[] data)
+ ///
+ public override TData Deserialize(string topic, byte[] data)
{
return DeserializeWithHeaders(topic, null!, data);
}
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data)
{
if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
@@ -111,43 +146,44 @@ public override T DeserializeWithHeaders(string topic, Headers headers, byte[] d
KeyContainer container = KeyContainer.Parser.ParseFrom(data);
- return (T)container.GetContent();
+ return (TData)container.GetContent();
}
}
///
- /// Protobuf Key Binary encoder extension of , for example based on
+ /// Protobuf Key Binary encoder extension of , for example based on
///
- ///
- public class BinaryBuffered : SerDesBuffered
+ ///
+ sealed class BinaryBuffered : SerDesBuffered
{
- readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(T).FullName!);
- readonly byte[] keySerDesName = Encoding.UTF8.GetBytes(typeof(BinaryBuffered<>).ToAssemblyQualified());
- readonly ISerDesBuffered _defaultSerDes = default!;
+ readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(TData).FullName!);
+ readonly byte[] keySerDesName;
+ readonly ISerDesBuffered _defaultSerDes = default!;
///
public override bool UseHeaders => true;
///
/// Default initializer
///
- public BinaryBuffered()
+ public BinaryBuffered(string selectorName)
{
- if (KNetSerialization.IsInternalManaged())
+ keySerDesName = Encoding.UTF8.GetBytes(selectorName);
+ if (KNetSerialization.IsInternalManaged())
{
- _defaultSerDes = new SerDesBuffered();
+ _defaultSerDes = new SerDesBuffered();
}
- else if (!typeof(T).IsArray)
+ else if (!typeof(TData).IsArray)
{
- throw new InvalidOperationException($"{typeof(BinaryBuffered<>).ToAssemblyQualified()} cannot manage {typeof(T).Name}, override or build a new serializaer");
+ throw new InvalidOperationException($"{typeof(BinaryBuffered<>).ToAssemblyQualified()} cannot manage {typeof(TData).Name}, override or build a new serializaer");
}
}
- ///
- public override ByteBuffer Serialize(string topic, T data)
+ ///
+ public override ByteBuffer Serialize(string topic, TData data)
{
return SerializeWithHeaders(topic, null!, data);
}
- ///
- public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T data)
+ ///
+ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, TData data)
{
headers?.Add(KNetSerialization.KeyTypeIdentifier, keyTypeName);
headers?.Add(KNetSerialization.KeySerializerIdentifier, keySerDesName);
@@ -163,13 +199,13 @@ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T
keyContainer.WriteTo(stream);
return stream.ToArray();
}
- ///
- public override T Deserialize(string topic, ByteBuffer data)
+ ///
+ public override TData Deserialize(string topic, ByteBuffer data)
{
return DeserializeWithHeaders(topic, null!, data);
}
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
{
if (_defaultSerDes != null) return _defaultSerDes.DeserializeWithHeaders(topic, headers, data);
@@ -177,54 +213,90 @@ public override T DeserializeWithHeaders(string topic, Headers headers, ByteBuff
KeyContainer container = KeyContainer.Parser.ParseFrom(data.ToStream());
- return (T)container.GetContent();
+ return (TData)container.GetContent();
}
}
}
///
- /// Base class to define ValueContainer extensions of , for example
+ /// Base class to define ValueContainer extensions of , for example
///
- public static class ValueContainer
+ public class ValueContainer : ISerDesSelector where T : class, IMessage
{
///
- /// Protobuf ValueContainer Binary encoder extension of , for example based on array
+ /// Returns a new instance of
+ ///
+ /// The of
+ public static ISerDesSelector NewInstance() => new ValueContainer();
+ ///
+ public static string SelectorTypeName => typeof(ValueContainer<>).ToAssemblyQualified();
+ ///
+ public static Type ByteArraySerDes => typeof(BinaryRaw);
+ ///
+ public static Type ByteBufferSerDes => typeof(BinaryBuffered);
+ ///
+ public static ISerDes NewSerDes()
+ {
+ if (typeof(TJVM) == typeof(Java.Nio.ByteBuffer)) return (ISerDes)NewByteBufferSerDes();
+ return (ISerDes)NewByteArraySerDes();
+ }
+ ///
+ public static ISerDesRaw NewByteArraySerDes() { return new BinaryRaw(SelectorTypeName); }
+ ///
+ public static ISerDesBuffered NewByteBufferSerDes() { return new BinaryBuffered(SelectorTypeName); }
+
+ ///
+ string ISerDesSelector.SelectorTypeName => SelectorTypeName;
+ ///
+ Type ISerDesSelector.ByteArraySerDes => ByteArraySerDes;
+ ///
+ Type ISerDesSelector.ByteBufferSerDes => ByteBufferSerDes;
+ ///
+ ISerDes ISerDesSelector.NewSerDes() => NewSerDes();
+ ///
+ ISerDesRaw ISerDesSelector.NewByteArraySerDes() => NewByteArraySerDes();
+ ///
+ ISerDesBuffered ISerDesSelector.NewByteBufferSerDes() => NewByteBufferSerDes();
+
+ ///
+ /// Protobuf ValueContainer Binary encoder extension of , for example based on array
///
- ///
- public class BinaryRaw : SerDesRaw where T : class, IMessage
+ ///
+ sealed class BinaryRaw : SerDesRaw where TData : class, IMessage
{
- readonly byte[] valueContainerSerDesName = Encoding.UTF8.GetBytes(typeof(BinaryRaw<>).ToAssemblyQualified());
+ readonly byte[] valueContainerSerDesName;
readonly byte[] valueContainerName = null!;
///
public override bool UseHeaders => true;
///
/// Default initializer
///
- public BinaryRaw()
+ public BinaryRaw(string selectorName)
{
- var tt = typeof(T);
+ valueContainerSerDesName = Encoding.UTF8.GetBytes(selectorName);
+ var tt = typeof(TData);
if (tt.IsGenericType)
{
var keyT = tt.GetGenericArguments();
- if (keyT.Length != 1) { throw new ArgumentException($"{typeof(T).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
+ if (keyT.Length != 1) { throw new ArgumentException($"{typeof(TData).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
var t = tt.GetGenericTypeDefinition();
if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
{
valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
return;
}
- else throw new ArgumentException($"{typeof(T).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
+ else throw new ArgumentException($"{typeof(TData).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
}
- throw new ArgumentException($"{typeof(T).Name} is not a generic type and cannot be used as a valid ValueContainer type");
+ throw new ArgumentException($"{typeof(TData).Name} is not a generic type and cannot be used as a valid ValueContainer type");
}
- ///
- public override byte[] Serialize(string topic, T data)
+ ///
+ public override byte[] Serialize(string topic, TData data)
{
return SerializeWithHeaders(topic, null!, data);
}
- ///
- public override byte[] SerializeWithHeaders(string topic, Headers headers, T data)
+ ///
+ public override byte[] SerializeWithHeaders(string topic, Headers headers, TData data)
{
headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
@@ -235,58 +307,59 @@ public override byte[] SerializeWithHeaders(string topic, Headers headers, T dat
return stream.ToArray();
}
}
- ///
- public override T Deserialize(string topic, byte[] data)
+ ///
+ public override TData Deserialize(string topic, byte[] data)
{
return DeserializeWithHeaders(topic, null!, data);
}
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, byte[] data)
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, byte[] data)
{
if (data == null) return default!;
var container = Storage.ValueContainer.Parser.ParseFrom(data);
- return (Activator.CreateInstance(typeof(T), container) as T)!;
+ return (Activator.CreateInstance(typeof(TData), container) as TData)!;
}
}
///
- /// Protobuf ValueContainer Binary encoder extension of , for example based on
+ /// Protobuf ValueContainer Binary encoder extension of , for example based on
///
- ///
- public class BinaryBuffered : SerDesBuffered where T : class, IMessage
+ ///
+ sealed class BinaryBuffered : SerDesBuffered where TData : class, IMessage
{
- readonly byte[] valueContainerSerDesName = Encoding.UTF8.GetBytes(typeof(BinaryBuffered<>).ToAssemblyQualified());
+ readonly byte[] valueContainerSerDesName;
readonly byte[] valueContainerName = null!;
///
public override bool UseHeaders => true;
///
/// Default initializer
///
- public BinaryBuffered()
+ public BinaryBuffered(string selectorName)
{
- var tt = typeof(T);
+ valueContainerSerDesName = Encoding.UTF8.GetBytes(selectorName);
+ var tt = typeof(TData);
if (tt.IsGenericType)
{
var keyT = tt.GetGenericArguments();
- if (keyT.Length != 1) { throw new ArgumentException($"{typeof(T).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
+ if (keyT.Length != 1) { throw new ArgumentException($"{typeof(TData).Name} does not contains a single generic argument and cannot be used because it is not a valid ValueContainer type"); }
var t = tt.GetGenericTypeDefinition();
if (t.GetInterface(typeof(IValueContainer<>).Name) != null)
{
valueContainerName = Encoding.UTF8.GetBytes(t.ToAssemblyQualified());
return;
}
- else throw new ArgumentException($"{typeof(T).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
+ else throw new ArgumentException($"{typeof(TData).Name} does not implement IValueContainer<> and cannot be used because it is not a valid ValueContainer type");
}
- throw new ArgumentException($"{typeof(T).Name} is not a generic type and cannot be used as a valid ValueContainer type");
+ throw new ArgumentException($"{typeof(TData).Name} is not a generic type and cannot be used as a valid ValueContainer type");
}
- ///
- public override ByteBuffer Serialize(string topic, T data)
+ ///
+ public override ByteBuffer Serialize(string topic, TData data)
{
return SerializeWithHeaders(topic, null!, data);
}
- ///
- public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T data)
+ ///
+ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, TData data)
{
headers?.Add(KNetSerialization.ValueSerializerIdentifier, valueContainerSerDesName);
headers?.Add(KNetSerialization.ValueTypeIdentifier, valueContainerName);
@@ -295,17 +368,17 @@ public override ByteBuffer SerializeWithHeaders(string topic, Headers headers, T
data.WriteTo(stream);
return ByteBuffer.From(stream);
}
- ///
- public override T Deserialize(string topic, ByteBuffer data)
+ ///
+ public override TData Deserialize(string topic, ByteBuffer data)
{
return DeserializeWithHeaders(topic, null!, data);
}
- ///
- public override T DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
+ ///
+ public override TData DeserializeWithHeaders(string topic, Headers headers, ByteBuffer data)
{
if (data == null) return default!;
var container = Storage.ValueContainer.Parser.ParseFrom(data.ToStream());
- return (Activator.CreateInstance(typeof(T), container) as T)!;
+ return (Activator.CreateInstance(typeof(TData), container) as TData)!;
}
}
}
diff --git a/src/net/KEFCore.SerDes.Protobuf/Storage/ProtobufValueContainer.cs b/src/net/KEFCore.SerDes.Protobuf/Storage/ProtobufValueContainer.cs
index 342d566b..0191f6a8 100644
--- a/src/net/KEFCore.SerDes.Protobuf/Storage/ProtobufValueContainer.cs
+++ b/src/net/KEFCore.SerDes.Protobuf/Storage/ProtobufValueContainer.cs
@@ -20,6 +20,7 @@
using Google.Protobuf;
using Google.Protobuf.Reflection;
+using MASES.KNet.Serialization;
namespace MASES.EntityFrameworkCore.KNet.Serialization.Protobuf.Storage;
@@ -58,7 +59,7 @@ public ProtobufValueContainer(IEntityType tName, object[] rData)
_innerMessage = new ValueContainer
{
EntityName = tName.Name,
- ClrType = tName.ClrType.FullName!
+ ClrType = tName.ClrType?.ToAssemblyQualified()!
};
_innerMessage.Data.Clear();
foreach (var item in tName.GetProperties())
@@ -68,7 +69,7 @@ public ProtobufValueContainer(IEntityType tName, object[] rData)
{
PropertyIndex = index,
PropertyName = item.Name,
- ClrType = item.ClrType?.FullName,
+ ClrType = item.ClrType?.ToAssemblyQualified(),
Value = new GenericValue(rData[index])
};
_innerMessage.Data.Add(pRecord);
diff --git a/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs b/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs
index c70e2746..d5054792 100644
--- a/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs
+++ b/src/net/KEFCore.SerDes/DefaultKEFCoreSerDes.cs
@@ -27,51 +27,87 @@
namespace MASES.EntityFrameworkCore.KNet.Serialization.Json;
///
-/// Default base class to define extensions of , for example
+/// Default base class to define extensions of , for example
///
public static class DefaultKEFCoreSerDes
{
///
/// Returns the default serializer for keys
///
- public static readonly Type DefaultKeySerialization = typeof(Key.JsonRaw<>);
+ public static readonly Type DefaultKeySerialization = typeof(Key<>);
///
/// Returns the default serializer for value containers
///
- public static readonly Type DefaultValueContainerSerialization = typeof(ValueContainer.JsonRaw<>);
+ public static readonly Type DefaultValueContainerSerialization = typeof(ValueContainer<>);
///
/// Returns the default for value containers
///
public static readonly Type DefaultValueContainer = typeof(DefaultValueContainer<>);
///
- /// Base class to define key extensions of , for example
+ /// Base class to define key extensions of , for example
///
- public static class Key
+ public class Key : ISerDesSelector
{
///
- /// Json extension of , for example based on array
+ /// Returns a new instance of
///
- /// The type to be serialized or deserialized. It can be a Primary Key or a ValueContainer like
- public class JsonRaw : SerDesRaw
+ /// The of
+ public static ISerDesSelector NewInstance() => new Key();
+ ///
+ public static string SelectorTypeName => typeof(Key<>).ToAssemblyQualified();
+ ///
+ public static Type ByteArraySerDes => typeof(JsonRaw);
+ ///
+ public static Type ByteBufferSerDes => typeof(JsonBuffered);
+ ///
+ public static ISerDes NewSerDes()
{
- readonly byte[] keySerDesName = Encoding.UTF8.GetBytes(typeof(JsonRaw<>).ToAssemblyQualified());
- readonly byte[] keyTypeName = Encoding.UTF8.GetBytes(typeof(T).FullName!);
- readonly ISerDesRaw _defaultSerDes = default!;
+ if (typeof(TJVM) == typeof(Java.Nio.ByteBuffer)) return (ISerDes)NewByteBufferSerDes();
+ return (ISerDes)NewByteArraySerDes();
+ }
+ ///
+ public static ISerDesRaw NewByteArraySerDes() { return new JsonRaw(SelectorTypeName); }
+ ///
+ public static ISerDesBuffered NewByteBufferSerDes() { return new JsonBuffered(SelectorTypeName); }
+
+ ///
+ string ISerDesSelector.SelectorTypeName => SelectorTypeName;
+ ///
+ Type ISerDesSelector.ByteArraySerDes => ByteArraySerDes;
+ ///
+ Type ISerDesSelector.ByteBufferSerDes => ByteBufferSerDes;
+ ///
+ ISerDes ISerDesSelector.NewSerDes() => NewSerDes();
+ ///
+ ISerDesRaw ISerDesSelector.NewByteArraySerDes() => NewByteArraySerDes();
+ ///