Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently working on a bigger WinForms project. I used to program small applications using WinForms that had a maximum of ~20 different windows.

Now I am facing a bigger project and want to adapt to that by overthinking my approach to that.

I have read about MVVM and MVC and some variations.
Speaking of variations: I found so many variations, that I have no idea which implementation they originated from.

My Idea is that it has:
-easily scalability
-clear seperation of concerns (SoC)
-an intuitive way of usage

Some side-notes on this project:
-Hotkey-driven navigation (every view can have multiple hotkeys to navigate to other views)
-Fast response time required (the client does not like to wait for a query to finish longer than 1 second.)

Below you will see my current idea of how to use a pattern.
I am very thankful for criticism and/or tips on this.
Also I would be very thankful to hear your suggestions on how to use this pattern and if any traps exists how to avoid them.

Thank you in advance :)

What I have tried:

So far I have designed the following pattern as an approach:

Classes:

View
C#
void SetViewModel(ViewModel viewmodel);
void Clear();

Thoughts:
The View only has to accept a ViewModel class to display.
All logic in the view is for registering key events and button clicks etc.
If something like that happens a event is fired or a command redirected to the Controller.

Controller
C#
void AddView(View view);
void RemoveView(View view);
void SetModel(Model model);
void SetViewModel(ViewModel viewmodel);

Thoughts:
My controller will contain all decision-relevant logic. (example: If button X is clicked on View - Open new View)
It will accept views (one or many) and update every View.

ViewModel
C#
event PropertyChangedEventHandler PropertyChanged;
T Get<T>(string property);
void Set<T>(string property, T value);

Model
C#
event PropertyChangedEventHandler PropertyChanged;
T Get<T>(string property);
void Set<T>(string property, T value);

Thoughts:
Model and ViewModel are almost similar.
They contain the values of the entity and fire events when a property changes.

Converter<TVM, TM>
C#
TM ConvertToModel(TVM viewmodel);
TVM ConvertToViewModel(TM model);


Thoughts:
My Converters will be responsible on converting models from ViewModel to Model and back. They will need to handle view-relevant conversions and formatting specific things.

Conclusion:
I am unsure how to identify implementation traps and possible faulty design choices beforehand and require some guidance on how to approach a big project with a neat pattern to follow.
Posted
Updated 6-Dec-18 8:37am

DZone has a nice refcard about the original Gang of Four design patterns which you can download for free (after signing up):
https://dzone.com/refcardz/design-patterns?chapter=1[^]
I also stumbled upon this article on DZone about the Factory pattern by a well known CodeProjector (Richard McCutchan): https://dzone.com/articles/design-patterns-c-factory[^]
The Factory pattern is one of the most useful I think especially if you want to decouple classes in a big solution.
 
Share this answer
 
v2
Comments
CursedProgrammer 8-Dec-18 21:13pm    
Working through the PDF really helped me with the implementation of the base of the project. Thanks for the link. I accept this solution because it helped me a lot already.
Maybe you can use custom forms, something like this:
public class CustomForm : Form
{
    /// <summary>
    /// Initializes a new instance of the <see cref="CustomForm"/> class.
    /// </summary>
    public CustomForm()
    {
        // Required for Windows Form Designer support
        this.InitializeComponent();
    }

    private void CustomFormLoad(object sender, EventArgs e)
    {
        this.BackColor = SkinSettings.FormBackgroundColor;
        this.ForeColor = SkinSettings.FormForegroundColor;
        this.Font = SkinSettings.FontDefault;

        if (this.Text == @"CustomForm")
        {
            this.Text = SkinSettings.ProductName;
            this.Icon = SkinSettings.FormIcon;
        }
    }
}
 
Share this answer
 
Comments
CursedProgrammer 6-Dec-18 10:33am    
Hello RickZeeland, thank you for your answer.

I have tinkered with a similar idea too.

But lets go back to the conceptual question here please:

Apart from the implementation details, do you know an idea on how to organize a big project with such a pattern?
Or do you have ideas on how to modify this pattern to make better use of specific features of WinForms? (e.g. DataBinding)
RickZeeland 6-Dec-18 13:10pm    
I'm afraid I'm no fan of MVC or MVVM, and agree with Gerry that you should not become a slave of such a pattern, but that might be because me and my colleagues have to struggle with a legacy MVC application of a developer that has the left the company and has given us many headaches.
To me Windows Forms is already MVC as the code and form design are in separate files ! But all this might be my biased view of course ...
[no name] 6-Dec-18 12:44pm    
"Refactor" your "old" project and make it the "template" for your new project (based on lessons learned).

Starting from "scratch" now (again) will have you spinning wheels most of the time.

And being a slave to MVVM (when you can't answer why) makes for a mediocre (herd) app.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900