diff --git a/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs b/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs index 3a9745f1..51a8c886 100644 --- a/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs +++ b/Examples/ApplicationServices/Examples.ApplicationServices.CQRS/Program.cs @@ -6,6 +6,7 @@ using RCommon; using RCommon.ApplicationServices; using RCommon.ApplicationServices.ExecutionResults; +using RCommon.Caching; using RCommon.FluentValidation; using System.Diagnostics; using System.Reflection; @@ -32,6 +33,7 @@ // Or this way which uses a little magic but is simple cqrs.AddCommandHandlers((typeof(Program).GetTypeInfo().Assembly)); cqrs.AddQueryHandlers((typeof(Program).GetTypeInfo().Assembly)); + cqrs.AddMemoryCachingForHandlers(); }) .WithValidation(validation => { diff --git a/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs b/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs index e96e237f..0ba7438c 100644 --- a/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs +++ b/Examples/Caching/Examples.Caching.MemoryCaching/Program.cs @@ -4,6 +4,8 @@ using Microsoft.Extensions.Hosting; using RCommon; using RCommon.Caching; +using RCommon.Json; +using RCommon.JsonNet; using RCommon.MemoryCache; using System.Diagnostics; using System.Reflection; @@ -21,6 +23,7 @@ { // Configure RCommon services.AddRCommon() + .WithJsonSerialization() .WithMemoryCaching(cache => { cache.Configure(x => diff --git a/Examples/Caching/Examples.Caching.PersistenceCaching/Program.cs b/Examples/Caching/Examples.Caching.PersistenceCaching/Program.cs index 4b9626dc..59237f93 100644 --- a/Examples/Caching/Examples.Caching.PersistenceCaching/Program.cs +++ b/Examples/Caching/Examples.Caching.PersistenceCaching/Program.cs @@ -19,7 +19,8 @@ .ConfigureServices(services => { // Configure RCommon - services.AddRCommon(); + services.AddRCommon() + .WithJsonSerialization(); services.AddTransient(); diff --git a/Examples/Caching/Examples.Caching.RedisCaching/Examples.Caching.RedisCaching.csproj b/Examples/Caching/Examples.Caching.RedisCaching/Examples.Caching.RedisCaching.csproj index 93b3dda3..8949b36f 100644 --- a/Examples/Caching/Examples.Caching.RedisCaching/Examples.Caching.RedisCaching.csproj +++ b/Examples/Caching/Examples.Caching.RedisCaching/Examples.Caching.RedisCaching.csproj @@ -9,6 +9,7 @@ + diff --git a/Examples/Caching/Examples.Caching.RedisCaching/Program.cs b/Examples/Caching/Examples.Caching.RedisCaching/Program.cs index cb11238f..3f885746 100644 --- a/Examples/Caching/Examples.Caching.RedisCaching/Program.cs +++ b/Examples/Caching/Examples.Caching.RedisCaching/Program.cs @@ -1,9 +1,11 @@ using Examples.Caching.RedisCaching; +using RCommon.Json; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using RCommon; using RCommon.Caching; +using RCommon.JsonNet; using RCommon.RedisCache; using System.Diagnostics; using System.Reflection; @@ -21,6 +23,7 @@ { // Configure RCommon services.AddRCommon() + .WithJsonSerialization() .WithDistributedCaching(cache => { cache.Configure(redis => diff --git a/Src/RCommon.ApplicationServices/CqrsCachingOptions.cs b/Src/RCommon.ApplicationServices/CqrsCachingOptions.cs new file mode 100644 index 00000000..69c8878e --- /dev/null +++ b/Src/RCommon.ApplicationServices/CqrsCachingOptions.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.ApplicationServices +{ + public class CqrsCachingOptions + { + public CqrsCachingOptions() + { + this.UseCacheForHandlers = false; + } + + public bool UseCacheForHandlers { get; set; } + } +} diff --git a/Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs b/Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs new file mode 100644 index 00000000..fd9f4966 --- /dev/null +++ b/Src/RCommon.ApplicationServices/ICqrsBuilderExtensions.cs @@ -0,0 +1,42 @@ +using RCommon.Caching; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.ApplicationServices +{ + public static class ICqrsBuilderExtensions + { + public static ICqrsBuilder AddMemoryCachingForHandlers(this ICqrsBuilder builder) + where T : IMemoryCachingBuilder + { + return AddMemoryCachingForHandlers(builder, x => { }); + } + + public static ICqrsBuilder AddMemoryCachingForHandlers(this ICqrsBuilder builder, Action actions) + where T : IMemoryCachingBuilder + { + Guard.IsNotNull(actions, nameof(actions)); + var cachingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder }); + actions(cachingConfig); + return builder; + } + + public static ICqrsBuilder AddDistributedCachingForHandlers(this ICqrsBuilder builder) + where T : IDistributedCachingBuilder + { + return AddDistributedCachingForHandlers(builder, x => { }); + } + + public static ICqrsBuilder AddDistributedCachingForHandlers(this ICqrsBuilder builder, Action actions) + where T : IDistributedCachingBuilder + { + Guard.IsNotNull(actions, nameof(actions)); + var cachingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder }); + actions(cachingConfig); + return builder; + } + } +} diff --git a/Src/RCommon.ApplicationServices/Queries/QueryBus.cs b/Src/RCommon.ApplicationServices/Queries/QueryBus.cs index 9bde45a2..c2482a0f 100644 --- a/Src/RCommon.ApplicationServices/Queries/QueryBus.cs +++ b/Src/RCommon.ApplicationServices/Queries/QueryBus.cs @@ -88,6 +88,7 @@ public async Task DispatchQueryAsync(IQuery query, Ca private CacheItem GetCacheItem(Type queryType) { + if return _memoryCache.GetOrCreate( CacheKey.With(GetType(), queryType.GetCacheKey()), e => diff --git a/Src/RCommon.ApplicationServices/RCommon.ApplicationServices.csproj b/Src/RCommon.ApplicationServices/RCommon.ApplicationServices.csproj index 77b4c074..a8b62f9b 100644 --- a/Src/RCommon.ApplicationServices/RCommon.ApplicationServices.csproj +++ b/Src/RCommon.ApplicationServices/RCommon.ApplicationServices.csproj @@ -5,21 +5,16 @@ + - - - - - - diff --git a/Src/RCommon.Caching/RCommon.Caching.csproj b/Src/RCommon.Caching/RCommon.Caching.csproj index f8cbcad8..f006da65 100644 --- a/Src/RCommon.Caching/RCommon.Caching.csproj +++ b/Src/RCommon.Caching/RCommon.Caching.csproj @@ -1,10 +1,8 @@  - - net8.0 - enable - enable - + + net6.0;net8.0; + diff --git a/Src/RCommon.Json/RCommon.Json.csproj b/Src/RCommon.Json/RCommon.Json.csproj index e79b9d93..806700ef 100644 --- a/Src/RCommon.Json/RCommon.Json.csproj +++ b/Src/RCommon.Json/RCommon.Json.csproj @@ -1,10 +1,8 @@  - - net8.0 - enable - enable - + + net6.0;net8.0; + diff --git a/Src/RCommon.JsonNet/RCommon.JsonNet.csproj b/Src/RCommon.JsonNet/RCommon.JsonNet.csproj index d503989a..38951e81 100644 --- a/Src/RCommon.JsonNet/RCommon.JsonNet.csproj +++ b/Src/RCommon.JsonNet/RCommon.JsonNet.csproj @@ -1,10 +1,8 @@  - - net8.0 - enable - enable - + + net6.0;net8.0; + diff --git a/Src/RCommon.MemoryCache/RCommon.MemoryCache.csproj b/Src/RCommon.MemoryCache/RCommon.MemoryCache.csproj index 3b6a5faa..49d3ce5a 100644 --- a/Src/RCommon.MemoryCache/RCommon.MemoryCache.csproj +++ b/Src/RCommon.MemoryCache/RCommon.MemoryCache.csproj @@ -1,10 +1,8 @@  - - net8.0 - enable - enable - + + net6.0;net8.0; + diff --git a/Src/RCommon.Persistence.Caching/RCommon.Persistence.Caching.csproj b/Src/RCommon.Persistence.Caching/RCommon.Persistence.Caching.csproj index 3e593c66..958b00c2 100644 --- a/Src/RCommon.Persistence.Caching/RCommon.Persistence.Caching.csproj +++ b/Src/RCommon.Persistence.Caching/RCommon.Persistence.Caching.csproj @@ -1,10 +1,8 @@  - - net8.0 - enable - enable - + + net6.0;net8.0; + diff --git a/Src/RCommon.RedisCache/RCommon.RedisCache.csproj b/Src/RCommon.RedisCache/RCommon.RedisCache.csproj index e8f1ca25..4ab67fea 100644 --- a/Src/RCommon.RedisCache/RCommon.RedisCache.csproj +++ b/Src/RCommon.RedisCache/RCommon.RedisCache.csproj @@ -1,10 +1,8 @@  - - net8.0 - enable - enable - + + net6.0;net8.0; + diff --git a/Src/RCommon.SystemTextJson/RCommon.SystemTextJson.csproj b/Src/RCommon.SystemTextJson/RCommon.SystemTextJson.csproj index d7f99a04..26c28f9a 100644 --- a/Src/RCommon.SystemTextJson/RCommon.SystemTextJson.csproj +++ b/Src/RCommon.SystemTextJson/RCommon.SystemTextJson.csproj @@ -1,10 +1,8 @@  - - net8.0 - enable - enable - + + net6.0;net8.0; +