MonoState, Singleton or Derived Forms: the best approach for a CRUD application?

I have a rather large CRUD WinForm application that has many objects. Person, registration, plan, CaseNote, etc. . There are over 30 forms that make up the application with a logical user interface. Participant, applications, plans, notes, etc. .

I am trying to figure out how I can create my Person object after searching in the search form and submit the object to the next requested form. Be that as it may, say, demographic data. In short, I need a Person object that will be available throughout the application, and there can only be one.

Now I have a ZERO exposure on Design Patterns, but I'm trying. I 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 that I understand correctly how to apply it is to my situation.

First, the examples indicate that you are accessing a link, right? Am I mistaken or do I need to access the value?

Secondly, is there anything else I need to do to make this accessible globally? I just declare an instance for each form, but through this Singleton template so as not to have more than one?

thanks

EDIT 1

To clarify, all objects are child objects of Person. Also, as the search page does not work; users can choose another currentPerson. But they can only interact with the character ONE .

Finally, since I said that I was a baby in this, and if I should consider something else, a different approach, please say this, and if you were so kind as to offer some explanation why I would be very grateful.

EDIT 2

Based on the comments of the Medic, I thought I would ask for clarification.

First of all, thanks to everyone who has contributed so far. Secondly, I don’t know what primarily concerns design patterns, and of course I don’t have a nebula if a certain one is needed in my current situation.

If someone has a better, simpler or, in your opinion, more suitable way to transfer a data object from FORM to FORM to FORM, then PLEASE tell me.

In the end, I just need a way to track information as my users move from place to place. Thank you.


+4
source share
5 answers

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

However, the jury is still not working (at least in my opinion) on whether this is a good decision. There is a lot of reading about SO and other places about it.

I would approach this from a different angle. I will do all my forms in the Person instance in the constructor. Thus, each form only ever worries about this instance of Person.

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

You, of course, will have to control the creation of your Person object. You could do it with a singleton. However, the advantage is that each form does not need to know how to create a Person or who created a Man. Thus, if you decide to move away from the Singleton template , you don’t have to change all your links to your singleton instance.

EDIT:

Here is the code demonstrating this. It took me a while to get the designer to play beautifully. I had to add an empty constructor to PersonForm so that the developer would not throw an error.

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

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

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 (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; } } 
+1
source

First, the examples indicate that you are accessing a link, right? Am I mistaken or do I need to access the value?

Your class that you are referring to is a reference to one class in memory. For example, say your class:

 public class Person { ... } 

If you have a singleton, you will have one “Person” stored in memory, with a common reference to one person in singleton. When you contact your one person, you will work with this link, which you probably want. Any changes in a person will change everywhere.

Secondly, is there anything else I need to do to make this accessible globally? I just declare an instance for each form, but through this Singleton template so as not to have more than one?

Singletones are used to basically ensure that every time you use an object, it is the same object (each use is a separate reference to one, one object in memory). You can just grab a singleton anywhere and it will work.

+2
source

You can do something like this:

 public static class PersonController { private static Person _Person; public static Person GetPerson() { if (_Person == null) _Person = new Person(); return _Person; } } 

This will ensure the availability of only one object. You will receive a link to the _Person object, not a copy, so any changes will be for the same object that you expect.

+1
source

As Reed says, singles ensure that the same object is used throughout the application. However, from your question, it does not look as if you have the same instance of the person class available throughout the application, as there is a “search form” that looks like you can change the selected person.

In this case, your singleton may need to be a container class that contains the current application context and which person is currently selected. It could be something like:

 public class Context { private static Context _instance; public static Context Instance { get { if (_instance == null) { _instance = new Context(); } return _instance; } } public Person CurrentlySelectedPerson { get; set; } private Context() { } } 

(Note that this is not an ideal singleton pattern, as it is not thread safe ...)

Then, in the search form, the currently selected person will be selected:

 Context.Instance.CurrentlySelectedPerson = personSelectedInForm; 

And the demographic of them can use it like:

 //Get the demographics for the current person ShowDemographics(Context.Instance.CurrentlySelectedPerson); 
+1
source

You can also use the monostate template with your Person class.

 public class Person { public Guid Id { get; set; } public String FirstName { get; set; } public String LastName { get; set; } } 

Build a monostate object for Person .

 public class CurrentPerson { public static Person Person { get; set; } public Guid Id { get { return CurrentPerson.Person.Id; } set { CurrentPerson.Person.Id = value; } } public String FirstName { get { return CurrentPerson.Person.FirstName; } set { CurrentPerson.Person.FirstName = value; } } public String LastName { get { return CurrentPerson.Person.LastName; } set { CurrentPerson.Person.LastName = value; } } } 

Now you can initialize the monostat.

 CurrentPerson.Person = GetPersonByUserInput(); 

And then use CurrentPerson instances throughout the code, and all of them will refer to the overall common state.

 CurrentPerson currentPerson = new CurrentPerson(); 
0
source

All Articles