Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class RulesCustomerRepository : ICustomerRepository
  2. {
  3.     private readonly ICustomerRepository _customerRepository;
  4.  
  5.     public RulesCustomerRepository(ICustomerRepository customerRepository)
  6.     {
  7.         if (customerRepository == null)
  8.             throw new ArgumentNullException(nameof(customerRepository));
  9.  
  10.         _customerRepository = customerRepository;
  11.     }
  12.  
  13.     public void Create(Customer customer)
  14.     {
  15.         if (customer == null)
  16.             throw new ArgumentNullException(nameof(customer));
  17.        
  18.         if (Find(c => c.Name.Value.Equals(customer.Name.Value)).Any())
  19.             throw new Exception("A customer with this name already exists");
  20.        
  21.         if (!customer.EmailAddress.IsEmpty() && Find(c => c.EmailAddress.Value.Equals(customer.EmailAddress.Value)).Any())  
  22.             throw new Exception("A customer with this Email Address already exists");
  23.  
  24.         _customerRepository.Create(customer);
  25.     }
  26.  
  27.     public Customer Find(int id)
  28.     {
  29.         if (id <= 0)
  30.             throw new ArgumentOutOfRangeException(nameof(id));
  31.  
  32.         return _customerRepository.Find(id);
  33.     }
  34.  
  35.     public IEnumerable<Customer> Find(Func<Customer, bool> predicate)
  36.     {
  37.         if (predicate == null)
  38.             throw new ArgumentNullException(nameof(predicate));
  39.  
  40.         return _customerRepository.Find(predicate);
  41.     }
  42.  
  43.     public void Update(Customer customer)
  44.     {
  45.         if (customer == null)
  46.             throw new ArgumentNullException(nameof(customer));
  47.  
  48.         if (Find(c => c.Name.Value.Equals(customer.Name.Value) && c.Id.Value != customer.Id.Value).Any())
  49.             throw new Exception("A customer with this name already exists");
  50.        
  51.         if (!customer.EmailAddress.IsEmpty() && Find(c => c.EmailAddress.Value.Equals(customer.EmailAddress.Value) && c.Id.Value != customer.Id.Value).Any())
  52.             throw new Exception("A customer with this Email Address already exist");
  53.  
  54.         _customerRepository.Update(customer);
  55.     }
  56.  
  57.     public void Delete(Id id)
  58.     {
  59.         if (id <= 0)
  60.             throw new ArgumentOutOfRangeException(nameof(id));
  61.  
  62.         _customerRepository.Delete(id);
  63.     }
  64. }