Skip to content

Commit e66b6a2

Browse files
committed
ArgumentNullException handling using throw expressions
Solves dotnet-architecture#64
1 parent 6d72c7d commit e66b6a2

9 files changed

Lines changed: 15 additions & 74 deletions

File tree

src/Services/Identity/Identity.API/Services/ProfileService.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public ProfileService(UserManager<ApplicationUser> userManager)
2222

2323
async public Task GetProfileDataAsync(ProfileDataRequestContext context)
2424
{
25-
var subject = context.Subject;
26-
if (subject == null) throw new ArgumentNullException(nameof(context.Subject));
25+
var subject = context.Subject ?? throw new ArgumentNullException(nameof(context.Subject));
2726

2827
var subjectId = subject.Claims.Where(x => x.Type == "sub").FirstOrDefault().Value;
2928

@@ -37,8 +36,7 @@ async public Task GetProfileDataAsync(ProfileDataRequestContext context)
3736

3837
async public Task IsActiveAsync(IsActiveContext context)
3938
{
40-
var subject = context.Subject;
41-
if (subject == null) throw new ArgumentNullException(nameof(context.Subject));
39+
var subject = context.Subject ?? throw new ArgumentNullException(nameof(context.Subject));
4240

4341
var subjectId = subject.Claims.Where(x => x.Type == "sub").FirstOrDefault().Value;
4442
var user = await _userManager.FindByIdAsync(subjectId);

src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,8 @@ public class CreateOrderCommandHandler
1515
// Using DI to inject infrastructure persistence Repositories
1616
public CreateOrderCommandHandler(IBuyerRepository buyerRepository, IOrderRepository orderRepository)
1717
{
18-
if (buyerRepository == null)
19-
{
20-
throw new ArgumentNullException(nameof(buyerRepository));
21-
}
22-
23-
if (orderRepository == null)
24-
{
25-
throw new ArgumentNullException(nameof(orderRepository));
26-
}
27-
28-
_buyerRepository = buyerRepository;
29-
_orderRepository = orderRepository;
18+
_buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository));
19+
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
3020
}
3121

3222
public async Task<bool> Handle(CreateOrderCommand message)

src/Services/Ordering/Ordering.API/Application/Queries/OrderQueries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class OrderQueries
1515

1616
public OrderQueries(string constr)
1717
{
18-
_connectionString = constr;
18+
_connectionString = !string.IsNullOrWhiteSpace(constr) ? constr : throw new ArgumentNullException(nameof(constr));
1919
}
2020

2121

src/Services/Ordering/Ordering.API/Controllers/OrdersController.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,10 @@ public class OrdersController : Controller
2020

2121
public OrdersController(IMediator mediator, IOrderQueries orderQueries, IIdentityService identityService)
2222
{
23-
if (mediator == null)
24-
{
25-
throw new ArgumentNullException(nameof(mediator));
26-
}
27-
28-
if (orderQueries == null)
29-
{
30-
throw new ArgumentNullException(nameof(orderQueries));
31-
}
32-
33-
if (identityService == null)
34-
{
35-
throw new ArgumentException(nameof(identityService));
36-
}
3723

38-
_mediator = mediator;
39-
_orderQueries = orderQueries;
40-
_identityService = identityService;
24+
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
25+
_orderQueries = orderQueries ?? throw new ArgumentNullException(nameof(orderQueries));
26+
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
4127
}
4228

4329
[Route("new")]

src/Services/Ordering/Ordering.API/Infrastructure/Services/IdentityService.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ public class IdentityService : IIdentityService
1313

1414
public IdentityService(IHttpContextAccessor context)
1515
{
16-
if (context == null)
17-
{
18-
throw new ArgumentNullException(nameof(context));
19-
}
20-
21-
_context = context;
16+
_context = context ?? throw new ArgumentNullException(nameof(context));
2217
}
2318

2419
public string GetUserIdentity()

src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/Buyer.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ protected Buyer() {
2121

2222
public Buyer(string identity) : this()
2323
{
24-
if (String.IsNullOrWhiteSpace(identity))
25-
{
26-
throw new ArgumentNullException(nameof(identity));
27-
}
28-
29-
IdentityGuid = identity;
24+
IdentityGuid = !string.IsNullOrWhiteSpace(identity) ? identity : throw new ArgumentNullException(nameof(identity));
3025

3126
}
3227

src/Services/Ordering/Ordering.Domain/AggregatesModel/BuyerAggregate/PaymentMethod.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,17 @@ protected PaymentMethod() { }
2121

2222
public PaymentMethod(int cardTypeId, string alias, string cardNumber, string securityNumber, string cardHolderName, DateTime expiration)
2323
{
24-
if (String.IsNullOrWhiteSpace(cardNumber))
25-
{
26-
throw new ArgumentException(nameof(cardNumber));
27-
}
2824

29-
if (String.IsNullOrWhiteSpace(securityNumber))
30-
{
31-
throw new ArgumentException(nameof(securityNumber));
32-
}
33-
34-
if (String.IsNullOrWhiteSpace(cardHolderName))
35-
{
36-
throw new ArgumentException(nameof(cardHolderName));
37-
}
25+
_cardNumber = !string.IsNullOrWhiteSpace(cardNumber) ? cardNumber : throw new ArgumentException(nameof(cardNumber));
26+
_securityNumber = !string.IsNullOrWhiteSpace(securityNumber) ? securityNumber : throw new ArgumentException(nameof(securityNumber));
27+
_cardHolderName = !string.IsNullOrWhiteSpace(cardHolderName) ? cardHolderName : throw new ArgumentException(nameof(cardHolderName));
3828

3929
if (expiration < DateTime.UtcNow)
4030
{
4131
throw new ArgumentException(nameof(expiration));
4232
}
4333

4434
_alias = alias;
45-
_cardNumber = cardNumber;
46-
_securityNumber = securityNumber;
47-
_cardHolderName = cardHolderName;
4835
_expiration = expiration;
4936
_cardTypeId = cardTypeId;
5037
}

src/Services/Ordering/Ordering.Infrastructure/Repositories/BuyerRepository.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ public IUnitOfWork UnitOfWork
2222

2323
public BuyerRepository(OrderingContext context)
2424
{
25-
if (context == null)
26-
{
27-
throw new ArgumentNullException(nameof(context));
28-
}
29-
30-
_context = context;
25+
_context = context ?? throw new ArgumentNullException(nameof(context));
3126
}
3227

3328
public Buyer Add(Buyer buyer)

src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ public IUnitOfWork UnitOfWork
1919

2020
public OrderRepository(OrderingContext context)
2121
{
22-
if (context == null)
23-
{
24-
throw new ArgumentNullException(nameof(context));
25-
}
26-
27-
_context = context;
22+
_context = context ?? throw new ArgumentNullException(nameof(context));
2823
}
2924

3025
public Order Add(Order order)

0 commit comments

Comments
 (0)