Skip to content

Instantly share code, notes, and snippets.

View wellingtonjhn's full-sized avatar

Wellington Nascimento wellingtonjhn

View GitHub Profile
@wellingtonjhn
wellingtonjhn / 7.Post.DomainNotification.CreateCustomerHandler.cs
Last active September 24, 2019 21:41
Handler with NotificationContext
public class CreateCustomerHandler : IRequestHandler<CreateCustomer, Guid>
{
private readonly NotificationContext _notificationContext;
private readonly ICustomerRepository _customerRepository;
public CreateCustomerHandler(
NotificationContext notificationContext,
ICustomerRepository customerRepository)
{
_notificationContext = notificationContext;
@wellingtonjhn
wellingtonjhn / 4.Post.DomainNotification.Entity.cs
Last active November 25, 2018 22:00
Entity with Validator Sample
public abstract class Entity
{
public Guid Id { get; protected set; }
public bool Valid { get; private set; }
public bool Invalid => !Valid;
public ValidationResult ValidationResult { get; private set; }
public bool Validate<TModel>(TModel model, AbstractValidator<TModel> validator)
{
ValidationResult = validator.Validate(model);
@wellingtonjhn
wellingtonjhn / 2.Post.DomainNotification.Notification.cs
Last active May 31, 2020 03:39
Domain Notification Structures Sample
public class Notification
{
public string Key { get; }
public string Message { get; }
public Notification(string key, string message)
{
Key = key;
Message = message;
}
@wellingtonjhn
wellingtonjhn / 1.Post.DomainNotification.CustomerWithExceptions.cs
Created November 16, 2018 14:01
Entity validation with Exceptions Sample
public class Customer
{
public string Name { get; }
public string Email { get; }
public Customer(string name, string email)
{
if (string.IsNullOrEmpty(name))
{
throw new InvalidOperationException("Name cannot be empty");
@wellingtonjhn
wellingtonjhn / 6.Post.GlobalExceptionHandler.Controller.cs
Created October 30, 2018 18:12
Controller without any try...catch statement
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly FakeData _data;
public ValuesController(FakeData data)
{
_data = data;
}
@wellingtonjhn
wellingtonjhn / 3.Post.GlobalExceptionHandler.GlobalExceptionHandlerMiddleware.cs
Last active November 1, 2018 05:24
Custom Global Exception Handler MIddleware
public class GlobalExceptionHandlerMiddleware : IMiddleware
{
private readonly ILogger<GlobalExceptionHandlerMiddleware> _logger;
public GlobalExceptionHandlerMiddleware(ILogger<GlobalExceptionHandlerMiddleware> logger)
{
_logger = logger;
}
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
@wellingtonjhn
wellingtonjhn / 1.Post.GlobalExceptionHandler.BuiltInMiddlewareExtension.cs
Last active November 1, 2018 05:23
Built-in UseExceptionHandler extension method
public static class ExceptionHandlerExtensions
{
public static void UseGlobalExceptionHandler(this IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
var exceptionHandlerFeature = context.Features.Get<IExceptionHandlerFeature>();
@wellingtonjhn
wellingtonjhn / 8.Post.SettingsValidation.Startup.cs
Created September 8, 2018 06:04
Startup ConfigurationSettingsValidationStartupFilter
public class Startup
{
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
services.AddSingleton(Configuration);
services.AddTransient<IStartupFilter, ConfigurationSettingValidationStartupFilter>();
public class ConfigurationSettingValidationStartupFilter : IStartupFilter
{
private readonly IEnumerable<ConfigurationSettings> _configurationSettings;
private readonly ILogger<ConfigurationSettingValidationStartupFilter> _logger;
public ConfigurationSettingValidationStartupFilter(
IEnumerable<ConfigurationSettings> configurationSettings,
ILogger<ConfigurationSettingValidationStartupFilter> logger)
{
_configurationSettings = configurationSettings;
@wellingtonjhn
wellingtonjhn / 1.Post.SettingsValidation.ConfigurationSettings.cs
Created August 30, 2018 06:15
MySettings implementation with Validation using Data Annotations
public abstract class ConfigurationSettings
{
public List<ValidationResult> ValidationResult = new List<ValidationResult>();
public bool Validate() => Validator.TryValidateObject(
this,
new ValidationContext(this),
ValidationResult,
true);
}