Skip to content

Commit

Permalink
Moved caching factory out of the constructors of CommandBus and Query…
Browse files Browse the repository at this point in the history
…Bus as it will be optional. Added some caching configuration validation.
  • Loading branch information
jasonmwebb-lv committed Nov 8, 2024
1 parent 409b346 commit a6efe92
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
8 changes: 5 additions & 3 deletions Src/RCommon.ApplicationServices/Commands/CommandBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,16 @@ public class CommandBus : ICommandBus
private readonly IServiceProvider _serviceProvider;
private readonly IValidationService _validationService;
private readonly IOptions<CqrsValidationOptions> _validationOptions;
private readonly ICacheService _cacheService;
private ICacheService _cacheService;
private readonly CachingOptions _cachingOptions;

public CommandBus(ILogger<CommandBus> logger, IServiceProvider serviceProvider, IValidationService validationService,
IOptions<CqrsValidationOptions> validationOptions, IOptions<CachingOptions> cachingOptions, ICommonFactory<ExpressionCachingStrategy, ICacheService> cacheFactory)
IOptions<CqrsValidationOptions> validationOptions, IOptions<CachingOptions> cachingOptions)
{
_logger = logger;
_serviceProvider = serviceProvider;
_validationService = validationService;
_validationOptions = validationOptions;
_cacheService = cacheFactory.Create(ExpressionCachingStrategy.Default);
_cachingOptions = cachingOptions.Value;
}

Expand Down Expand Up @@ -134,6 +133,9 @@ private CommandExecutionDetails GetCommandExecutionDetails(Type commandType)
{
if (_cachingOptions.CachingEnabled && _cachingOptions.CacheDynamicallyCompiledExpressions)
{
var cachingFactory = _serviceProvider.GetService<ICommonFactory<ExpressionCachingStrategy, ICacheService>>();
Guard.Against<InvalidCacheException>(cachingFactory == null, "We could not properly inject the caching factory: 'ICommonFactory<ExpressionCachingStrategy, ICacheService>>' into the CommandBus");
_cacheService = cachingFactory.Create(ExpressionCachingStrategy.Default);
return _cacheService.GetOrCreate(CacheKey.With(GetType(), commandType.GetCacheKey()), () => this.BuildCommandDetails(commandType));
}
return this.BuildCommandDetails(commandType);
Expand Down
16 changes: 16 additions & 0 deletions Src/RCommon.ApplicationServices/InvalidCacheException.cs
Original file line number Diff line number Diff line change
@@ -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)
{

}
}
}
8 changes: 5 additions & 3 deletions Src/RCommon.ApplicationServices/Queries/QueryBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,16 @@ private class HandlerFuncMapping
private readonly IValidationService _validationService;
private readonly IOptions<CqrsValidationOptions> _validationOptions;
private readonly CachingOptions _cachingOptions;
private readonly ICacheService _cacheService;
private ICacheService _cacheService;

public QueryBus(ILogger<QueryBus> logger, IServiceProvider serviceProvider, IValidationService validationService,
IOptions<CqrsValidationOptions> validationOptions, IOptions<CachingOptions> cachingOptions, ICommonFactory<ExpressionCachingStrategy, ICacheService> cacheFactory)
IOptions<CqrsValidationOptions> validationOptions, IOptions<CachingOptions> cachingOptions)
{
_logger = logger;
_serviceProvider = serviceProvider;
_validationService = validationService;
_validationOptions = validationOptions;
_cachingOptions = cachingOptions.Value;
_cacheService = cacheFactory.Create(ExpressionCachingStrategy.Default);
}

public async Task<TResult> DispatchQueryAsync<TResult>(IQuery<TResult> query, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -92,6 +91,9 @@ private HandlerFuncMapping GetHandlerFuncMapping(Type queryType)
{
if (_cachingOptions.CachingEnabled && _cachingOptions.CacheDynamicallyCompiledExpressions)
{
var cachingFactory = _serviceProvider.GetService<ICommonFactory<ExpressionCachingStrategy, ICacheService>>();
Guard.Against<InvalidCacheException>(cachingFactory == null, "We could not properly inject the caching factory: 'ICommonFactory<ExpressionCachingStrategy, ICacheService>>' into the QueryBus");
_cacheService = cachingFactory.Create(ExpressionCachingStrategy.Default);
return _cacheService.GetOrCreate(CacheKey.With(GetType(), queryType.GetCacheKey()),
() => this.BuildHandlerFuncMapping(queryType));
}
Expand Down

0 comments on commit a6efe92

Please sign in to comment.