Search code examples
c#winformsmulti-layer

Common way to handover the status of multiple Checkboxes to Business-layer


I've a three layer architecture. On the top layer is my GUI and on the second layer are my Business classes. On the GUI are many checkboxes (+20 items) and a "Start"-button available. If the User click on the button I would like to handover the status of the checkboxes to a business object via constructor.

To pass a List<Checkbox> is not a suitable way and the solution should be flexible for changes. Additionally I need a possibility to identify the checkbox status on the business-layer. Is there a common possibility to solve this?

Here a example to my question:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        for (int i = 0; i < 20; i++)
        {
            flowLayoutPanel1.Controls.Add(CreateCheckbox(i));
        }

        var button = new Button();
        button.Name = "StartButton";
        button.Text = "Start";
        button.Click += new EventHandler(button_Click);

        flowLayoutPanel1.Controls.Add(button);
    }

    private void button_Click(object sender, EventArgs e)
    {
        var businessObject 
           = new BusinessClass(/*How to handover the checkbox status*/);
    }

    private CheckBox CreateCheckbox(int index)
    {
        var checkBox = new CheckBox();
        checkBox.Name = "checkBox" + index;
        checkBox.Text = "checkBox" + index;

        return checkBox;
    }
}

enter image description here


Solution

  • One way would be is to create a custom Interface and you can have a GetStatus() method inside it.

    IStatus
    {
        bool GetStatus();
    }
    

    Then you can create a another class and call it Status which inherits from IStatus and you can pass a list of IStatus inside the constructor, this concept is known as dependency invertion.

    Status : IStatus
    {
          bool Status {get;set;}
          bool GetStatus()
          {
               return Status;
          }
    }
    

    Now all you have to do is pass a List into your constructor.This gives you the flexibility to later pass information about any type of control from the UI layer.

    BusinessLayer(List<IStatus> statusObjects)
    {
    
    }
    

    All you have to do is create custom classes which inherit IStatus and pass it, your BusinessLayer logic is never changed.