skip to Main Content
if(section1()){
  if(section2()){
    if(section3()){
     .
     .
     .
     // Upto 10 methods
       if(section10(){
         return true;
       }

    }
  }
}
 

I am writing validations for 170+ controls, divided into 10 sections. As per business logic, if first section method returns true, then execute second method, else return false and show message like for ex. "Title is missing". If second method is true then execute third method and so on..

Is there an alternative to the above code? I need alternative for nested if conditions. Thanks a lot.

2

Answers


  1. It sounds like what you really want to do is loop through and invoke these methods and for each iteration, do something with the result and decide whether or not to move on to the next step.

    It’s hard to give specifics here because we don’t know what these functions do, but we know they all return a boolean value. So if any of them returns false, you probably want to break the loop and return early.

    Login or Signup to reply.
  2. Here are a few examples/gists of C# classes you can use to implement that.
    Try to nest the ValidationSummary and Validator instances to simulate the nested if()s you had.
    In each validator instance, you have to set the dynamic function (stored here in Process) to invoke during the validation for each section.
    It’s a more flexible and scalable solution I think.

    class Program
    {
        public static void Main()
        {
            ValidationSummary valSum = new ValidationSummary();
    
            Validator val1 = new Validator("section 1");
            val1.Process = () => { return true; };
            valSum.Validators.Add(val1);
    
            Validator val2 = new Validator("section 2");
            val2.Process = () => { return false; };
            valSum.Validators.Add(val2);
    
            valSum.Validate();
    
            Console.WriteLine("Result = " + (valSum.IsValid ? "YES" : "NO"));
    
            Console.ReadKey();
        }
    }
    
    class ValidationSummary
    {
        private bool m_valid = false;
    
        public bool IsValid
        {
            get
            {
                return this.m_valid;
            }
        }
    
        public List<Validator> Validators
        {
            get;
        }
    
        public ValidationSummary()
        {
            this.m_valid = false;
            this.Validators = new List<Validator>();
        }
    
        public void Validate()
        {
            // Trigger all validators.
            foreach (Validator v in this.Validators)
            {
                if (v.IsValid == false)
                {
                    // sudden death, so stop all validations.
                    this.m_valid = false;
                    return;
                }
            }
    
            this.m_valid = true;
        }
    }
    
    class Validator
    {
    
        public string Name
        {
            get;
            set;
        }
    
        // Use this property to encapsulate all validation rules and processes.
        public Func<bool> Process
        {
            get;
            set;
        }
    
        public bool IsValid
        {
            get
            {
                return this.Execute();
            }
        }
    
        public Validator(string name)
        {
            this.Name = name;
            this.Process = null;
        }
    
        private bool Execute()
        {
            return (bool)this.Process.DynamicInvoke();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search