Skip to content

Code cleanup and refactoring #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/SampleProject.API/Configuration/CorrelationMiddleware.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Microsoft.AspNetCore.Http;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace SampleProject.API.Configuration
{
Expand All @@ -13,19 +13,19 @@ internal class CorrelationMiddleware
public CorrelationMiddleware(
RequestDelegate next)
{
this._next = next;
_next = next;
}

public async Task Invoke(HttpContext context)
{
var correlationId = Guid.NewGuid();
Guid correlationId = Guid.NewGuid();

if (context.Request != null)
{
context.Request.Headers.Add(CorrelationHeaderKey, correlationId.ToString());
}

await this._next.Invoke(context);
await _next.Invoke(context);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;
using SampleProject.Application.Configuration;
using System;
using System.Linq;

namespace SampleProject.API.Configuration
{
Expand Down
12 changes: 6 additions & 6 deletions src/SampleProject.API/Configuration/SwaggerExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace SampleProject.API.Configuration
{
Expand All @@ -19,9 +19,9 @@ internal static IServiceCollection AddSwaggerDocumentation(this IServiceCollecti
Description = "Sample .NET Core REST API CQRS implementation with raw SQL and DDD using Clean Architecture.",
});

var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
var commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML";
var commentsFile = Path.Combine(baseDirectory, commentsFileName);
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string commentsFileName = Assembly.GetExecutingAssembly().GetName().Name + ".XML";
string commentsFile = Path.Combine(baseDirectory, commentsFileName);
options.IncludeXmlComments(commentsFile);
});

Expand Down
16 changes: 8 additions & 8 deletions src/SampleProject.API/Customers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Net;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SampleProject.Application.Customers;
using SampleProject.Application.Customers.RegisterCustomer;
using System.Net;
using System.Threading.Tasks;

namespace SampleProject.API.Customers
{
Expand All @@ -15,7 +15,7 @@ public class CustomersController : Controller

public CustomersController(IMediator mediator)
{
this._mediator = mediator;
_mediator = mediator;
}

/// <summary>
Expand All @@ -24,11 +24,11 @@ public CustomersController(IMediator mediator)
[Route("")]
[HttpPost]
[ProducesResponseType(typeof(CustomerDto), (int)HttpStatusCode.Created)]
public async Task<IActionResult> RegisterCustomer([FromBody]RegisterCustomerRequest request)
public async Task<IActionResult> RegisterCustomer([FromBody] RegisterCustomerRequest request)
{
var customer = await _mediator.Send(new RegisterCustomerCommand(request.Email, request.Name));
CustomerDto customer = await _mediator.Send(new RegisterCustomerCommand(request.Email, request.Name));

return Created(string.Empty, customer);
}
return Created(string.Empty, customer);
}
}
}
4 changes: 2 additions & 2 deletions src/SampleProject.API/Orders/CustomerOrderRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using SampleProject.Application.Orders;
using SampleProject.Application.Orders;
using System.Collections.Generic;

namespace SampleProject.API.Orders
{
Expand Down
36 changes: 18 additions & 18 deletions src/SampleProject.API/Orders/CustomerOrdersController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using MediatR;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using SampleProject.Application.Orders.ChangeCustomerOrder;
using SampleProject.Application.Orders.GetCustomerOrderDetails;
using SampleProject.Application.Orders.GetCustomerOrders;
using SampleProject.Application.Orders.PlaceCustomerOrder;
using SampleProject.Application.Orders.RemoveCustomerOrder;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

namespace SampleProject.API.Orders
{
Expand All @@ -20,7 +20,7 @@ public class CustomerOrdersController : Controller

public CustomerOrdersController(IMediator mediator)
{
this._mediator = mediator;
_mediator = mediator;
}

/// <summary>
Expand All @@ -33,7 +33,7 @@ public CustomerOrdersController(IMediator mediator)
[ProducesResponseType(typeof(List<OrderDto>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetCustomerOrders(Guid customerId)
{
var orders = await _mediator.Send(new GetCustomerOrdersQuery(customerId));
List<OrderDto> orders = await _mediator.Send(new GetCustomerOrdersQuery(customerId));

return Ok(orders);
}
Expand All @@ -46,9 +46,9 @@ public async Task<IActionResult> GetCustomerOrders(Guid customerId)
[HttpGet]
[ProducesResponseType(typeof(OrderDetailsDto), (int)HttpStatusCode.OK)]
public async Task<IActionResult> GetCustomerOrderDetails(
[FromRoute]Guid orderId)
[FromRoute] Guid orderId)
{
var orderDetails = await _mediator.Send(new GetCustomerOrderDetailsQuery(orderId));
OrderDetailsDto orderDetails = await _mediator.Send(new GetCustomerOrderDetailsQuery(orderId));

return Ok(orderDetails);
}
Expand All @@ -63,12 +63,12 @@ public async Task<IActionResult> GetCustomerOrderDetails(
[HttpPost]
[ProducesResponseType((int)HttpStatusCode.Created)]
public async Task<IActionResult> AddCustomerOrder(
[FromRoute]Guid customerId,
[FromBody]CustomerOrderRequest request)
[FromRoute] Guid customerId,
[FromBody] CustomerOrderRequest request)
{
await _mediator.Send(new PlaceCustomerOrderCommand(customerId, request.Products, request.Currency));
await _mediator.Send(new PlaceCustomerOrderCommand(customerId, request.Products, request.Currency));

return Created(string.Empty, null);
return Created(string.Empty, null);
}

/// <summary>
Expand All @@ -81,9 +81,9 @@ public async Task<IActionResult> AddCustomerOrder(
[HttpPut]
[ProducesResponseType((int)HttpStatusCode.OK)]
public async Task<IActionResult> ChangeCustomerOrder(
[FromRoute]Guid customerId,
[FromRoute]Guid orderId,
[FromBody]CustomerOrderRequest request)
[FromRoute] Guid customerId,
[FromRoute] Guid orderId,
[FromBody] CustomerOrderRequest request)
{
await _mediator.Send(new ChangeCustomerOrderCommand(customerId, orderId, request.Products, request.Currency));

Expand All @@ -99,8 +99,8 @@ public async Task<IActionResult> ChangeCustomerOrder(
[HttpDelete]
[ProducesResponseType(typeof(List<OrderDto>), (int)HttpStatusCode.OK)]
public async Task<IActionResult> RemoveCustomerOrder(
[FromRoute]Guid customerId,
[FromRoute]Guid orderId)
[FromRoute] Guid customerId,
[FromRoute] Guid orderId)
{
await _mediator.Send(new RemoveCustomerOrderCommand(customerId, orderId));

Expand Down
8 changes: 5 additions & 3 deletions src/SampleProject.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public static void Main(string[] args)
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class BusinessRuleValidationExceptionProblemDetails : Microsoft.AspNetCor
{
public BusinessRuleValidationExceptionProblemDetails(BusinessRuleValidationException exception)
{
this.Title = "Business rule validation error";
this.Status = StatusCodes.Status409Conflict;
this.Detail = exception.Details;
this.Type = "https://somedomain/business-rule-validation-error";
Title = "Business rule validation error";
Status = StatusCodes.Status409Conflict;
Detail = exception.Details;
Type = "https://somedomain/business-rule-validation-error";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class InvalidCommandProblemDetails : Microsoft.AspNetCore.Mvc.ProblemDeta
{
public InvalidCommandProblemDetails(InvalidCommandException exception)
{
this.Title = exception.Message;
this.Status = StatusCodes.Status400BadRequest;
this.Detail = exception.Details;
this.Type = "https://somedomain/validation-error";
Title = exception.Message;
Status = StatusCodes.Status400BadRequest;
Detail = exception.Details;
Type = "https://somedomain/validation-error";
}
}
}
30 changes: 15 additions & 15 deletions src/SampleProject.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Linq;
using Hellang.Middleware.ProblemDetails;
using Hellang.Middleware.ProblemDetails;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand All @@ -10,19 +8,21 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SampleProject.API.Configuration;
using SampleProject.Application.Configuration.Validation;
using SampleProject.API.SeedWork;
using SampleProject.Application.Configuration;
using SampleProject.Application.Configuration.Emails;
using SampleProject.Application.Configuration.Validation;
using SampleProject.Domain.SeedWork;
using SampleProject.Infrastructure;
using SampleProject.Infrastructure.Caching;
using Serilog;
using Serilog.Formatting.Compact;
using System;
using System.Linq;

[assembly: UserSecretsId("54e8eb06-aaa1-4fff-9f05-3ced1cb623c2")]
namespace SampleProject.API
{
{
public class Startup
{
private readonly IConfiguration _configuration;
Expand All @@ -36,7 +36,7 @@ public Startup(IWebHostEnvironment env)
_logger = ConfigureLogger();
_logger.Information("Logger configured");

this._configuration = new ConfigurationBuilder()
_configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json")
.AddJsonFile($"hosting.{env.EnvironmentName}.json")
Expand All @@ -47,7 +47,7 @@ public Startup(IWebHostEnvironment env)
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddControllers();

services.AddMemoryCache();

services.AddSwaggerDocumentation();
Expand All @@ -57,20 +57,20 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
x.Map<InvalidCommandException>(ex => new InvalidCommandProblemDetails(ex));
x.Map<BusinessRuleValidationException>(ex => new BusinessRuleValidationExceptionProblemDetails(ex));
});


services.AddHttpContextAccessor();
var serviceProvider = services.BuildServiceProvider();
ServiceProvider serviceProvider = services.BuildServiceProvider();

IExecutionContextAccessor executionContextAccessor = new ExecutionContextAccessor(serviceProvider.GetService<IHttpContextAccessor>());

var children = this._configuration.GetSection("Caching").GetChildren();
var cachingConfiguration = children.ToDictionary(child => child.Key, child => TimeSpan.Parse(child.Value));
var emailsSettings = _configuration.GetSection("EmailsSettings").Get<EmailsSettings>();
var memoryCache = serviceProvider.GetService<IMemoryCache>();
System.Collections.Generic.IEnumerable<IConfigurationSection> children = _configuration.GetSection("Caching").GetChildren();
System.Collections.Generic.Dictionary<string, TimeSpan> cachingConfiguration = children.ToDictionary(child => child.Key, child => TimeSpan.Parse(child.Value));
EmailsSettings emailsSettings = _configuration.GetSection("EmailsSettings").Get<EmailsSettings>();
IMemoryCache memoryCache = serviceProvider.GetService<IMemoryCache>();
return ApplicationStartup.Initialize(
services,
this._configuration[OrdersConnectionString],
services,
_configuration[OrdersConnectionString],
new MemoryCacheStore(memoryCache, cachingConfiguration),
null,
emailsSettings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public class CommandBase : ICommand

public CommandBase()
{
this.Id = Guid.NewGuid();
Id = Guid.NewGuid();
}

protected CommandBase(Guid id)
{
this.Id = id;
Id = id;
}
}

Expand All @@ -23,12 +23,12 @@ public abstract class CommandBase<TResult> : ICommand<TResult>

protected CommandBase()
{
this.Id = Guid.NewGuid();
Id = Guid.NewGuid();
}

protected CommandBase(Guid id)
{
this.Id = id;
Id = id;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using MediatR;
using MediatR;
using System;

namespace SampleProject.Application.Configuration.Commands
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class InternalCommandBase : ICommand

protected InternalCommandBase(Guid id)
{
this.Id = id;
Id = id;
}
}

Expand All @@ -18,12 +18,12 @@ public abstract class InternalCommandBase<TResult> : ICommand<TResult>

protected InternalCommandBase()
{
this.Id = Guid.NewGuid();
Id = Guid.NewGuid();
}

protected InternalCommandBase(Guid id)
{
this.Id = id;
Id = id;
}
}
}
Loading