From e66b6a2402a34ded2c6d55a6644a4def084b89bd Mon Sep 17 00:00:00 2001 From: etomas Date: Mon, 27 Feb 2017 10:35:31 +0100 Subject: [PATCH] ArgumentNullException handling using throw expressions Solves #64 --- .../Identity.API/Services/ProfileService.cs | 6 ++---- .../Commands/CreateOrderCommandHandler.cs | 14 ++----------- .../Application/Queries/OrderQueries.cs | 2 +- .../Controllers/OrdersController.cs | 20 +++---------------- .../Services/IdentityService.cs | 7 +------ .../AggregatesModel/BuyerAggregate/Buyer.cs | 7 +------ .../BuyerAggregate/PaymentMethod.cs | 19 +++--------------- .../Repositories/BuyerRepository.cs | 7 +------ .../Repositories/OrderRepository.cs | 7 +------ 9 files changed, 15 insertions(+), 74 deletions(-) diff --git a/src/Services/Identity/Identity.API/Services/ProfileService.cs b/src/Services/Identity/Identity.API/Services/ProfileService.cs index 1d7e548bb..3d3826929 100644 --- a/src/Services/Identity/Identity.API/Services/ProfileService.cs +++ b/src/Services/Identity/Identity.API/Services/ProfileService.cs @@ -22,8 +22,7 @@ namespace Identity.API.Services async public Task GetProfileDataAsync(ProfileDataRequestContext context) { - var subject = context.Subject; - if (subject == null) throw new ArgumentNullException(nameof(context.Subject)); + var subject = context.Subject ?? throw new ArgumentNullException(nameof(context.Subject)); var subjectId = subject.Claims.Where(x => x.Type == "sub").FirstOrDefault().Value; @@ -37,8 +36,7 @@ namespace Identity.API.Services async public Task IsActiveAsync(IsActiveContext context) { - var subject = context.Subject; - if (subject == null) throw new ArgumentNullException(nameof(context.Subject)); + var subject = context.Subject ?? throw new ArgumentNullException(nameof(context.Subject)); var subjectId = subject.Claims.Where(x => x.Type == "sub").FirstOrDefault().Value; var user = await _userManager.FindByIdAsync(subjectId); diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs index 3e721cadc..27d828b9f 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs @@ -15,18 +15,8 @@ // Using DI to inject infrastructure persistence Repositories public CreateOrderCommandHandler(IBuyerRepository buyerRepository, IOrderRepository orderRepository) { - if (buyerRepository == null) - { - throw new ArgumentNullException(nameof(buyerRepository)); - } - - if (orderRepository == null) - { - throw new ArgumentNullException(nameof(orderRepository)); - } - - _buyerRepository = buyerRepository; - _orderRepository = orderRepository; + _buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository)); + _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); } public async Task Handle(CreateOrderCommand message) diff --git a/src/Services/Ordering/Ordering.API/Application/Queries/OrderQueries.cs b/src/Services/Ordering/Ordering.API/Application/Queries/OrderQueries.cs index 043747826..f10f76273 100644 --- a/src/Services/Ordering/Ordering.API/Application/Queries/OrderQueries.cs +++ b/src/Services/Ordering/Ordering.API/Application/Queries/OrderQueries.cs @@ -15,7 +15,7 @@ public OrderQueries(string constr) { - _connectionString = constr; + _connectionString = !string.IsNullOrWhiteSpace(constr) ? constr : throw new ArgumentNullException(nameof(constr)); } diff --git a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs index ec2886e2c..88e51fe71 100644 --- a/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs +++ b/src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs @@ -20,24 +20,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService) { - if (mediator == null) - { - throw new ArgumentNullException(nameof(mediator)); - } - if (orderQueries == null) - { - throw new ArgumentNullException(nameof(orderQueries)); - } - - if (identityService == null) - { - throw new ArgumentException(nameof(identityService)); - } - - _mediator = mediator; - _orderQueries = orderQueries; - _identityService = identityService; + _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); + _orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries)); + _identityService = identityService ?? throw new ArgumentNullException(nameof(identityService)); } [Route("new")] diff --git a/src/Services/Ordering/Ordering.API/Infrastructure/Services/IdentityService.cs b/src/Services/Ordering/Ordering.API/Infrastructure/Services/IdentityService.cs index e897b61c7..48975b6c1 100644 --- a/src/Services/Ordering/Ordering.API/Infrastructure/Services/IdentityService.cs +++ b/src/Services/Ordering/Ordering.API/Infrastructure/Services/IdentityService.cs @@ -13,12 +13,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Servi public IdentityService(IHttpContextAccessor context) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - _context = context; + _context = context ?? throw new ArgumentNullException(nameof(context)); } public string GetUserIdentity() diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/Buyer.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/Buyer.cs index 253532b71..6bd6853b7 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/Buyer.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/Buyer.cs @@ -21,12 +21,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B public Buyer(string identity) : this() { - if (String.IsNullOrWhiteSpace(identity)) - { - throw new ArgumentNullException(nameof(identity)); - } - - IdentityGuid = identity; + IdentityGuid = !string.IsNullOrWhiteSpace(identity) ? identity : throw new ArgumentNullException(nameof(identity)); } diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs index d4625fe81..0b1e2ceb0 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs @@ -21,20 +21,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B public PaymentMethod(int cardTypeId, string alias, string cardNumber, string securityNumber, string cardHolderName, DateTime expiration) { - if (String.IsNullOrWhiteSpace(cardNumber)) - { - throw new ArgumentException(nameof(cardNumber)); - } - if (String.IsNullOrWhiteSpace(securityNumber)) - { - throw new ArgumentException(nameof(securityNumber)); - } - - if (String.IsNullOrWhiteSpace(cardHolderName)) - { - throw new ArgumentException(nameof(cardHolderName)); - } + _cardNumber = !string.IsNullOrWhiteSpace(cardNumber) ? cardNumber : throw new ArgumentException(nameof(cardNumber)); + _securityNumber = !string.IsNullOrWhiteSpace(securityNumber) ? securityNumber : throw new ArgumentException(nameof(securityNumber)); + _cardHolderName = !string.IsNullOrWhiteSpace(cardHolderName) ? cardHolderName : throw new ArgumentException(nameof(cardHolderName)); if (expiration < DateTime.UtcNow) { @@ -42,9 +32,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B } _alias = alias; - _cardNumber = cardNumber; - _securityNumber = securityNumber; - _cardHolderName = cardHolderName; _expiration = expiration; _cardTypeId = cardTypeId; } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs index 8a2856de8..3a6bcede9 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs @@ -22,12 +22,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor public BuyerRepository(OrderingContext context) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - _context = context; + _context = context ?? throw new ArgumentNullException(nameof(context)); } public Buyer Add(Buyer buyer) diff --git a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs index dba90dfcb..89a63e028 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs @@ -19,12 +19,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor public OrderRepository(OrderingContext context) { - if (context == null) - { - throw new ArgumentNullException(nameof(context)); - } - - _context = context; + _context = context ?? throw new ArgumentNullException(nameof(context)); } public Order Add(Order order)