Created
June 22, 2018 14:09
-
-
Save JonathanLoscalzo/446efd16061c780cd4f142a7d5538481 to your computer and use it in GitHub Desktop.
an approach for add aspects (AOP) to C# service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ICommandHandler<TCommand> | |
{ | |
void Handle(TCommand command); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IQueryHandler<TQuery, TResult> | |
{ | |
TResult Handle(TQuery query); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
//.. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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