I have a request / handler based architecture app . I have the following interface:
public interface IQueryHandler<TQuery, TResult> where TQuery : IQuery<TResult>
{
TResult Handle(TQuery query);
}
There are many non-general implementations of this interface. These implementations are wrapped by universal decorators for registration, profiling, authorization, etc. Sometimes, however, I want to apply a common decorator conditionally based on the constraints of a typical decorator type. Take, for example, this caching decoder, which can only be applied to queries that return ReadOnlyCollection<T>(simply because caching any collection that is modified does not make much sense):
public class CachingQueryHandlerDecorator<TQuery, TResult>
: IQueryHandler<TQuery, ReadOnlyCollection<TResult>>
where TQuery : IQuery<ReadOnlyCollection<TResult>>
{
private readonly IQueryHandler<TQuery, ReadOnlyCollection<TResult>> decoratee;
private readonly IQueryCache cache;
public CachingQueryHandlerDecorator(
IQueryHandler<TQuery, ReadOnlyCollection<TResult>> decoratee,
IQueryCache cache)
{
this.decoratee = decoratee;
this.cache = cache;
}
public ReadOnlyCollection<TResult> Handle(TQuery query)
{
ReadOnlyCollection<TResult> result;
if (!this.cache.TryGetResult(query, out result))
{
this.cache.Store(query, result = this.decoratee.Handle(query));
}
return result;
}
}
, , . . , CachingQueryHandlerDecorator ProfilingQueryHandlerDecorator SecurityQueryHandlerDecorator.
, ; , . Autofac?