ICustomersBusinessLogicLayer.cs
using HJHBank.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HJHBank.BussinessLayer.BALContracts
{
/// <summary>
/// Interface that represents customers business logic
/// </summary>
public interface ICustomersBusinessLogicLayer
{
// <summary>
/// Returns all existing customers
/// </summary>
/// <returns>List of customers</returns>
List<Customer> GetCustomers();
/// <summary>
/// Returns a set of customers that matches with specified criteria
/// </summary>
/// <param name="predicate">Lamdba expression that contains condition to check</param>
/// <returns>The list of matching customers</returns>
List<Customer> GetCustomersByCondition(Predicate<Customer> predicate);
/// <summary>
/// Adds a new customer to the existing customers list
/// </summary>
/// <param name="customer">The customer object to add</param>
/// <returns>Returns true, that indicates the customer is added successfully
/// </returns>
Guid AddCustomer(Customer customer);
/// <summary>
/// Updates an existing customer
/// </summary>
/// <param name="customer">Customer object that contains customer details to update</param>
/// <returns>Returns true, that indicates the customer is updated successfully</returns>
bool UpdateCustomer(Customer customer);
/// <summary>
/// Deletes an existing customer
/// </summary>
/// <param name="customerID">CustomerID to delete</param>
/// <returns>Returns true, that indicates the customer is deleted successfully</returns>
bool DeleteCustomer(Guid customerID);
}
}
CustomersBusinessLogicLayer.cs
using HJHBank.BussinessLayer.BALContracts;
using HJHBank.DataAccessLayer.DALContracts;
using HJHBank.DataAccessLayer;
using HJHBank.Entities;
using HJHBank.Exceptions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HJHBank.BussinessLayer
{
/// <summary>
/// Represents customer business logic
/// </summary>
public class CustomersBusinessLogicLayer : ICustomersBusinessLogicLayer
{
#region Private Fields
private ICustomersDataAccessLayer _customersDataAccessLayer;
#endregion
#region Constructors
/// <summary>
/// Constructor that initializes CustomersDataAccessLayer
/// </summary>
public CustomersBusinessLogicLayer()
{
_customersDataAccessLayer = new CustomersDataAccessLayer();
}
#endregion
#region Properties
/// <summary>
/// Private property that represents reference of CustomersDataAccessLayer
/// </summary>
private ICustomersDataAccessLayer CustomersDataAccessLayer
{
set => _customersDataAccessLayer = value;
get => _customersDataAccessLayer;
}
#endregion
#region Methods
// <summary>
/// Returns all existing customers
/// </summary>
/// <returns>List of customers</returns>
public List<Customer> GetCustomers()
{
try
{
//invoke DAL
return CustomersDataAccessLayer.GetCustomers();
}
catch (CustomerException)
{
throw;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Returns a set of customers that matches with specified criteria
/// </summary>
/// <param name="predicate">Lamdba expression that contains condition to check</param>
/// <returns>The list of matching customers</returns>
public List<Customer> GetCustomersByCondition(Predicate<Customer> predicate)
{
try
{
//invoke DAL
return CustomersDataAccessLayer.GetCustomersByCondition(predicate);
}
catch (CustomerException)
{
throw;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Adds a new customer to the existing customers list
/// </summary>
/// <param name="customer">The customer object to add</param>
/// <returns>Returns true, that indicates the customer is added successfully
/// </returns>
public Guid AddCustomer(Customer customer)
{
try
{
//get all customers
List<Customer> allCustomers = CustomersDataAccessLayer.GetCustomers();
long maxCustCode = 0;
foreach (var item in allCustomers)
{
if (item.CustomerCode > maxCustCode)
{
maxCustCode = item.CustomerCode;
}
}
//generate new customer no
if (allCustomers.Count >= 1)
{
customer.CustomerCode = maxCustCode + 1;
}
else
{
customer.CustomerCode = HJHBank.Configuration.Settings.BaseCustomerNo + 1;
}
//invoke DAL
return CustomersDataAccessLayer.AddCustomer(customer);
}
catch (CustomerException)
{
throw;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Updates an existing customer
/// </summary>
/// <param name="customer">Customer object that contains customer details to update</param>
/// <returns>Returns true, that indicates the customer is updated successfully</returns>
public bool UpdateCustomer(Customer customer)
{
try
{
//invoke DAL
return CustomersDataAccessLayer.UpdateCustomer(customer);
}
catch (CustomerException)
{
throw;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Deletes an existing customer
/// </summary>
/// <param name="customerID">CustomerID to delete</param>
/// <returns>Returns true, that indicates the customer is deleted successfully</returns>
public bool DeleteCustomer(Guid customerID)
{
try
{
//invoke DAL
return CustomersDataAccessLayer.DeleteCustomer(customerID);
}
catch (CustomerException)
{
throw;
}
catch (Exception)
{
throw;
}
}
#endregion
}
}