I'm making a Windows Forms app:

  • It is client-server. Authorization, then load some data from server, then send back some statistics from time to time.
  • You should go through a bootstrapping procedure, then it starts computations (a separate process). Then you can pause it anytime, probably scheduling it to start computing again, or quit it completely.

My code is full of checks like "Is this data available? Load it from the server." or "Is this data available? Prompt it from the user." or "Is a start is scheduled? Cancel it, start now.".

I feel like my app is a state machine. It's like it has a state like this: AUTH / WORKING / PAUSED / PAUSED_SCHEDULED / SHOWING_ERROR_MSG plus a bunch of data availability flags. My code initiates transitions from state to state and performs them, is basically tries to keep the app in a correct state. The code is messy with data availability checks, long bootstrapping in every single method, tons of fail-retry code everywhere.

Are there any design patterns and frameworks I can use? Is there something good and widely used? Are there any caveats I overlook? It feels like my code could be a level more high-level.


Without specific context it difficult to give an advise about best approach.
Based on the information provided you have few main states of your application

  • AUTH

You can introduce abstraction of all states, so every implementation of this abstraction will have responsibility only for the logic of correspondent state.

Main goal of this approach move "validation" to the most possible higher layer of your application.

public interface IState
    void DoJob();

public class Authorization : IState
    public void DoJob()
        // Don't need any validation -> 
        // only use validated result for this particular case
        // do authorization logic

// And so on for other states

If for example WORKING state have different logic based on the data validation results - you can introduce own implementation of IState for every possible branch of validation result

public class WorkingWithData : IState
    public void DoJob()
        // do working with data logic

public class WorkingWithNoData : IState
    public void DoJob()
        // do working with no data logic

Definitely a candidate for Model-View-Controller pattern.

Some of the complexity in your applications, represented via flags, will go away if you can identify different MODELS/VIEWS of your application.

From wiki:https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

The model directly manages the data, logic, and rules of the application. A view can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants. The third part, the controller, accepts input and converts it to commands for the model.[7]

As pointed in the comment a specialization of MVC, ModelViewPresenter pattern will be even more suited to this scenario. https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern, and is used mostly for building user interfaces. MVP is a user interface architectural pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic:

The model is an interface defining the data to be displayed or otherwise acted upon in the user interface. The presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view. The view is a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.

  • 2
    OP should note that the flavor of MVP that works best for Winforms is Model-View-Presenter. – RubberDuck Dec 20 '16 at 9:36
  • Agree. Edited answer to mention that. – Ajeet Ganga Dec 20 '16 at 9:46
  • Sorry, I don't understand how patterns which separate UI logic and "business" logic will help in the OP question. Even with MVC/MVP OP still must deal with same validations logic everywhere – Fabio Jan 1 '17 at 11:53
  • Besides Winforms support Data-Binding which give possibility to use MVVM pattern which on my personal opinion is most suitable for separation UI and business logic – Fabio Jan 1 '17 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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