Skip to content

A concise example on applying MVC and Strategy pattern on legacy Windows Forms

License

Notifications You must be signed in to change notification settings

IanEscober/FormsMVC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FormsMVC

A concise example on applying the Model-View-Controller (MVC) and Strategy pattern on a legacy Windows Forms application. It accepts a simple string which it sorts in different sorting algorithms. The algorithms are dynamically instatiated with the Strategy pattern. Lastly, the UI is updated with adherence to the MVC pattern.

MVC Implementation

The flow of data on the MVC pattern can be decomposed in several parts:

  • Binding of a model on the view.
  • Calling a controller method which invokes a change.
  • Parsing of the model from the view into a model accepted by the controller method.
  • Validating the parsed model (Optional).
  • Returning a view with an updated model.

Implementation Summary

Concept Implementation
Binding View Constructor
Calling Events
Parsing Custom Parsing
Validating Custom Validation
Returning ViewManager

ViewManager

The ViewManager is the core of the MVC implementation. It enables the Views to be updated by closing the stale View with an updated View through an updater function.

public static void UpdateView<T, U>(Form currentView, Func<T,U> updater, T model)
{
    var updatedView = updater(model);
    Update(currentView, updatedView as Form);
}

public static void Update(Form currentView, Form updatedView)
{
    currentView.Close();
    currentView.Dispose();

    var thread = new Thread(() => Application.Run(updatedView));
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

Contribution

Yeet a Pull Request

License

MIT

About

A concise example on applying MVC and Strategy pattern on legacy Windows Forms

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages