简体   繁体   English

C#形式和一成不变的类

[英]C# Forms and Immutable Classes

I understand how and why to create immutable classes, however, does the same rules apply to WinForms and Subforms? 我知道如何以及为什么创建不可变类,但是,相同的规则是否适用于WinForms和Subforms? Allow me to elaborate. 请允许我详细说明。

Main Form: 主要形式:

private void addNewEmployeeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int empcount = (comboEmail.Items.Count - 1);
            Employee retrive = null;



            using (Add addEmp = new Add(empcount))
            {
                DialogResult dr = new DialogResult();
                addEmp.FormBorderStyle = FormBorderStyle.FixedDialog;
                dr = addEmp.ShowDialog();

                if (dr == DialogResult.OK)
                {
                    retrive = addEmp.GetEmployee;
                    addtoTextFileCombo(retrive);

                }

            }


        }

Subform: 子窗体:

public partial class Add : Form
    {
        public Employee GetEmployee {get; private set;}
        public int CreateId { get; private set; }

        public Add(int id)
        {
            InitializeComponent();
            this.CreateId = id;


        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                this.CreateId++;
                this.GetEmployee = new Employee(CreateId, txtFirstName.Text, txtLastName.Text, txtEmail.Text);
                this.DialogResult = DialogResult.OK;
            }
            catch (ArgumentNullException msg)
            {
                MessageBox.Show(msg.Message);

            }
        }
    }

In my subform, I'm using a getter/setter property to create an Employee object and pass it back to my main form. 在我的子表单中,我正在使用getter / setter属性创建一个Employee对象,并将其传递回我的主表单。 Is it okay to use a Getter/Setter in my subform the way I did? 可以像以前一样在子窗体中使用Getter / Setter吗?

When creating immutable classes, you have readonly variables and use a constructor to set them. 创建不可变类时,您具有只读变量,并使用构造函数进行设置。 You don't use setters at all even if it's private. 即使是私有的,您也根本不使用设置器。 In this example, Employee is immutable. 在此示例中,Employee是不可变的。

Is what i did, bad practice? 是我做的,不好的做法吗? If so, how do I fix it without losing any of the functionality I have right now. 如果是这样,我如何解决它而不丢失我现在拥有的任何功能。

I don't think this is bad practice. 我认为这不是坏习惯。 We regularly use Microsoft's own OpenFileDialog (and its sister dialogs) that allow you to call ShowDialog() and once user chooses a file and clicks OK button (Open or Save actually), the sub form (the dialog that is) vanishes away and we can still access FileName property to get the name of the selected file(s). 我们通常使用Microsoft自己的OpenFileDialog (及其姊妹对话框),允许您调用ShowDialog() ,一旦用户选择了文件并单击“确定”按钮(实际上是“打开”或“保存”),子窗体(即对话框)便消失了,我们仍然可以访问FileName属性以获取所选文件的名称。 So it appears to be a standard practice. 因此,这似乎是一种标准做法。

The mutability of an object should be decided according to the business rules/flow or inherited from the object it represents from the domain universe. 对象的可变性应根据业务规则/流程来决定,或从其代表的对象(从领域Universe继承)继承。

A form per se is not a representation of an object from your applications domain; 表单本身并不代表您的应用程序域中的对象; it is merely a way of visualizing such objects and/or a way of interacting with such objects (either by modifying object state through bounded controls and/or by deleting/creating objects). 它仅仅是可视化此类对象的一种方式和/或与此类对象进行交互的一种方式(通过限制控件修改对象状态和/或通过删除/创建对象)。 As such, a form should never be immutable - it has state that is not relevant to your applications domain and that state can and will change without you being able to enforce that. 因此,表单永远不应该是不可变的-它的状态与您的应用程序域无关,并且该状态可以并且将会改变,而您无法强制执行。

As examples of such properties you have Position which changes whenever user moves the window, Location and so on... 作为此类属性的示例,您具有“ Position ,该Position在用户移动窗口时会更改,“ Location等...

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

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