1

I have two classess and a Userform. I am trying not to use any Form related code in my classess but i am rather new to OOP. In the CreateGraph() method, i would like to prompt the user with a Yes/No dialog. The code in the method would continue based on the result. I have read some examples on MVP but not exactly sure how i can implement in this case.

Can someone guide me on this? I do believe there is some serious design issues in my code

//singleton class
public class MyApplication
{
    private int state;
    private static MyApplication instance = null;

    public void Task1()
    {
        GraphGenerator gg = new GraphGenerator();
        gg.CreateGraph();
        state = 1;
    }

    public void Task2()
    {
        //some other tasks..
        state = 2;

    }
}

Class where i have issue..

public class GraphGenerator
{
    public void CreateGraph()
    {
        //some code for creating a graph..
        //here i want to prompt the user with a
        // Yes/No dialog box..
    }
}

The userform

public partial class Form1 : Form
{
    private void btnTask1_Click(object sender, EventArgs e)
    {
        MyApplication ma = MyApplication.Instance;
        ma.Task1();
    }

    private void btnTask1_Click(object sender, EventArgs e)
    {
        MyApplication ma = MyApplication.Instance;
        ma.Task2();
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
vinayan
  • 1,597
  • 3
  • 19
  • 42
  • 1
    I only now realize the reason for asking the question. Is GraphGenerator meant to be used in some other project? Clean design is good when it has purpose, but without purpose it's only clean, but not necessarily good. – Dialecticus Jun 18 '12 at 16:30
  • @Dialecticus - I only made up a example to simulate a situation in my project..I am in a situation where improper design may cost me high later when it becomes huge :) – vinayan Jun 19 '12 at 01:06
  • Okay, just take into consideration [KISS and YAGNI principles](http://www.codinghorror.com/blog/2004/10/kiss-and-yagni.html) – Dialecticus Jun 19 '12 at 16:22

3 Answers3

2
  1. Naming - MyApplication - is bad name for controller or presenter, use for example "form name" + "presenter" for naming.
  2. Singleton. Controller or presenter should not be singleton. Inject it through constructor or create in ctor and than save to field or property with private setter in class. For example:

    public Form1(FormPresenter presenter)
    {
       InitializeComponent();
       this.presenter = presenter;
    }
    
  3. All other on this simple sample is normal. But for buttons event handlers you can use events in presenter/controller and fire in button event handler specific to presenter/controller events.

Also try to look for MVC/MVP frameworks. Look here for related question: Implementing MVC with Windows Forms

As I remember there is Microsoft Smart Client Guidance (CAB / Microsoft Composite Application Block) for that.

Community
  • 1
  • 1
Regfor
  • 8,515
  • 1
  • 38
  • 51
1

First , it is best to design your classes as much as possible such that you don't need to intermingle UI code with your domain objects. Can you restructure the code so that the caller/owner of GraphGenerator decides if it needs to ask the user something instead of GraphGenerator doing it? Try to keep GraphGenerator solely focused on his task or making graphs.

Failing that, you could define events (or delegates or callback interface, but lets call these related mechanisms events for now) within GraphGenerator such that the caller/owner can listen for notifications from GraphGenerator and in turn interact with the user. For example, define an event called QueryConfirmSave to raise and the caller can handle the event and prompt the user and then pass back a boolean as an EventArg field.

(The code would be something like this (from the hip, not editor checked):

GraphGenerator gg = new GraphGenerator();
gg.QueryConfirmSave += new EventHandler<ConfirmSaveArgs>(GraphGenerator_QueryConfirmSave);

and then:

private void GraphGenerator_QueryConfirmSave(object sender, ConfirmSaveArgs args)
{
   if (MessageBox.Show("Save?") == DialogResult.Yes)
   {
      args.SaveConfirmed = true;
   }
}
tcarvin
  • 10,715
  • 3
  • 31
  • 52
-3

You need MessageBox.Show(...).

Dialecticus
  • 16,400
  • 7
  • 43
  • 103
  • isn't it against design priciples Calling the System.Windows.Forms.MessageBox object here? – vinayan Jun 18 '12 at 08:40
  • You asked about Yes/No prompt. MessageBox class provides this prompt. I don't know much about design principles. Is your question "how to achieve yes/no prompt" or "should I achieve yes/no prompt"? – Dialecticus Jun 18 '12 at 08:43