0

I have multiple winforms which interact with each other. One form allows the user to enter a set of properties and then validates them and with a button click moves onto the next form. The user may choose to back out and go to the previous form at any point of time.

I have a Business Object Class that records values for essential properties.

Now originally, I was using simple, naïve parameters to exchange values between forms. For example, lets say Form1 and Form2. On button click "Next" Form1 is hidden and Form2 is displayed:-

   // event of button click "Next" on Form1.cs //
    private void btnNext_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2 (myTxtBoxValue.Text);
        form2.Show();
        this.Hide();
    }


    // constructor in Form2.cs //
    public Form2(string myTxtBoxValue)
    {
        InitializeComponent();
        TransactionBO trans = new TransactionBO();
        trans.myValue = myTxtBoxValue;  // <-- this gets the value accurately. 
    }

    // my Business Object class is called TransactionBO.cs //
    class TransactionBO
    {
       public string myValue {get;set;}
    }

However, it was suggested to me that a better way of doing this would be to use singletons. The reason, 'it is better not to bind form2 to form1, creating dependencies might reduce flexibility/scalability in the future'.

Can someone explain to my why? and how may I use Singletons (I know what Singletons are in its basic principle, do I have to create a separate singleton class or can I use my TransactionBO.cs class to pass values between forms)?

8
  • What you described looks like wizard control. Are you implementing that kind of stuff? Feb 21, 2017 at 23:06
  • Isn't it better using multiple panel in one form? Feb 21, 2017 at 23:07
  • You can use a Wizard-like control. You can have a single model object which is used among different pages of the wizard. Controls (or user controls) which are hosted in each wizard page are in fact different Views which you want to use for your model in each step. Feb 21, 2017 at 23:07
  • 1
    Your colleague is about 10 years behind the curve. The singleton pattern is now considered an anti-pattern and has fallen into disuse, in part because it poses huge problems for unit testing.
    – John Wu
    Feb 21, 2017 at 23:09
  • 1
    @OP, there are a number of techniques described in this answer. Yours isn't terrible; it wouldn't be my first choice, but I wouldn't bother to rewrite it if it's already mostly done.
    – John Wu
    Feb 21, 2017 at 23:13

1 Answer 1

2

Let's address your colleagues' concerns about dependencies.

Just to level set, let's look at some topological graphs. Imagine that these are dependency graphs.

Various graphs

Your current design, depending on the details, will have a dependency graph that is either a line, a mesh, or is "fully connected." All of these graphs have the property that code changes at any single node in the graph will require the examination and possibly changes in numerous other points, and in some cases all of them will have to be looked at. This is bad.

What we'd prefer is a simpler graph. A tree, for example, is much simpler. but the very best is the star. In the star graph, you have only pairwise dependencies; a change to any one node requires the examination of at most one other node. This means that there is far better isolation and resilience to modification.

So, how do we get a star graph in your case? Here's how I would do it.

  1. Define a new class that represents the overall workflow. If it's a workflow to enroll in a service, for example, you might call it EnrollmentWorkflow. in this example I'll just call it Workflow.

  2. Give Workflow a series of fields and public properties that represent every data item you need to collect from the user.

  3. In the code that starts the multi-step user interface, create a new Workflow instance and pass it to the first form's constructor.

  4. In the event handler that fires when the user finishes a form, add code to copy all of the form's fields into properties of the Workflow. Then instantiate the next form, passing the same Workflow instance in the constructor.

  5. In the next form, you can read all of the values needed from the Workflow object.

  6. Continue like this until the last step is completed, at which point you would pass the Workflow to your business logic layer for processing.

  7. If you like, you can add helper methods to the Workflow for things like data lookup, field validation, etc. so that they are all contained in one place.

  8. If a user needs to save progress and resume later, it is a pretty simple matter to persist the Workflow as a serialized object, with only a small amount of work.

In the end, the Workflow object will be the center of the star graph, and the forms will be the nodes on the perimeter. This way you end up with a nice, simple dependency graph, and a good design that is easy to modify.

2
  • Thanks! This was my initial idea. But I feel like I don't need 2 separate classes. TransactionBO has all properties and methods required to complete my transaction. I am assuming that if I have a TransactionBO obj that I pass around from Form1 to Form2 then finally validate and process all user input stored in TransactionBO, then my workflow is maintained and stored in one object. Your thoughts?
    – Philo
    Feb 22, 2017 at 0:05
  • 1
    Yeah that should be OK for starters (it's not like we're building Apollo 11 here). Maybe define an interface on TransactionBO that has only the fields you need, and use that instead... that way if you want to add a separate Workflow class later it'll be easier to refactor.
    – John Wu
    Feb 22, 2017 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.