ArgumentNullException handling using throw expressions

Solves #64
This commit is contained in:
etomas 2017-02-27 10:35:31 +01:00
parent 6d72c7dda0
commit e66b6a2402
9 changed files with 15 additions and 74 deletions

View File

@ -22,8 +22,7 @@ namespace Identity.API.Services
async public Task GetProfileDataAsync(ProfileDataRequestContext context) async public Task GetProfileDataAsync(ProfileDataRequestContext context)
{ {
var subject = context.Subject; var subject = context.Subject ?? throw new ArgumentNullException(nameof(context.Subject));
if (subject == null) throw new ArgumentNullException(nameof(context.Subject));
var subjectId = subject.Claims.Where(x => x.Type == "sub").FirstOrDefault().Value; 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) async public Task IsActiveAsync(IsActiveContext context)
{ {
var subject = context.Subject; var subject = context.Subject ?? throw new ArgumentNullException(nameof(context.Subject));
if (subject == null) throw new ArgumentNullException(nameof(context.Subject));
var subjectId = subject.Claims.Where(x => x.Type == "sub").FirstOrDefault().Value; var subjectId = subject.Claims.Where(x => x.Type == "sub").FirstOrDefault().Value;
var user = await _userManager.FindByIdAsync(subjectId); var user = await _userManager.FindByIdAsync(subjectId);

View File

@ -15,18 +15,8 @@
// Using DI to inject infrastructure persistence Repositories // Using DI to inject infrastructure persistence Repositories
public CreateOrderCommandHandler(IBuyerRepository buyerRepository, IOrderRepository orderRepository) public CreateOrderCommandHandler(IBuyerRepository buyerRepository, IOrderRepository orderRepository)
{ {
if (buyerRepository == null) _buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository));
{ _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
throw new ArgumentNullException(nameof(buyerRepository));
}
if (orderRepository == null)
{
throw new ArgumentNullException(nameof(orderRepository));
}
_buyerRepository = buyerRepository;
_orderRepository = orderRepository;
} }
public async Task<bool> Handle(CreateOrderCommand message) public async Task<bool> Handle(CreateOrderCommand message)

View File

@ -15,7 +15,7 @@
public OrderQueries(string constr) public OrderQueries(string constr)
{ {
_connectionString = constr; _connectionString = !string.IsNullOrWhiteSpace(constr) ? constr : throw new ArgumentNullException(nameof(constr));
} }

View File

@ -20,24 +20,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Controllers
public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService) public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService)
{ {
if (mediator == null)
{
throw new ArgumentNullException(nameof(mediator));
}
if (orderQueries == null) _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
{ _orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries));
throw new ArgumentNullException(nameof(orderQueries)); _identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
}
if (identityService == null)
{
throw new ArgumentException(nameof(identityService));
}
_mediator = mediator;
_orderQueries = orderQueries;
_identityService = identityService;
} }
[Route("new")] [Route("new")]

View File

@ -13,12 +13,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Servi
public IdentityService(IHttpContextAccessor context) public IdentityService(IHttpContextAccessor context)
{ {
if (context == null) _context = context ?? throw new ArgumentNullException(nameof(context));
{
throw new ArgumentNullException(nameof(context));
}
_context = context;
} }
public string GetUserIdentity() public string GetUserIdentity()

View File

@ -21,12 +21,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
public Buyer(string identity) : this() public Buyer(string identity) : this()
{ {
if (String.IsNullOrWhiteSpace(identity)) IdentityGuid = !string.IsNullOrWhiteSpace(identity) ? identity : throw new ArgumentNullException(nameof(identity));
{
throw new ArgumentNullException(nameof(identity));
}
IdentityGuid = identity;
} }

View File

@ -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) 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)) _cardNumber = !string.IsNullOrWhiteSpace(cardNumber) ? cardNumber : throw new ArgumentException(nameof(cardNumber));
{ _securityNumber = !string.IsNullOrWhiteSpace(securityNumber) ? securityNumber : throw new ArgumentException(nameof(securityNumber));
throw new ArgumentException(nameof(securityNumber)); _cardHolderName = !string.IsNullOrWhiteSpace(cardHolderName) ? cardHolderName : throw new ArgumentException(nameof(cardHolderName));
}
if (String.IsNullOrWhiteSpace(cardHolderName))
{
throw new ArgumentException(nameof(cardHolderName));
}
if (expiration < DateTime.UtcNow) if (expiration < DateTime.UtcNow)
{ {
@ -42,9 +32,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.B
} }
_alias = alias; _alias = alias;
_cardNumber = cardNumber;
_securityNumber = securityNumber;
_cardHolderName = cardHolderName;
_expiration = expiration; _expiration = expiration;
_cardTypeId = cardTypeId; _cardTypeId = cardTypeId;
} }

View File

@ -22,12 +22,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor
public BuyerRepository(OrderingContext context) public BuyerRepository(OrderingContext context)
{ {
if (context == null) _context = context ?? throw new ArgumentNullException(nameof(context));
{
throw new ArgumentNullException(nameof(context));
}
_context = context;
} }
public Buyer Add(Buyer buyer) public Buyer Add(Buyer buyer)

View File

@ -19,12 +19,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor
public OrderRepository(OrderingContext context) public OrderRepository(OrderingContext context)
{ {
if (context == null) _context = context ?? throw new ArgumentNullException(nameof(context));
{
throw new ArgumentNullException(nameof(context));
}
_context = context;
} }
public Order Add(Order order) public Order Add(Order order)