Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to optimize duplicated code between forms in C#

I have a self-made project called "Library Management", I have many forms: Book Form, Book Type Form, Author Form,…
But I realized that the code in the forms is very similar (only in some places like controls, ..).
How to reduce this duplication?. I know I have to create a base class and forms will inherit this class but I don’t know what to write in that base class.
Please help me, thank you very much

private void AuthorForm_Load(object sender, EventArgs e)
    {
        if (_authorDAL == null)
            _authorDAL = new AuthorDAL();

        loadData().ContinueWith((t) => 
        {
            if (InvokeRequired)
            {
                Invoke((MethodInvoker)(() =>
                {
                    bindingData();
                    applyUIStrings();
                }));
            }
            else
            {
                bindingData();
                applyUIStrings();
            }
        });
    }

And this is the duplicate code:

private void BookTypeForm_Load(object sender, EventArgs e)
    {
        if (bookTypeDAL == null)
            bookTypeDAL = new BookTypeDAL();

        loadData().ContinueWith((t) =>
        {
            if (InvokeRequired)
            {
                Invoke((MethodInvoker)(() =>
                {
                    bindingData();
                    applyUIStrings();
                }));
            }
            else
            {
                bindingData();
                applyUIStrings();
            }
        });
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are a number of approaches you could take, but with inheritance, you could create an abstract base class with a method and use generics like this:

protected void Form_Load<T>(T dalObject)
    where T : new()
{
    if (dalObject == null)
        dalObject= new T();

    loadData().ContinueWith((t) =>
    {
        if (InvokeRequired)
        {
            Invoke((MethodInvoker)(() =>
            {
                bindingData();
                applyUIStrings();
            }));
        }
        else
        {
            bindingData();
            applyUIStrings();
        }
    });
}

which can then be called in the derived class:

private void AuthorForm_Load(object sender, EventArgs e)
{
    FormLoad<AuthorDAL>();
}

or simplified further to the call site like this:

form.Load += (s, e) => FormLoad<AuthorDAL>();

However, this would require your loadData, bindingData and applyUIStrings methods to be to visible in the base class too. If they’re different for each DAL type, you could make them virtual methods in the base class and override them in the derived classes.

Another approach, perhaps better, would be to have the common Form_Load in the base class, and have it call a virtual method to create the DAL object.

protected virtual void CreateDALObject();

protected void Form_Load(object sender, EventArgs e)
    where T : new()
{
    CreateDALObject();
    // ...
}

Or you could try to move methods out into supporting classes, which can be preferable to using inheritance, but sometimes difficult because WinForms so heavily rely on inheritance.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading