Snippets for http://fagner.co/2016/01/02/Defining-cross-cutting-concerns-using-the-MediatR-library/
Forked from fagnercarvalho/2016-01-02-Defining-cross-cutting-concerns-using-the-MediatR-library.md
Created
August 7, 2019 12:03
-
-
Save bdaniel7/159758c60ee1fa6ccfa5b86e978a02e3 to your computer and use it in GitHub Desktop.
2016-01-02-Defining-cross-cutting-concerns-using-the-MediatR-library
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 DatabaseHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse> where TRequest : IRequest<TResponse> | |
{ | |
private readonly IRequestHandler<TRequest, TResponse> inner; | |
private readonly Context db; | |
private readonly ILogger<TRequest, TResponse> logger; | |
public DatabaseHandler(IRequestHandler<TRequest, TResponse> inner, Context db, ILogger<TRequest, TResponse> logger) | |
{ | |
this.inner = inner; | |
this.db = db; | |
this.logger = logger; | |
} | |
public TResponse Handle(TRequest request) | |
{ | |
TResponse response; | |
using (var transaction = this.db.Database.BeginTransaction(IsolationLevel.ReadCommitted)) | |
{ | |
this.db.Database.Log = (log) => | |
{ | |
if (log != Environment.NewLine) | |
{ | |
this.logger.LogSql(log); | |
} | |
}; | |
response = this.inner.Handle(request); | |
this.db.SaveChanges(); | |
transaction.Commit(); | |
} | |
return response; | |
} | |
} |
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 ExceptionHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse> where TRequest : IRequest<TResponse> | |
{ | |
private readonly IRequestHandler<TRequest, TResponse> inner; | |
private readonly ILogger<TRequest, TResponse> logger; | |
public ExceptionHandler(IRequestHandler<TRequest, TResponse> inner, ILogger<TRequest, TResponse> logger) | |
{ | |
this.inner = inner; | |
this.logger = logger; | |
} | |
public TResponse Handle(TRequest request) | |
{ | |
var response = default(TResponse); | |
try | |
{ | |
response = this.inner.Handle(request); | |
} | |
catch (ValidationException) | |
{ | |
throw; | |
} | |
catch (Exception ex) | |
{ | |
this.logger.LogError(request, response, ex); | |
} | |
return response; | |
} | |
} |
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 Logger<TRequest, TResponse> : ILogger<TRequest, TResponse> where TRequest : IRequest<TResponse> | |
{ | |
public void LogInfo(TRequest request, TResponse response) | |
{ | |
this.Log(request, response); | |
} | |
public void LogError(TRequest request, TResponse response, Exception ex) | |
{ | |
this.Log(request, response, ex); | |
} | |
public void LogRequestInfo<TRequest, TResponse>(TRequest request) where TRequest : IRequest<TResponse> | |
{ | |
ExceptionlessClient.Default | |
.CreateLog("ExceptionLessTest", string.Format("[Request] {0} {1}", request.GetType()), "Info") | |
.AddObject(new RequestLogMessage<TRequest, TResponse> { Request = request }) | |
.Submit(); | |
} | |
public void LogResponseInfo<TResponse>(TResponse response) | |
{ | |
ExceptionlessClient.Default | |
.CreateLog("ExceptionLessTest", string.Format("[Response] {0} {1}", response.GetType()), "Info") | |
.AddObject(new ResponseLogMessage<TResponse> { Response = response }) | |
.Submit(); | |
} | |
public void LogSql(string sql) | |
{ | |
ExceptionlessClient.Default | |
.CreateLog("ExceptionLessTest", sql, "Info") | |
.AddObject(new SqlLogMessage { Sql = sql }) | |
.Submit(); | |
} | |
private void Log(TRequest request, TResponse response) | |
{ | |
this.Log(request, response, null); | |
} | |
private void Log(TRequest request, TResponse response, Exception ex) | |
{ | |
if (ex == null) | |
{ | |
// log info | |
ExceptionlessClient.Default.CreateLog("ExceptionLessTest", "Info") | |
.AddObject(new RequestResponseLogMessage<TRequest, TResponse> | |
{ | |
Request = request, | |
Response = response, | |
}) | |
.Submit(); | |
} | |
else | |
{ | |
// log error | |
ExceptionlessClient.Default.CreateLog("ExceptionLessTest", "Error") | |
.AddObject(new RequestResponseLogMessage<TRequest, TResponse> | |
{ | |
Request = request, | |
Response = response, | |
Exception = ex | |
}) | |
.Submit(); | |
} | |
} | |
} | |
public interface ILogger<TRequest, TResponse> where TRequest : IRequest<TResponse> | |
{ | |
void LogInfo(TRequest request, TResponse response); | |
void LogError(TRequest request, TResponse response, Exception ex); | |
void LogRequestInfo<TRequest, TResponse>(TRequest request) where TRequest : IRequest<TResponse>; | |
void LogResponseInfo<TResponse>(TResponse response); | |
void LogSql(string sql); | |
} |
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 LoggingHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse> where TRequest : IRequest<TResponse> | |
{ | |
private readonly IRequestHandler<TRequest, TResponse> inner; | |
private readonly ILogger<TRequest, TResponse> logger; | |
public LoggingHandler(IRequestHandler<TRequest, TResponse> inner, ILogger<TRequest, TResponse> logger) | |
{ | |
this.inner = inner; | |
this.logger = logger; | |
} | |
public TResponse Handle(TRequest request) | |
{ | |
this.logger.LogRequestInfo<TRequest, TResponse>(request); | |
var response = this.inner.Handle(request); | |
this.logger.LogResponseInfo(response); | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment