From 53e0d1ee96b19a555a1ab9745fbcdfae418d13dc Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Tue, 21 Mar 2017 14:25:49 -0400 Subject: [PATCH 1/2] Refactor and update to C# 7 constructs Controllers/BasketController: Use Expression Bodied Members. Delete() should not be 'fire and forget' Controllers/HomeController: Expression bodied constructor IntegrationEvents/EventHandling/ProductPriceChangedIntegratedEventHandler: Use valueTask to minimize allocations for 'fast path' tasks. One might believe that most users would not have the changing item in the basket at any given time. Using ValueTask instead of Task, this minimizes allocations to only when async work must actually take place. IntegrationEvents/EventHandling/ProductPriceChangedIntegratedEvent: Make the properties ReadOnly Model/Basket: Use property initializer Model/RedisBasketRepository: Remove redundant ToString() Use elvis operator instead of statement null check Startup: remove redundant ToString() --- .../Basket.API/Controllers/BasketController.cs | 9 ++++----- .../Basket.API/Controllers/HomeController.cs | 6 ++---- ...ProductPriceChangedIntegrationEventHandler.cs | 16 ++++++++++------ .../ProductPriceChangedIntegrationEvent.cs | 6 +++--- src/Services/Basket/Basket.API/Model/Basket.cs | 7 ++----- .../Basket.API/Model/RedisBasketRepository.cs | 10 ++-------- src/Services/Basket/Basket.API/Startup.cs | 2 +- 7 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index 2cb3fcafe..d8fe740b7 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -18,10 +18,9 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers { private IBasketRepository _repository; - public BasketController(IBasketRepository repository) - { + public BasketController(IBasketRepository repository) => _repository = repository; - } + // GET api/values/5 [HttpGet("{id}")] public async Task Get(string id) @@ -42,9 +41,9 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers // DELETE api/values/5 [HttpDelete("{id}")] - public void Delete(string id) + public Task Delete(string id) { - _repository.DeleteBasket(id); + return _repository.DeleteBasket(id); } } } diff --git a/src/Services/Basket/Basket.API/Controllers/HomeController.cs b/src/Services/Basket/Basket.API/Controllers/HomeController.cs index 5cf7ab855..f1c054adf 100644 --- a/src/Services/Basket/Basket.API/Controllers/HomeController.cs +++ b/src/Services/Basket/Basket.API/Controllers/HomeController.cs @@ -11,9 +11,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers public class HomeController : Controller { // GET: // - public IActionResult Index() - { - return new RedirectResult("~/swagger/ui"); - } + public IActionResult Index() => + new RedirectResult("~/swagger/ui"); } } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs index 84d874271..fe3a04cfd 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/EventHandling/ProductPriceChangedIntegrationEventHandler.cs @@ -4,6 +4,7 @@ using Microsoft.eShopOnContainers.Services.Basket.API.Model; using System.Linq; using System.Threading.Tasks; + namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.EventHandling { public class ProductPriceChangedIntegrationEventHandler : IIntegrationEventHandler @@ -20,26 +21,29 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even foreach (var id in userIds) { var basket = await _repository.GetBasket(id); - await UpdateBasket(@event.ProductId, @event.NewPrice, basket); + await UpdateBasket(@event.ProductId, @event.NewPrice, basket); } } - private async Task UpdateBasket(int productId, decimal newPrice, CustomerBasket basket) + private ValueTask UpdateBasket(int productId, decimal newPrice, CustomerBasket basket) { var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == productId).ToList(); if (itemsToUpdate != null) { foreach (var item in itemsToUpdate) { - if(item.UnitPrice != newPrice) - { + if (item.UnitPrice != newPrice) + { var originalPrice = item.UnitPrice; item.UnitPrice = newPrice; item.OldUnitPrice = originalPrice; } } - await _repository.UpdateBasket(basket); - } + return new ValueTask(_repository.UpdateBasket(basket)); + } else + { + return new ValueTask(default(CustomerBasket)); + } } } } diff --git a/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs b/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs index 87d2e9e81..249650727 100644 --- a/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs +++ b/src/Services/Basket/Basket.API/IntegrationEvents/Events/ProductPriceChangedIntegrationEvent.cs @@ -10,11 +10,11 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even // An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems. public class ProductPriceChangedIntegrationEvent : IntegrationEvent { - public int ProductId { get; private set; } + public int ProductId { get; } - public decimal NewPrice { get; private set; } + public decimal NewPrice { get; } - public decimal OldPrice { get; private set; } + public decimal OldPrice { get; } public ProductPriceChangedIntegrationEvent(int productId, decimal newPrice, decimal oldPrice) { diff --git a/src/Services/Basket/Basket.API/Model/Basket.cs b/src/Services/Basket/Basket.API/Model/Basket.cs index d07c710e7..e5b0910f7 100644 --- a/src/Services/Basket/Basket.API/Model/Basket.cs +++ b/src/Services/Basket/Basket.API/Model/Basket.cs @@ -8,12 +8,9 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model public class CustomerBasket { public string BuyerId { get; set; } - public List Items { get; set; } + public List Items { get; set; } = new List(); - public CustomerBasket(string customerId) - { + public CustomerBasket(string customerId) => BuyerId = customerId; - Items = new List(); - } } } diff --git a/src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs b/src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs index fc5c256f8..424468a30 100644 --- a/src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs +++ b/src/Services/Basket/Basket.API/Model/RedisBasketRepository.cs @@ -28,7 +28,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model public async Task DeleteBasket(string id) { var database = await GetDatabase(); - return await database.KeyDeleteAsync(id.ToString()); + return await database.KeyDeleteAsync(id); } public async Task> GetUsers() @@ -36,11 +36,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model var server = await GetServer(); IEnumerable data = server.Keys(); - if (data == null) - { - return null; - } - return data.Select(k => k.ToString()); + return data?.Select(k => k.ToString()); } public async Task GetBasket(string customerId) @@ -99,8 +95,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Model _logger.LogInformation($"Connecting to database {_settings.ConnectionString} at IP {ips.First().ToString()}"); _redis = await ConnectionMultiplexer.ConnectAsync(ips.First().ToString()); } - - } } diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index db72792bd..7e7351399 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -114,7 +114,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API var identityUrl = Configuration.GetValue("IdentityUrl"); app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions { - Authority = identityUrl.ToString(), + Authority = identityUrl, ScopeName = "basket", RequireHttpsMetadata = false }); From 9dbabb4f50abb13b2bb5a3eaa2582ac206cab571 Mon Sep 17 00:00:00 2001 From: Bill Wagner Date: Thu, 23 Mar 2017 11:59:02 -0400 Subject: [PATCH 2/2] Add executable permission to bash script --- cli-mac/build-bits.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 cli-mac/build-bits.sh diff --git a/cli-mac/build-bits.sh b/cli-mac/build-bits.sh old mode 100644 new mode 100755