Use file-scoped namespaces

This commit is contained in:
Sumit Ghosh 2021-08-12 18:56:38 +05:30
parent 5157b01e81
commit 76ddee7756
31 changed files with 955 additions and 1150 deletions

View File

@ -1,13 +1,7 @@
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Auth.Server;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Auth.Server
public class AuthorizationHeaderParameterOperationFilter : IOperationFilter
{
public class AuthorizationHeaderParameterOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
var filterPipeline = context.ApiDescription.ActionDescriptor.FilterDescriptors;
@ -29,5 +23,4 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Auth.Server
});
}
}
}
}

View File

@ -1,7 +1,7 @@
namespace Microsoft.eShopOnContainers.Services.Basket.API
namespace Microsoft.eShopOnContainers.Services.Basket.API;
public class BasketSettings
{
public class BasketSettings
{
public string ConnectionString { get; set; }
}
}

View File

@ -1,23 +1,10 @@
using Basket.API.IntegrationEvents.Events;
using Basket.API.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
using Microsoft.Extensions.Logging;
using System;
using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
[Route("api/v1/[controller]")]
[Authorize]
[ApiController]
public class BasketController : ControllerBase
{
[Route("api/v1/[controller]")]
[Authorize]
[ApiController]
public class BasketController : ControllerBase
{
private readonly IBasketRepository _repository;
private readonly IIdentityService _identityService;
private readonly IEventBus _eventBus;
@ -99,5 +86,4 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
{
await _repository.DeleteBasketAsync(id);
}
}
}

View File

@ -1,13 +1,11 @@
using Microsoft.AspNetCore.Mvc;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
public class HomeController : Controller
{
public class HomeController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return new RedirectResult("~/swagger");
}
}
}

View File

@ -1,14 +1,6 @@
using Grpc.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Threading.Tasks;
namespace GrpcBasket
namespace GrpcBasket;
public class BasketService : Basket.BasketBase
{
public class BasketService : Basket.BasketBase
{
private readonly IBasketRepository _repository;
private readonly ILogger<BasketService> _logger;
@ -98,5 +90,4 @@ namespace GrpcBasket
return response;
}
}
}

View File

@ -1,14 +1,11 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Basket.API.Infrastructure.ActionResults;
namespace Basket.API.Infrastructure.ActionResults
public class InternalServerErrorObjectResult : ObjectResult
{
public class InternalServerErrorObjectResult : ObjectResult
{
public InternalServerErrorObjectResult(object error)
: base(error)
{
StatusCode = StatusCodes.Status500InternalServerError;
}
}
}

View File

@ -1,12 +1,7 @@
using System;
namespace Basket.API.Infrastructure.Exceptions;
namespace Basket.API.Infrastructure.Exceptions
public class BasketDomainException : Exception
{
/// <summary>
/// Exception type for app exceptions
/// </summary>
public class BasketDomainException : Exception
{
public BasketDomainException()
{ }
@ -17,5 +12,5 @@ namespace Basket.API.Infrastructure.Exceptions
public BasketDomainException(string message, Exception innerException)
: base(message, innerException)
{ }
}
}

View File

@ -1,10 +1,7 @@
using Microsoft.AspNetCore.Builder;
using System;
namespace Basket.API.Infrastructure.Middlewares;
namespace Basket.API.Infrastructure.Middlewares
public static class FailingMiddlewareAppBuilderExtensions
{
public static class FailingMiddlewareAppBuilderExtensions
{
public static IApplicationBuilder UseFailingMiddleware(this IApplicationBuilder builder)
{
return UseFailingMiddleware(builder, null);
@ -16,5 +13,5 @@ namespace Basket.API.Infrastructure.Middlewares
builder.UseMiddleware<FailingMiddleware>(options);
return builder;
}
}
}

View File

@ -1,17 +1,7 @@
using Basket.API.Infrastructure.ActionResults;
using Basket.API.Infrastructure.Exceptions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Net;
namespace Basket.API.Infrastructure.Filters;
namespace Basket.API.Infrastructure.Filters
public partial class HttpGlobalExceptionFilter : IExceptionFilter
{
public partial class HttpGlobalExceptionFilter : IExceptionFilter
{
private readonly IWebHostEnvironment env;
private readonly ILogger<HttpGlobalExceptionFilter> logger;
@ -54,5 +44,4 @@ namespace Basket.API.Infrastructure.Filters
}
context.ExceptionHandled = true;
}
}
}

View File

@ -1,9 +1,9 @@
namespace Basket.API.Infrastructure.Filters
namespace Basket.API.Infrastructure.Filters;
public class JsonErrorResponse
{
public class JsonErrorResponse
{
public string[] Messages { get; set; }
public object DeveloperMessage { get; set; }
}
}

View File

@ -1,11 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System.Linq;
namespace Basket.API.Infrastructure.Filters;
namespace Basket.API.Infrastructure.Filters
public class ValidateModelStateFilter : ActionFilterAttribute
{
public class ValidateModelStateFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
if (context.ModelState.IsValid)
@ -26,5 +22,5 @@ namespace Basket.API.Infrastructure.Filters
context.Result = new BadRequestObjectResult(json);
}
}
}

View File

@ -1,13 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
using System.Linq;
namespace Basket.API.Infrastructure.Filters;
namespace Basket.API.Infrastructure.Filters
public class AuthorizeCheckOperationFilter : IOperationFilter
{
public class AuthorizeCheckOperationFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
// Check for authorize attribute
@ -32,5 +26,4 @@ namespace Basket.API.Infrastructure.Filters
}
};
}
}
}

View File

@ -1,19 +1,13 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Basket.API.Infrastructure.Middlewares;
namespace Basket.API.Infrastructure.Middlewares
public class FailingMiddleware
{
public class FailingMiddleware
{
private readonly RequestDelegate _next;
private bool _mustFail;
private readonly FailingOptions _options;
private readonly ILogger _logger;
private readonly Microsoft.Extensions.Logging.ILogger _logger;
public FailingMiddleware(RequestDelegate next, ILogger<FailingMiddleware> logger, FailingOptions options)
public FailingMiddleware(RequestDelegate next, Microsoft.Extensions.Logging.ILogger<FailingMiddleware> logger, FailingOptions options)
{
_next = next;
_options = options;
@ -91,5 +85,4 @@ namespace Basket.API.Infrastructure.Middlewares
(_options.EndpointPaths.Any(x => x == rpath)
|| _options.EndpointPaths.Count == 0);
}
}
}

View File

@ -1,12 +1,10 @@
using System.Collections.Generic;
namespace Basket.API.Infrastructure.Middlewares;
namespace Basket.API.Infrastructure.Middlewares
public class FailingOptions
{
public class FailingOptions
{
public string ConfigPath = "/Failing";
public List<string> EndpointPaths { get; set; } = new List<string>();
public List<string> NotFilteredPaths { get; set; } = new List<string>();
}
}

View File

@ -1,11 +1,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using System;
namespace Basket.API.Infrastructure.Middlewares;
namespace Basket.API.Infrastructure.Middlewares
public class FailingStartupFilter : IStartupFilter
{
public class FailingStartupFilter : IStartupFilter
{
private readonly Action<FailingOptions> _options;
public FailingStartupFilter(Action<FailingOptions> optionsAction)
{
@ -20,5 +16,5 @@ namespace Basket.API.Infrastructure.Middlewares
next(app);
};
}
}
}

View File

@ -1,11 +1,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Basket.API.Infrastructure.Middlewares;
namespace Basket.API.Infrastructure.Middlewares
public static class WebHostBuildertExtensions
{
public static class WebHostBuildertExtensions
{
public static IWebHostBuilder UseFailing(this IWebHostBuilder builder, Action<FailingOptions> options)
{
builder.ConfigureServices(services =>
@ -14,5 +10,5 @@ namespace Basket.API.Infrastructure.Middlewares
});
return builder;
}
}
}

View File

@ -1,15 +1,6 @@
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text.Json;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Repositories
namespace Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Repositories;
public class RedisBasketRepository : IBasketRepository
{
public class RedisBasketRepository : IBasketRepository
{
private readonly ILogger<RedisBasketRepository> _logger;
private readonly ConnectionMultiplexer _redis;
private readonly IDatabase _database;
@ -69,5 +60,4 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Reposit
var endpoint = _redis.GetEndPoints();
return _redis.GetServer(endpoint.First());
}
}
}

View File

@ -1,15 +1,7 @@
using Basket.API.IntegrationEvents.Events;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.Extensions.Logging;
using Serilog.Context;
using System;
using System.Threading.Tasks;
namespace Basket.API.IntegrationEvents.EventHandling;
namespace Basket.API.IntegrationEvents.EventHandling
public class OrderStartedIntegrationEventHandler : IIntegrationEventHandler<OrderStartedIntegrationEvent>
{
public class OrderStartedIntegrationEventHandler : IIntegrationEventHandler<OrderStartedIntegrationEvent>
{
private readonly IBasketRepository _repository;
private readonly ILogger<OrderStartedIntegrationEventHandler> _logger;
@ -30,8 +22,8 @@ namespace Basket.API.IntegrationEvents.EventHandling
await _repository.DeleteBasketAsync(@event.UserId.ToString());
}
}
}
}

View File

@ -1,16 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.Extensions.Logging;
using Serilog.Context;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling;
namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling
public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>
{
public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler<ProductPriceChangedIntegrationEvent>
{
private readonly ILogger<ProductPriceChangedIntegrationEventHandler> _logger;
private readonly IBasketRepository _repository;
@ -59,6 +50,4 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
await _repository.UpdateBasketAsync(basket);
}
}
}
}

View File

@ -1,15 +1,13 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Basket.API.IntegrationEvents.Events;
namespace Basket.API.IntegrationEvents.Events
// Integration Events notes:
// An Event is “something that has happened in the past”, therefore its name has to be
// An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
public record OrderStartedIntegrationEvent : IntegrationEvent
{
// Integration Events notes:
// An Event is “something that has happened in the past”, therefore its name has to be
// An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
public record OrderStartedIntegrationEvent : IntegrationEvent
{
public string UserId { get; init; }
public OrderStartedIntegrationEvent(string userId)
=> UserId = userId;
}
}

View File

@ -1,12 +1,10 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events
// Integration Events notes:
// An Event is “something that has happened in the past”, therefore its name has to be
// An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
public record ProductPriceChangedIntegrationEvent : IntegrationEvent
{
// Integration Events notes:
// An Event is “something that has happened in the past”, therefore its name has to be
// An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
public record ProductPriceChangedIntegrationEvent : IntegrationEvent
{
public int ProductId { get; private init; }
public decimal NewPrice { get; private init; }
@ -19,5 +17,5 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
NewPrice = newPrice;
OldPrice = oldPrice;
}
}
}

View File

@ -1,11 +1,7 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using System;
namespace Basket.API.IntegrationEvents.Events;
namespace Basket.API.IntegrationEvents.Events
public record UserCheckoutAcceptedIntegrationEvent : IntegrationEvent
{
public record UserCheckoutAcceptedIntegrationEvent : IntegrationEvent
{
public string UserId { get; }
public string UserName { get; }
@ -60,5 +56,4 @@ namespace Basket.API.IntegrationEvents.Events
RequestId = requestId;
}
}
}

View File

@ -1,9 +1,6 @@
using System;
namespace Basket.API.Model
namespace Basket.API.Model;
public class BasketCheckout
{
public class BasketCheckout
{
public string City { get; set; }
public string Street { get; set; }
@ -27,6 +24,4 @@ namespace Basket.API.Model
public string Buyer { get; set; }
public Guid RequestId { get; set; }
}
}

View File

@ -1,10 +1,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
public class BasketItem : IValidatableObject
{
public class BasketItem : IValidatableObject
{
public string Id { get; set; }
public int ProductId { get; set; }
public string ProductName { get; set; }
@ -23,5 +20,4 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
return results;
}
}
}

View File

@ -1,9 +1,6 @@
using System.Collections.Generic;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model;
public class CustomerBasket
{
public class CustomerBasket
{
public string BuyerId { get; set; }
public List<BasketItem> Items { get; set; } = new List<BasketItem>();
@ -17,5 +14,5 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
{
BuyerId = customerId;
}
}
}

View File

@ -1,13 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Model
public interface IBasketRepository
{
public interface IBasketRepository
{
Task<CustomerBasket> GetBasketAsync(string customerId);
IEnumerable<string> GetUsers();
Task<CustomerBasket> UpdateBasketAsync(CustomerBasket basket);
Task<bool> DeleteBasketAsync(string id);
}
}

View File

@ -1,18 +1,4 @@
using Basket.API.Infrastructure.Middlewares;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.eShopOnContainers.Services.Basket.API;
using Microsoft.Extensions.Configuration;
using Azure.Identity;
using Serilog;
using System;
using System.IO;
using System.Net;
using Azure.Core;
var configuration = GetConfiguration();
var configuration = GetConfiguration();
Log.Logger = CreateSerilogLogger(configuration);

View File

@ -1,7 +1,7 @@
namespace Microsoft.eShopOnContainers.Services.Basket.API.Services
namespace Microsoft.eShopOnContainers.Services.Basket.API.Services;
public interface IIdentityService
{
public interface IIdentityService
{
string GetUserIdentity();
}
}

View File

@ -1,11 +1,7 @@

using Microsoft.AspNetCore.Http;
using System;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Services;
namespace Microsoft.eShopOnContainers.Services.Basket.API.Services
public class IdentityService : IIdentityService
{
public class IdentityService : IIdentityService
{
private IHttpContextAccessor _context;
public IdentityService(IHttpContextAccessor context)
@ -17,5 +13,5 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Services
{
return _context.HttpContext.User.FindFirst("sub").Value;
}
}
}

View File

@ -1,43 +1,7 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Basket.API.Infrastructure.Filters;
using Basket.API.IntegrationEvents.EventHandling;
using Basket.API.IntegrationEvents.Events;
using GrpcBasket;
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.ServiceBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
using Microsoft.eShopOnContainers.Services.Basket.API.Controllers;
using Microsoft.eShopOnContainers.Services.Basket.API.Infrastructure.Repositories;
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling;
using Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Events;
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
using Microsoft.eShopOnContainers.Services.Basket.API.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using RabbitMQ.Client;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.IO;
namespace Microsoft.eShopOnContainers.Services.Basket.API;
namespace Microsoft.eShopOnContainers.Services.Basket.API
public class Startup
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
@ -66,7 +30,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
services.AddSwaggerGen(options =>
{
options.DescribeAllEnumsAsStrings();
options.SwaggerDoc("v1", new OpenApiInfo
{
Title = "eShopOnContainers - Basket HTTP API",
@ -323,10 +286,10 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
eventBus.Subscribe<ProductPriceChangedIntegrationEvent, ProductPriceChangedIntegrationEventHandler>();
eventBus.Subscribe<OrderStartedIntegrationEvent, OrderStartedIntegrationEventHandler>();
}
}
}
public static class CustomExtensionMethods
{
public static class CustomExtensionMethods
{
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
{
var hcBuilder = services.AddHealthChecks();
@ -359,5 +322,4 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
return services;
}
}
}

View File

@ -1,10 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
namespace Microsoft.eShopOnContainers.Services.Basket.API;
namespace Microsoft.eShopOnContainers.Services.Basket.API
internal class TestHttpResponseTrailersFeature : IHttpResponseTrailersFeature
{
internal class TestHttpResponseTrailersFeature : IHttpResponseTrailersFeature
{
public IHeaderDictionary Trailers { get; set; }
}
}