简体   繁体   中英

how can i simplify crud

Hi i would like to simplify my code on the crud messagebox. I currently have almost 20 over pages of CRUD forms with 4 buttons of create, update delete and reset. How do i simplify this to become a user control? so that i don have to keep writting "Save successfully", "sorry, error",....

my Code

 protected override void btnSave_Click(object sender, EventArgs e)
    {
        if (!validateBeforeSave()) return;

        if (MessageBox.Show(MessageManager.SaveAsk, "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
        {
            try
            {                  
                BindValueToObject();                   

                if (Convert.ToInt32(lblEmpId.Text) == 0)
                {
                    user.Add();
                    ResetAfterSave(true, user.Id);
                    base.Success = true;
                }
                else
                {
                    user.Update();
                    ResetAfterSave(false, user.Id);
                    base.Success = true;
                }
                base.btnSave_Click(this, null);
            }
            catch (Exception ex)
            {
                Logger.Error(typeof(UsersForm), ex.ToString());
                base.Success = false;
                base.btnSave_Click(this, null);
            }
        }
    }
    protected override void btnDelete_Click(object sender, EventArgs e)
    {
        if (null == dgUser.CurrentRow) return;
        user.Id = (int)dgUser.SelectedRows[0].Cells["empId"].Value;

        try
        {
            if ((MessageBox.Show(MessageManager.DeleteAsk, "Are you sure to delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
            {
                user.Delete();
                ResetAfterSave(false, 0);
                base.Success = true;
                base.btnDelete_Click(this, null);
            }
        }
        catch
        {
            base.Success = false;
            base.btnDelete_Click(this, null);
        }
    }

The base.btnSave_Click(this, null); is calling this below where i pass in the flag.

 protected virtual void btnSave_Click(object sender, EventArgs e)
    {
        if (this.success)
            MessageBox.Show(MessageManager.SaveSuccess, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        else
        {
            MessageBox.Show(MessageManager.SaveFailed, "Fail to save", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            return;
        }
    }

I felt very annoying of rewriting this over and over again.

Sorry I meant Extracting not Encapsulating..fields can be encapsulated while methods are extracted. You can select this option from Refactor > Extract Method. Write these methods in main form and make them public. Then utilize them throughout the application.

I have a better option than that, but I would like to correct myself if it is hazardous to do so.

What we do is we declare the event handler public in the main form.

for eg

public virtual void btnSave_Click(object sender, EventArgs e)
    {
        if (this.success)
            MessageBox.Show(MessageManager.SaveSuccess, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        else
        {
            MessageBox.Show(MessageManager.SaveFailed, "Fail to save", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            return;
        }
    }

Now, Select any of the Save button in any of your form , go to properties > Events > and select btnSave_Click. You can use the same method throughout your software, provided that you have same code to run everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM