的Monostate,辛格尔顿,或派生形式:最佳方法CRUD应用程序?格尔、应用程序、形式、方法

2023-09-04 12:41:26 作者:欲念

我有一个相当大的CRUD的WinForm的应用程序,有无数的对象。 的人,招生,计划,CaseNote等。有超过30的形式,使与分解逻辑用户界面应用程序。 会员,扩招,计划,CaseNotes等。

I have a fairly large CRUD WinForm app that has numerous objects. Person, Enrollment, Plan, CaseNote etc. There are over 30 forms that make up the app with the UI broken down logically. Member, Enrollments, Plans, CaseNotes, etc.

我试图找出如何我可以创造我的的 Person对象的上搜索后的搜索表单的和对象传递给下一个申请表格。不管这可能是,让我们说的人口统计的。它的短是,我需要在整个应用程序提供Person对象,并有可能只有一个。

I am trying to figure out how I can create my Person Object after searching on the Search Form and pass THE object to the next requested form. Whatever that may be, let's say Demographics. The short of it is that I need the Person object to be available throughout the App and there can only be one.

现在我是零接触到的设计模式,但我想。我已阅读 HTTP://www.switchonthe$c$c。 COM /教程/ CSHARP-教程单图案和 HTTP:// WWW .yoda.arachsys.com / CSHARP / singleton.html 但我想确认我理解正确的话如何应用这对我的情况。

Now I have ZERO exposure to Design Patterns but I am trying. I have read http://www.switchonthecode.com/tutorials/csharp-tutorial-singleton-pattern and http://www.yoda.arachsys.com/csharp/singleton.html but I want to make sure I understand correctly how to apply this to my situation.

首先,举例说明您正在访问的引用的,对不对?难道我错了还是我需要访问的值的?

First, the examples state that you are accessing a reference, correct? Am I mistaken or would I need to access the value?

二,还有什么,我需要做的,使这个在全球范围内提供?难道我只是声明对每一个形式,但通过这个Singleton模式,以没有更多然后一个实例?

Second, is there anything else that I need to do to make this Globally available? Do I just declare a instance on each form but through this Singleton Pattern so as to not have more then one?

感谢

要澄清一下,所有的对象都是人的子对象。此外,由于搜索页面逃避到;用户可以选择不同的currentPerson。但是,他们只能是的一个的人一次互动。

To clarify, All objects are child objects of Person. Also, As the Search Page eludes to; the users can select a different currentPerson. But they can only interact with ONE Person at a time.

最后,我说我是个婴儿在这一点,如果我要考虑别的东西,不同的方法,请直说,如果你会这么好心提供一些解释,为什么,我会非常感谢。

Lastly, as I stated I am an infant in this and if I should be considering something else, a different approach please say so and if you'd be so kind as to offer some explanation as to why, I'd be very grateful.

首先,感谢大家谁迄今已做出了贡献。 第二,我不知道设计模式的第一件事,我当然如果某一个需要在我目前的状况没有foggiest。

First, Thank you to everyone who has contributed so far. Second, I don't know the first thing about design patterns and I certainly don't have the foggiest if a certain one is needed in my current situation.

如果有人有一个更好,更简单,或者,在您看来,更贴合传递一个数据对象的形式方式构成,形成,那么请告诉。

If someone has a better, simpler, or ,in your opinion, a more fitting method of passing a Data Object from FORM to FORM to FORM then PLEASE tell.

在最后,我只需要跟踪的信息的一种方式,我的用户去从地方到另一个地方。 谢谢

In the end I just need a way of tracking the information as my users go from place to place. Thank You

推荐答案

您可以使用Singleton模式,以确保只有一个实例是目前所开发。

You can use the Singleton pattern to assure that only one instance is ever created.

然而,陪审团还在外面(至少在我的脑海里)这是否是一个好的决定。有大量的阅读SO和其他地方这个问题。

However, the jury is still out (at least in my mind) on whether this is a good decision. There's plenty of reading on SO and other places about this.

我会从不同的角度接近这个。我会做我所有的形式参加在构造一个Person实例。这样一来,每个表单只能是不断担心它的人的实例。

I would approach this from a different angle. I'd make all of my forms take in a Person instance in the constructor. That way, each form is only ever worried about it's instance of Person.

您可以通过创建一个新的类继承的形式,并有一个字段/属性/构造你的人做到这一点。然后,任何形式的使用人可以继承你的新类。

You could do this by creating a new class that inherits from Form and has a field/property/constructor for your Person. Then, any form that uses Person can inherit from your new class.

您会,当然,有管理的创建Person对象。你甚至可以用一个单做到这一点。但好处是,每个表单不必知道如何创建一个人或谁创造了人。这样一来,如果你选择move距离Singleton模式,你就不会去改变所有的引用您的单一实例。

You would, of course, have to manage the creation of your Person object. You could even do this with a singleton. However, the benefit is that each form doesn't have to know how to create a Person or who created the Person. That way, if you choose to move away from the Singleton pattern, you wouldn't have to go change all of your references to your singleton instance.

编辑:

下面是一些code来证明这一点。我花了一段时间来让设计师发挥好。我不得不添加一个空的私有构造中为personForm让设计师不会引发错误。

Here's some code to demonstrate this. It took me a while to get the designer to play nice. I had to add an empty private constructor in PersonForm to get the designer to not throw an error.

Program.cs的

Program.cs

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyDerivedForm(new Person { Name = "Hello World!" }));
        }
    }

Person.cs的

Person.cs

public class Person
{
    public virtual string Name { get; set; }
}

PersonForm.cs

PersonForm.cs

using System;
using System.Windows.Forms;

public class PersonForm : Form
{
    private readonly Person myPerson;

    protected virtual Person MyPerson 
    {
        get
        {
            return this.myPerson;
        }
    }

    private PersonForm()
    {
    }

    public PersonForm(Person person)
    {
        this.myPerson = person;
    }
}

MyDerivedForm.cs(添加标签名为label1的)

MyDerivedForm.cs (add a label named label1)

public partial class MyDerivedForm : SingletonMadness.PersonForm
{
    public MyDerivedForm(Person person)
        : base(person)
    {
        InitializeComponent();
    }

    private void MyDerivedForm_Load(object sender, EventArgs e)
    {
        label1.Text = this.MyPerson.Name;
    }
}