将对象从表单传递给表单

时间:2022-12-16 15:54:32

I have a fairly large CRUD winform app that is set up to display forms embedded in tabcontrols. I want to have objects for Person,(has a) Enrollment,(has a) Plan that hold and track the information as they interact with the forms. How do I accomplish this? I found a suggestion to declare the Person object in my Program.cs like so -->

我有一个相当大的CRUD winform应用程序,它设置为显示tabcontrols中嵌入的表单。我想拥有Person的对象,(有)a Enrollment,(有a)计划在与表单交互时保存和跟踪信息。我该如何做到这一点?我发现了一个建议,在我的Program.cs中声明Person对象,就像这样 - >

internal static class Program
{
    public static CurrentPerson _CurrentPerson;

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    private static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FrmWWCShell());
    }
}

and then on the Search.cs -->

然后在Search.cs上 - >

 Program._CurrentPerson = new CurrentPerson
                                     {
                                         PersonID = Convert.ToInt32(pID),
                                         LastName = lName,
                                         FirstName = fName,
                                         SocialSn = sSN,
                                         MiddleName = mName,
                                         BirthDate = Convert.ToDateTime(bDate)
                                     };

Is this the best way? There is still a bunch of Data that needs to be filled in from the database once they have made this selection on the Search page. What about declaring the object on each form and passing it some way? The object is slowly "built" as they progress. First they Search for someone by name and select who they will work with. Then they can work with there Enrollments. After selecting an Enrollment they will be able to interact with there Plans.

这是最好的方法吗?在“搜索”页面上进行此选择后,仍有大量数据需要从数据库中填写。如何在每个表单上声明对象并以某种方式传递它?随着对象的进展,对象会慢慢“建立”。首先,他们按名称搜索某人,并选择他们将与谁合作。然后他们可以在那里工作。选择注册后,他们将能够与计划进行互动。

I would be grateful for any guidance here as the scope of this has left my inexperienced head spinning...

我很感激这里的任何指导,因为这个范围让我没有经验的头脑旋转......

3 个解决方案

#1


You will need to seperate your data, logic and GUI.

您需要分离数据,逻辑和GUI。

Look into the Model-View-Controller pattern.

查看模型 - 视图 - 控制器模式。

If you think it's too complex in your case you might just create a central controller class to hold the central data. Pass on this object in every form constructor and you're done.

如果您认为在您的情况下它太复杂,您可能只需创建一个*控制器类来保存*数据。在每个表单构造函数中传递此对象,您就完成了。

#2


Take a look at the Mediator design pattern.

看看Mediator设计模式。

#3


a) Singleton

A global static property is not such a good idea from the code reuse perspective. If you are planning to access a global static property from all over your code, you are making your code pretty tied to this specific application.

从代码重用的角度来看,全局静态属性并不是一个好主意。如果您计划从代码中访问全局静态属性,那么您的代码将与此特定应用程序紧密相关。

If there was always only a single instance of Person in your code, then you could place this Singleton inside the Person class, but certainly not inside your Program class. Note, however, that use of Singleton classes will usually be limited to logging service or something which is so widely common that it will surely never change.

如果代码中始终只有一个Person实例,那么你可以将这个Singleton放在Person类中,但肯定不在你的Program类中。但请注意,Singleton类的使用通常仅限于日志记录服务或某种广泛使用的东西,它肯定永远不会改变。

b) Same object reference

b)相同的对象引用

In this case you don't need a singleton instance, but rather to pass the same reference to your data object (Person, or whatever it is) to each Form that is accessing it. If your Form is a representation of a part of your data, then you can pass only that part of data to the form, preferably through a simplest possible interface.

在这种情况下,您不需要单例实例,而是将相同的引用传递给您访问它的每个Form的数据对象(Person或其他任何东西)。如果您的表单是数据的一部分,那么您只能将该部分数据传递给表单,最好通过最简单的接口。

Changing the data in one form will probably need to update other forms. That is what Model-View-Controller and similar patterns help you accomplish - notifying views that the data has been changed somewhere else.

更改一个表单中的数据可能需要更新其他表单。这就是模型 - 视图 - 控制器和类似模式可以帮助您完成的任务 - 通知视图数据已在其他地方更改。

For example, by implementing the IPropertyNameChanged interface in your Person class, you can notify anyone interested (any Form), whenever a property is changed. Check this for an example: http://msdn.microsoft.com/en-us/library/ms229614.aspx. By attaching an event handler to that event in each form, you will notify all of them that they need to be invalidated.

例如,通过在Person类中实现IPropertyNameChanged接口,无论何时更改属性,您都可以通知任何感兴趣的人(任何表单)。请查看此示例:http://msdn.microsoft.com/en-us/library/ms229614.aspx。通过在每个表单中附加事件处理程序到该事件,您将通知所有这些事件需要使其失效。

#1


You will need to seperate your data, logic and GUI.

您需要分离数据,逻辑和GUI。

Look into the Model-View-Controller pattern.

查看模型 - 视图 - 控制器模式。

If you think it's too complex in your case you might just create a central controller class to hold the central data. Pass on this object in every form constructor and you're done.

如果您认为在您的情况下它太复杂,您可能只需创建一个*控制器类来保存*数据。在每个表单构造函数中传递此对象,您就完成了。

#2


Take a look at the Mediator design pattern.

看看Mediator设计模式。

#3


a) Singleton

A global static property is not such a good idea from the code reuse perspective. If you are planning to access a global static property from all over your code, you are making your code pretty tied to this specific application.

从代码重用的角度来看,全局静态属性并不是一个好主意。如果您计划从代码中访问全局静态属性,那么您的代码将与此特定应用程序紧密相关。

If there was always only a single instance of Person in your code, then you could place this Singleton inside the Person class, but certainly not inside your Program class. Note, however, that use of Singleton classes will usually be limited to logging service or something which is so widely common that it will surely never change.

如果代码中始终只有一个Person实例,那么你可以将这个Singleton放在Person类中,但肯定不在你的Program类中。但请注意,Singleton类的使用通常仅限于日志记录服务或某种广泛使用的东西,它肯定永远不会改变。

b) Same object reference

b)相同的对象引用

In this case you don't need a singleton instance, but rather to pass the same reference to your data object (Person, or whatever it is) to each Form that is accessing it. If your Form is a representation of a part of your data, then you can pass only that part of data to the form, preferably through a simplest possible interface.

在这种情况下,您不需要单例实例,而是将相同的引用传递给您访问它的每个Form的数据对象(Person或其他任何东西)。如果您的表单是数据的一部分,那么您只能将该部分数据传递给表单,最好通过最简单的接口。

Changing the data in one form will probably need to update other forms. That is what Model-View-Controller and similar patterns help you accomplish - notifying views that the data has been changed somewhere else.

更改一个表单中的数据可能需要更新其他表单。这就是模型 - 视图 - 控制器和类似模式可以帮助您完成的任务 - 通知视图数据已在其他地方更改。

For example, by implementing the IPropertyNameChanged interface in your Person class, you can notify anyone interested (any Form), whenever a property is changed. Check this for an example: http://msdn.microsoft.com/en-us/library/ms229614.aspx. By attaching an event handler to that event in each form, you will notify all of them that they need to be invalidated.

例如,通过在Person类中实现IPropertyNameChanged接口,无论何时更改属性,您都可以通知任何感兴趣的人(任何表单)。请查看此示例:http://msdn.microsoft.com/en-us/library/ms229614.aspx。通过在每个表单中附加事件处理程序到该事件,您将通知所有这些事件需要使其失效。