/// <summary> /// Retrieve an accounts ledger for a given accountId. /// This talks with the data access layer. /// </summary> /// <param name="accountId">The account to retrieve ledger of.<param> /// <returns>Returns an accounts ledger if found or null otherwise.</returns> public async Task <AccountLedger> GetAccountLedgerAsync(int accountId) { // Parallel async calls to save time var accountTask = _accountRepository.GetAccountAsync(accountId); var ledgerTask = _ledgerRepository.GetLedgerByAccountAsync(accountId); await Task.WhenAll(accountTask, ledgerTask).ConfigureAwait(false); // Avoid deadlocks var account = await accountTask; var ledger = await ledgerTask; // Map results to return type return(account == null ? null : new AccountLedger { Account = account.Id, Balance = account.Balance, PaymentHistory = ledger == null || !ledger.Any() ? new List <Payment>() : ledger.Select(l => new Payment { Id = l.Id, Date = l.Date, Amount = l.Amount, Status = l.Status, ClosedReason = l.ClosedReason }) }); }