简体   繁体   English

我如何简化克鲁德

[英]how can i simplify crud

Hi i would like to simplify my code on the crud messagebox. 嗨,我想简化我在Crud MessageBox上的代码。 I currently have almost 20 over pages of CRUD forms with 4 buttons of create, update delete and reset. 我目前有20多个CRUD表单页面,其中包含4个创建,更新,删除和重置按钮。 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); 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. 现在,选择任何表单中的任何“保存”按钮,转到“属性”>“事件”>,然后选择btnSave_Click。 You can use the same method throughout your software, provided that you have same code to run everywhere. 您可以在整个软件中使用相同的方法,前提是您要在所有地方运行相同的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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