diff --git a/guides/rabbitmq/Serialization.md b/guides/rabbitmq/Serialization.md index e2562f5..0676da6 100644 --- a/guides/rabbitmq/Serialization.md +++ b/guides/rabbitmq/Serialization.md @@ -32,24 +32,27 @@ You can configure your own, or use one of the provided `IserializationProviders` to help simplify code. Here we use the `JsonProvider` to take ` input` and serialize the object to `byte[]`. ```csharp +using HouseofCat.RabbitMQ; +using HouseofCat.RabbitMQ.Pools; using HouseofCat.Serialization; +using HouseofCat.Utilities; -var rabbitOptions = JsonFileReader.ReadFileAsync("SampleRabbitOptions.json"); +var rabbitOptions = await JsonFileReader.ReadFileAsync("SampleRabbitOptions.json"); var channelPool = new ChannelPool(rabbitOptions); var jsonProvider = new JsonProvider(); var channelHost = await channelPool.GetChannelAsync(); -var channel = channelHost.GetChannel(); -var properties = channel.CreateBasicProperties(); +var properties = channelHost.Channel.CreateBasicProperties(); properties.DeliveryMode = 2; -var data = jsonProvider.Serialize(input); +var myClass = new { Name = "MyName", Value = 42 }; +var body = jsonProvider.Serialize(myClass); var error = false; try { - channel.BasicPublish("MyExchange", "MyRoutingKey", properties, data); + channelHost.Channel.BasicPublish("MyExchange", "MyRoutingKey", false, properties, body); } catch (Exception ex) { @@ -63,27 +66,30 @@ await channelPool.ReturnChannelAsync(channelHost, error); ### Serialize And Compress An example showing JsonSerialization and GzipCompression. ```csharp -using HouseofCat.Serialization; using HouseofCat.Compression; +using HouseofCat.RabbitMQ; +using HouseofCat.RabbitMQ.Pools; +using HouseofCat.Serialization; +using HouseofCat.Utilities; -var rabbitOptions = JsonFileReader.ReadFileAsync("SampleRabbitOptions.json"); +var rabbitOptions = await JsonFileReader.ReadFileAsync("SampleRabbitOptions.json"); var channelPool = new ChannelPool(rabbitOptions); var jsonProvider = new JsonProvider(); var gzipProvider = new GzipProvider(); var channelHost = await channelPool.GetChannelAsync(); -var channel = channelHost.GetChannel(); -var properties = channel.CreateBasicProperties(); +var properties = channelHost.Channel.CreateBasicProperties(); properties.DeliveryMode = 2; -var dataAsJson = jsonProvider.Serialize(input); -var compressedJson = gzipProvider.Compress(dataAsJson); +var myClass = new { Name = "MyName", Value = 42 }; +var myClassAsJson = jsonProvider.Serialize(myClass); +var body = gzipProvider.Compress(myClassAsJson); var error = false; try { - channel.BasicPublish("MyExchange", "MyRoutingKey", properties, compressedJson); + channelHost.Channel.BasicPublish("MyExchange", "MyRoutingKey", false, properties, body); } catch (Exception ex) { @@ -97,10 +103,15 @@ await channelPool.ReturnChannelAsync(channelHost, error); ### Serialize, Compression, And Encrypt An example showing JsonSerialization, Gzip compression, and AesGcm 256 bit encryption. ```csharp -using HouseofCat.Serialization; using HouseofCat.Compression; +using HouseofCat.Encryption; +using HouseofCat.Hashing; +using HouseofCat.RabbitMQ; +using HouseofCat.RabbitMQ.Pools; +using HouseofCat.Serialization; +using HouseofCat.Utilities; -var rabbitOptions = JsonFileReader.ReadFileAsync("SampleRabbitOptions.json"); +var rabbitOptions = await JsonFileReader.ReadFileAsync("SampleRabbitOptions.json"); var channelPool = new ChannelPool(rabbitOptions); var jsonProvider = new JsonProvider(); var gzipProvider = new GzipProvider(); @@ -110,19 +121,18 @@ var aes256Key = argonProvider.GetHashKey("MySuperSecretPassword", "MySaltySaltSa var aesProvider = new AesGcmEncryptionProvider(aes256Key); var channelHost = await channelPool.GetChannelAsync(); -var channel = channelHost.GetChannel(); - -var properties = channel.CreateBasicProperties(); +var properties = channelHost.Channel.CreateBasicProperties(); properties.DeliveryMode = 2; -var dataAsJson = jsonProvider.Serialize(input); -var compressedJson = gzipProvider.Compress(dataAsJson); -var encryptedCompressedJson = aesProvider.Encrypt(encryptedCompressedJson); +var myClass = new { Name = "MyName", Value = 42 }; +var myClassAsJson = jsonProvider.Serialize(myClass); +var compressedJson = gzipProvider.Compress(myClassAsJson); +var body = aesProvider.Encrypt(compressedJson); var error = false; try { - channel.BasicPublish("MyExchange", "MyRoutingKey", properties, compressedJson); + channelHost.Channel.BasicPublish("MyExchange", "MyRoutingKey", false, properties, compressedJson); } catch (Exception ex) { @@ -136,54 +146,79 @@ await channelPool.ReturnChannelAsync(channelHost, error); ### DataTransfomer Alternative #1 You can also use the `DataTransformer` to simplify the process of serialization, compression, and/or encryption. ```csharp +using HouseofCat.Compression; +using HouseofCat.Data; +using HouseofCat.Encryption; +using HouseofCat.Hashing; +using HouseofCat.Serialization; + var hashingProvider = new ArgonHashingProvider(); -var hashKey = hashingProvider.GetHashKey(Passphrase, Salt, 32); +var hashKey = hashingProvider.GetHashKey("PasswordMcPassword", "SaltySaltSalt", 32); var dataTransformer = new DataTransformer( new JsonProvider(), new AesGcmEncryptionProvider(hashKey), new GzipProvider()); -var data = dataTransformer.Serialize(input) +var myClass = new { Name = "MyName", Value = 42 }; +var data = dataTransformer.Serialize(myClass); ``` ### DataTransfomer Alternative #2 You can also use the `DataTransformer` to simplify the process of serialization, compression, and/or encryption. This constructor assumes you want to use Json, Gzip, and AesGcm 256 bit encryption. ```csharp -var hashingProvider = new ArgonHashingProvider(); -var hashKey = hashingProvider.GetHashKey(Passphrase, Salt, 32); +using HouseofCat.Data; // Json, Gzip, and AesGcm 256 bit encryption. -_middleware = new DataTransformer(Passphrase, Salt, 32); +var transformer = new DataTransformer("PasswordMcPassword", "SaltySaltSalt", 32); // Json and Gzip only. -_middleware = new DataTransformer(); +// var transformer = new DataTransformer(); -_serializedData = _middleware.Serialize(input) +var myClass = new { Name = "MyName", Value = 42 }; +var body = transformer.Serialize(myClass); ``` ### Deserializing This is kind of simple. You want to deserialize, decompress, and decrypt your `byte[]` back into your object in the opposite manner you performed the serialization, compression, and encryption. ```csharp -var dataAsJson = jsonProvider.Serialize(input); +using HouseofCat.Compression; +using HouseofCat.Encryption; +using HouseofCat.Hashing; +using HouseofCat.Serialization; + +var jsonProvider = new JsonProvider(); +var gzipProvider = new GzipProvider(); +var argonProvider = new ArgonHashingProvider(); + +var aes256Key = argonProvider.GetHashKey("MySuperSecretPassword", "MySaltySaltSalt", size: 32); +var aesProvider = new AesGcmEncryptionProvider(aes256Key); + +// Making encrypted compressed json. +var myClass = new MyClass { Name = "MyName", Value = 42 }; +var dataAsJson = jsonProvider.Serialize(myClass); var compressedJson = gzipProvider.Compress(dataAsJson); -var encryptedCompressedJson = aesProvider.Encrypt(encryptedCompressedJson); +var encryptedCompressedJson = aesProvider.Encrypt(compressedJson); -... +// Decrypting and decompressing json into your object. var decryptedCompressedJson = aesProvider.Decrypt(encryptedCompressedJson); var decompressedJson = gzipProvider.Decompress(decryptedCompressedJson); -var myObject = _jsonProvider.Deserialize(decompressedJson); +var myObject = jsonProvider.Deserialize(decompressedJson); ``` ### Deserializing With DataTransformer ```csharp -// Json, Gzip, and AesGcm 256 bit encryption. -var dataTransformer = new DataTransformer(Passphrase, Salt, 32); +using HouseofCat.Data; + +var transformer = new DataTransformer("PasswordMcPassword", "SaltySaltSalt", 32); -var data = dataTransformer.Serialize(input) +// Make encrypted and compressed json from object. +var myClass = new MyClass { Name = "MyName", Value = 42 }; +var body = transformer.Serialize(myClass); -var myObject = dataTransformer.Deserialize(data); +// Get object from encrypted compressed json. +var myObject = transformer.Deserialize(body); ``` \ No newline at end of file diff --git a/src/HouseofCat.Data/DataTransformer.cs b/src/HouseofCat.Data/DataTransformer.cs index cd60473..31b0097 100644 --- a/src/HouseofCat.Data/DataTransformer.cs +++ b/src/HouseofCat.Data/DataTransformer.cs @@ -6,6 +6,7 @@ using HouseofCat.Utilities.Errors; using System; using System.IO; +using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace HouseofCat.Data; @@ -57,6 +58,7 @@ public DataTransformer( /// /// /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public Task DeserializeAsync(ReadOnlyMemory data) { if (_encryptionProvider is not null && _compressionProvider is not null) @@ -82,6 +84,7 @@ public Task DeserializeAsync(ReadOnlyMemory data) /// /// /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public TOut Deserialize(ReadOnlyMemory data) { if (_encryptionProvider is not null && _compressionProvider is not null) @@ -100,6 +103,7 @@ public TOut Deserialize(ReadOnlyMemory data) return _serializationProvider.Deserialize(data); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public TOut DecryptDecompressDeserialize(ReadOnlyMemory data) { var decryptedData = _encryptionProvider.Decrypt(data); @@ -108,6 +112,7 @@ public TOut DecryptDecompressDeserialize(ReadOnlyMemory data) return _serializationProvider.Deserialize(decompressedData); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task DecryptDecompressDeserializeAsync(ReadOnlyMemory data) { var memoryStream = _encryptionProvider.DecryptToStream(data); @@ -124,6 +129,7 @@ public async Task DecryptDecompressDeserializeAsync(ReadOnlyMemory(ReadOnlyMemory data) { var decryptedData = _encryptionProvider.Decrypt(data); @@ -131,6 +137,7 @@ public TOut DecryptDeserialize(ReadOnlyMemory data) return _serializationProvider.Deserialize(decryptedData); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task DecryptDeserializeAsync(ReadOnlyMemory data) { var memoryStream = _encryptionProvider.DecryptToStream(data); @@ -141,6 +148,7 @@ public async Task DecryptDeserializeAsync(ReadOnlyMemory data) .ConfigureAwait(false); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public TOut DecompressDeserialize(ReadOnlyMemory data) { var decompressedData = _compressionProvider.Decompress(data); @@ -148,6 +156,7 @@ public TOut DecompressDeserialize(ReadOnlyMemory data) return _serializationProvider.Deserialize(decompressedData); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task DecompressDeserializeAsync(ReadOnlyMemory data) { var memoryStream = _compressionProvider.DecompressToStream(data); @@ -158,6 +167,7 @@ public async Task DecompressDeserializeAsync(ReadOnlyMemory da .ConfigureAwait(false); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory Serialize(TIn input) { if (_encryptionProvider is not null && _compressionProvider is not null) @@ -176,6 +186,7 @@ public ReadOnlyMemory Serialize(TIn input) return _serializationProvider.Serialize(input); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task> SerializeAsync(TIn input) { if (_encryptionProvider is not null && _compressionProvider is not null) @@ -194,6 +205,7 @@ public async Task> SerializeAsync(TIn input) return _serializationProvider.Serialize(input); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task> SerializeCompressEncryptAsync(TIn input) { var memoryStream = new MemoryStream(); @@ -218,6 +230,7 @@ await _serializationProvider return encryptionStream.ToArray(); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory SerializeCompressEncrypt(TIn input) { return _encryptionProvider @@ -226,6 +239,7 @@ public ReadOnlyMemory SerializeCompressEncrypt(TIn input) .Serialize(input))); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task> SerializeEncryptAsync(TIn input) { using var memoryStream = new MemoryStream(); @@ -244,6 +258,7 @@ await _serializationProvider return compressionStream.ToArray(); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory SerializeEncrypt(TIn input) { return _encryptionProvider @@ -251,6 +266,7 @@ public ReadOnlyMemory SerializeEncrypt(TIn input) .Serialize(input)); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task> SerializeCompressAsync(TIn input) { using var memoryStream = new MemoryStream(); @@ -267,10 +283,9 @@ public async Task> SerializeCompressAsync(TIn input) return compressionStream.ToArray(); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory SerializeCompress(TIn input) { - return _compressionProvider - .Compress(_serializationProvider - .Serialize(input)); + return _compressionProvider.Compress(_serializationProvider.Serialize(input)); } } diff --git a/src/HouseofCat.Data/RecyclableTransformer.cs b/src/HouseofCat.Data/RecyclableTransformer.cs index d8d4812..1f01d73 100644 --- a/src/HouseofCat.Data/RecyclableTransformer.cs +++ b/src/HouseofCat.Data/RecyclableTransformer.cs @@ -5,6 +5,7 @@ using HouseofCat.Utilities.Errors; using System; using System.IO; +using System.Runtime.CompilerServices; using System.Threading.Tasks; namespace HouseofCat.Data; @@ -35,6 +36,7 @@ public RecyclableTransformer( /// /// /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public ReadOnlyMemory Transform(TIn input) { using var serializedStream = RecyclableManager.GetStream(nameof(RecyclableTransformer)); @@ -52,6 +54,7 @@ public ReadOnlyMemory Transform(TIn input) /// /// /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task> TransformAsync(TIn input) { using var serializedStream = RecyclableManager.GetStream(nameof(RecyclableTransformer)); @@ -70,6 +73,7 @@ await _serializationProvider return encryptedStream.ToArray(); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public MemoryStream TransformToStream(TIn input) { using var serializedStream = RecyclableManager.GetStream(nameof(RecyclableTransformer)); @@ -80,6 +84,7 @@ public MemoryStream TransformToStream(TIn input) return _encryptionProvider.Encrypt(compressedStream, false); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task TransformToStreamAsync(TIn input) { using var serializedStream = RecyclableManager.GetStream(nameof(RecyclableTransformer)); @@ -96,6 +101,7 @@ await _serializationProvider .ConfigureAwait(false); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public TOut Restore(ReadOnlyMemory data) { using var decryptStream = _encryptionProvider.DecryptToStream(data); @@ -103,6 +109,7 @@ public TOut Restore(ReadOnlyMemory data) return _serializationProvider.Deserialize(decompressStream); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public TOut Restore(MemoryStream data) { using var decryptedStream = _encryptionProvider.Decrypt(data, false); @@ -110,6 +117,7 @@ public TOut Restore(MemoryStream data) return _serializationProvider.Deserialize(decompressedStream); } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public async Task RestoreAsync(ReadOnlyMemory data) { using var decryptedStream = _encryptionProvider.DecryptToStream(data);