OCP

New behaviors must be added to software assets. The system must be extensible without affecting existing code

Photo 1

Example

To use Validation in Customer Manager, I can extend my project as in the example.

                         public class CustomerManager : ICustomerService
 {
     private IEnumerable<ICustomerValidator> _customerValidator;

     public CustomerManager(IEnumerable<ICustomerValidator> customerValidator)
     {
         _customerValidator = customerValidator;
     }

     public void AddCustomer(CUSTOMER customer)
     {
         foreach(var validator in _customerValidator)
         {
             if(!validator.Validate(customer))
             {
                 Console.WriteLine("Invalid Customer");
                 return;
             }
         }
         Console.WriteLine("success Customer");
     }
 }
                    

ICustomerValidation
                        public interface ICustomerValidator
{
    bool Validate(CUSTOMER customer);
}
                    

Validations
                        public class EmailValidator:ICustomerValidator
  {
      public bool Validate(CUSTOMER customer)
      {
          return !string.IsNullOrEmpty(customer.Email) && customer.Email.Contains("@");
      }
  }
public class NameValidator :ICustomerValidator
{
    public bool Validate(CUSTOMER customer)
    {
        return !string.IsNullOrWhiteSpace(customer.Name);
    }
}
public class PasswordValidator :ICustomerValidator
 {
     public bool Validate(CUSTOMER customer)
     {
         return !string.IsNullOrEmpty(customer.Password) && customer.Password.Length >= 6;
     }
 }