Skip to content

Instantly share code, notes, and snippets.

@JonathanLoscalzo
Created June 22, 2018 14:09
Show Gist options
  • Save JonathanLoscalzo/446efd16061c780cd4f142a7d5538481 to your computer and use it in GitHub Desktop.
Save JonathanLoscalzo/446efd16061c780cd4f142a7d5538481 to your computer and use it in GitHub Desktop.
an approach for add aspects (AOP) to C# service
public interface ICommandHandler<TCommand>
{
void Handle(TCommand command);
}
public interface IQueryHandler<TQuery, TResult>
{
TResult Handle(TQuery query);
}
public class DocumentSource : IQueryHandler<GetDocumentsQuery, GetDocumentsResult>
{
//..
public GetDocumentsResult Handle(GetDocumentsQuery query)
{
using (var context = CreateEFContext())
{
return
new GetDocumentsResult(
context
.Documents
.Where(c => c.Name.EndsWith("." + query.Format))
.ToArray());
}
}
//..
}
public class LoggingAwareQueryHandler<TQuery, TResult> : IQueryHandler<TQuery,TResult>
{
private readonly IQueryHandler<TQuery, TResult> decoratedHandler;
//..
public TResult Handle(TQuery query)
{
try
{
var result = decoratedHandler.Handle(query);
logger.LogSuccess(...);
return result;
}
catch (Exception ex)
{
logger.LogError(..., ex);
throw;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment