views:

287

answers:

5

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.

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.

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?

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?

Thanks

EDIT 1

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.

EDIT 2

Based on Medicine Man's comment I thought I wauld clarify.

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


+1  A: 

You could 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 there is only one person object. You will be getting a reference to the _Person object, not a copy, so any changes will be to the single object you're expecting.

Joshua Belden
I am truly sorry but where would this go? In a class file at the root of my App? Would I then need a using or reference on each forms code page?
Refracted Paladin
usually your code would be tucked in a namespace. As long as the class you added was the same as the form, they will be shared and you will be able to access it. Just add a new class to the root of the project.
Joshua Belden
+2  A: 

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

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

public class Person { ... }

If you have a singleton of that, you'll have a single "Person" saved in memory, with a shared reference to that one person in the singleton. When you access your single person, you'll be working with that reference, which is probably what you want. Any changes to the person will change it everywhere.

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?

Singletons are used to basically enforce that every time you use the object, it's the same object (each use is a separate reference to the one, single object in memory). You can just grab the singleton anywhere you need it, and it'll just work.

Reed Copsey
Thank you for clearing that up. I seem to have had by reference flipped around. Maybe, (probably) I am just dense but HOW do I grab it from anywhere? Something like this --> Singleton1 single = Singleton1.getInstance();But on each form that would need to refer to it?
Refracted Paladin
+1  A: 

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

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.

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.

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.

EDIT:

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

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;
    }
}
Aaron Daniels
Whoa, that sounds like it could be what I need. Espeacially since all the other objects are child objects of Person. Can you elaborate or direct me to a good source to explore implementing this?
Refracted Paladin
I've done this with web forms (inherited from Page, then made other pages inherit from MyPage). When trying to do this with Windows Forms, the designer seems to not like it...I'm trying now to get it to play nice with the desinger.
Aaron Daniels
Wow, that is impressive to someone like me. Unfortunately it may also be intimidating. I think I follow what you did in your example but how would I apply this on a large scale? As in Person has a Plan? How does it work with your derived forms?
Refracted Paladin
It's really how you manage your object model. I've just used this technique when I have multiple forms each dealing with one instance of a specific type. Not to beat a dead horse, but this gives you the flexibility of using one instance of a type, regardless of whether it is actually a singleton or not. Each form will deal with one instance. There may be multiple instances of Person in your application, or you may have a singleton. Either way, each form only really cares about the instance that it receives. If you do have a complex object model, this may not be best approach. It's up to you.
Aaron Daniels
After reading your edit, I think this approach may suit you. If all of the child objects that you are dealing with in your forms are nested within Person, this would probably work. Each form with update its Person, then pass it along to the next form.
Aaron Daniels
Thanks, I will explore this one as well then though Derived Forms are as new to me as Singletons!
Refracted Paladin
+1  A: 

As Reed says, singletons enforce that the same object is used throughout the application. However, from your question it doesn't look to me like you do have the same instance of the person class available throughout the entire application, since there is the "search form" which looks like it allows you to change the currently selected person.

In this case your singleton may need to be a container class that holds the current context of the application and which person is currently selected. This may 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 isn't an ideal singleton pattern since it isn't thread safe...)

Then the search form would set the currently selected person with:

Context.Instance.CurrentlySelectedPerson = personSelectedInForm;

And the demographic from can use it like:

//Get the demographics for the current person
ShowDemographics(Context.Instance.CurrentlySelectedPerson);
Martin Harris
When you say "context of the Application" what, precisely, are you referring to? The current person?
Refracted Paladin
I just meant context of the application as a hold-all for the current state. In the case of your example this only holds the currently selected person, but the context would be an object that could hold any data which is scoped outside a specific form and is instead application wide. For example it may include the currently logged in user, the time the application started, whether the interface is in basic / expert mode, or any other information that lives beyond a single form.
Martin Harris
A: 

You could also use the monostate pattern with your Personclass.

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 monostate.

CurrentPerson.Person = GetPersonByUserInput();

And then use CurrentPerson instances throughout the code and they will all access a common shared state.

CurrentPerson currentPerson = new CurrentPerson();
Daniel Brückner
This seems like a really good idea. I would just instance this on each desired Form and it will be pointing at the singular Person? So on Demograhics.frm I would instance it(in constructor??) and then could access the Data stored there like so --> tbx.FirstName = currentPerson.Firstname;Where do I need to initialize the monostate? Does it need to be somewhere speacial? Global? or just on the Searh.frm?Thanks much!
Refracted Paladin
You can initialize it just in the search form or whenever the current person changes. You should of course add some null checking, because CurrentPerson.Person will be null until first initialized. You could also set CurrentPerson.Person to a dummy person very early. Just to note, a monostate is almost the same as a singleton. GetInstance() of a singleton would just return CurrentPerson.Person of the monostate and you would directly work with this person object - so it is just a kind of another way to code a singleton.
Daniel Brückner
I would want everything Public in both Classes?
Refracted Paladin