From a6efe9285111c8d76abf58764ab62a2c29dee1d0 Mon Sep 17 00:00:00 2001 From: Jason Webb Date: Fri, 8 Nov 2024 15:08:00 -0700 Subject: [PATCH] Moved caching factory out of the constructors of CommandBus and QueryBus as it will be optional. Added some caching configuration validation. --- .../Commands/CommandBus.cs | 8 +++++--- .../InvalidCacheException.cs | 16 ++++++++++++++++ .../Queries/QueryBus.cs | 8 +++++--- 3 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 Src/RCommon.ApplicationServices/InvalidCacheException.cs diff --git a/Src/RCommon.ApplicationServices/Commands/CommandBus.cs b/Src/RCommon.ApplicationServices/Commands/CommandBus.cs index 208881a..6e8b0f1 100644 --- a/Src/RCommon.ApplicationServices/Commands/CommandBus.cs +++ b/Src/RCommon.ApplicationServices/Commands/CommandBus.cs @@ -45,17 +45,16 @@ public class CommandBus : ICommandBus private readonly IServiceProvider _serviceProvider; private readonly IValidationService _validationService; private readonly IOptions _validationOptions; - private readonly ICacheService _cacheService; + private ICacheService _cacheService; private readonly CachingOptions _cachingOptions; public CommandBus(ILogger logger, IServiceProvider serviceProvider, IValidationService validationService, - IOptions validationOptions, IOptions cachingOptions, ICommonFactory cacheFactory) + IOptions validationOptions, IOptions cachingOptions) { _logger = logger; _serviceProvider = serviceProvider; _validationService = validationService; _validationOptions = validationOptions; - _cacheService = cacheFactory.Create(ExpressionCachingStrategy.Default); _cachingOptions = cachingOptions.Value; } @@ -134,6 +133,9 @@ private CommandExecutionDetails GetCommandExecutionDetails(Type commandType) { if (_cachingOptions.CachingEnabled && _cachingOptions.CacheDynamicallyCompiledExpressions) { + var cachingFactory = _serviceProvider.GetService>(); + Guard.Against(cachingFactory == null, "We could not properly inject the caching factory: 'ICommonFactory>' into the CommandBus"); + _cacheService = cachingFactory.Create(ExpressionCachingStrategy.Default); return _cacheService.GetOrCreate(CacheKey.With(GetType(), commandType.GetCacheKey()), () => this.BuildCommandDetails(commandType)); } return this.BuildCommandDetails(commandType); diff --git a/Src/RCommon.ApplicationServices/InvalidCacheException.cs b/Src/RCommon.ApplicationServices/InvalidCacheException.cs new file mode 100644 index 0000000..9028134 --- /dev/null +++ b/Src/RCommon.ApplicationServices/InvalidCacheException.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RCommon.ApplicationServices +{ + public class InvalidCacheException : GeneralException + { + public InvalidCacheException(string message):base(message) + { + + } + } +} diff --git a/Src/RCommon.ApplicationServices/Queries/QueryBus.cs b/Src/RCommon.ApplicationServices/Queries/QueryBus.cs index 868735b..c55bff3 100644 --- a/Src/RCommon.ApplicationServices/Queries/QueryBus.cs +++ b/Src/RCommon.ApplicationServices/Queries/QueryBus.cs @@ -49,17 +49,16 @@ private class HandlerFuncMapping private readonly IValidationService _validationService; private readonly IOptions _validationOptions; private readonly CachingOptions _cachingOptions; - private readonly ICacheService _cacheService; + private ICacheService _cacheService; public QueryBus(ILogger logger, IServiceProvider serviceProvider, IValidationService validationService, - IOptions validationOptions, IOptions cachingOptions, ICommonFactory cacheFactory) + IOptions validationOptions, IOptions cachingOptions) { _logger = logger; _serviceProvider = serviceProvider; _validationService = validationService; _validationOptions = validationOptions; _cachingOptions = cachingOptions.Value; - _cacheService = cacheFactory.Create(ExpressionCachingStrategy.Default); } public async Task DispatchQueryAsync(IQuery query, CancellationToken cancellationToken = default) @@ -92,6 +91,9 @@ private HandlerFuncMapping GetHandlerFuncMapping(Type queryType) { if (_cachingOptions.CachingEnabled && _cachingOptions.CacheDynamicallyCompiledExpressions) { + var cachingFactory = _serviceProvider.GetService>(); + Guard.Against(cachingFactory == null, "We could not properly inject the caching factory: 'ICommonFactory>' into the QueryBus"); + _cacheService = cachingFactory.Create(ExpressionCachingStrategy.Default); return _cacheService.GetOrCreate(CacheKey.With(GetType(), queryType.GetCacheKey()), () => this.BuildHandlerFuncMapping(queryType)); }