Search code examples
c#code-duplication

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();
            }
        });
    }

Solution

  • There are a number of approaches you could take, but with inheritance, you could create an abstract base class:

    public abstract BaseForm : Form
    {
        protected virtual void CreateDALObject();
        protected virtual void BindingData();
        protected virtual void ApplyUIStrings();
    
        protected void Form_Load(object sender, EventArgs e)
        {
            CreateDALObject();
    
            loadData().ContinueWith((t) =>
            {
                if (InvokeRequired)
                {
                    Invoke((MethodInvoker)(() =>
                    {
                        BindingData();
                        ApplyUIStrings();
                    }));
                }
                else
                {
                    BindingData();
                    ApplyUIStrings();
                }
            });
        }
    }
    

    Which can then be derived from like this:

    public class AuthorForm : BaseForm
    {
        // ...
     
        protected override void CreateDALObject()
        {
            if (_authorDAL == null)
                _authorDAL = new AuthorDAL();
        }
    
        // ...
    }
    

    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.

    Edit note: I've removed the generic example I originally gave, because it would have required passing the objects to be initialised by reference, which wouldn't be a clean approach.