Controller/Static State Class in WinForms Application - Where to put? - c#

I'm writing a WinForms application and want to have an "MVC-Type" Design. Actually it's more MVP or MVVM,.
The plan is to have a Central Controller which does all the actual work, so that the Forms just render out ViewModels and handle user input, but everything that actually does something goes through the Controller.
I just wonder if this is a good idea, and where to put the Controller? The current idea is to have a static class which is initialized in Program.cs (Sending in some Dependencies like IMyDatabaseRepository) so that it just stays a controller that delegates work between User Interface and Model.
As you might guess, I come from a Web Background and have little experience with WinForms architecture. Previously, my MainForm was the Controller class, holding all the State Variables, which obviously means that my MainForm is my application rather than just a part of the User Interface.

Nice question Michael!
Here are some links:
Sacha Barber's WPF MVVM VS Project Template
Sacha's Article Series on CodeProject.com
Nice article on this Wordpress blog
Hope these help you to structure your project properly!

I don't know if this is a better way, but I am having Structuremap create my controller and database instance.
The main form has no real code in it - it just loads the first set of controls and then starts the controller. The user controls on the form use StructureMap to access the controller.
My project is regular WinForms and not WPF and is my first time using the MVC pattern with WinForms.

You might have a look at the WAF Windows Forms Adapter download. It comes with the BookLibrary sample application which uses a Controller / MVVM design together with Windows Forms.

Related

Winform control code placement

I'm adding a bar manager and popup menu control to a Winforms application. I have to add the code to bind the menu to the manager, but I don't know where it would be best to do so.
I'll be using the designer heavily (company mandated) for the rest of the build-out, but the binding has to be done in code AFAIK. Currently I have it in the form load method.
I believe this is just fine to make it work, but I'm curious if you could put it in the designer code with the control details, or if it should go somewhere else in the code behind.
Hopefully this isn't an opinion based question.
Winforms doesn't make it very easy to separate things correctly, but you should try to separate as much as possible the UI code from the functional code.
Basically, you should try to put all your business logic in classes that are separated from your UI. Try to think that all that code could be used by another type of application, like a web app, or a WPF app.
The things that are in the codebehind should be only related to UI management, updating the UI and passing the changes to your business classes. There also seems to be some things that exist to have a MVVM or MVP on Winforms, check this SO question: UI Design Pattern for Windows Forms (like MVVM for WPF).

Are MVC Partial views like WebForms Controls?

Ok, the question title doesn't really reflect the question that well. But here it is in all it's glory.
I am currently making the transition from WebFroms to MVC as everyone seems to want it without really knowing why. Office politics aside.
Am I right in perceiving that a partial view is "like" a webform control in that it is an almost self-contained unit that adds to the overall _layout? IF not - could you please tell me the point and rationale of Partial views - where to use them, when and why?
Sorry not the best explanation - but it is kind of confusing for me, so it's not suprising that my question is confused as well :P
Thanks in advance.
In the web forms philosophy controls are hold UI part and some backend logic, so you can create a combobox control which always display the list items from some DB table with special stile for e.g. always in red rectangle with blue background
In the MVC philosophy partial views just an a view part, so according to the example above particular view will hold only UI component, the rest of the logic (data provider) should be in the controler's action.
MVC decouple UI from data providers, so you can create other view which still can work with previous defined data provider (action) and vice versa you can use the same partial view (view) for other data providers (actions) which have some interface
In a typical win form, you have a form that contains a few custom controls, each custom control implements part of a whole business logic, when the custom control wants to talk to each other, they will pass data across via presenter. Also, each custom control is a combination of windows standard controls(button, label, textbox,etc). In such case, you can think that each standard control is a html element, custom control is partial view, the whole form is a view and presenter is action method + ajax call.
MVC and WebForms are both programming models.
Comparing partial views to Webforms is not comparing apple to apple.
You have to think different when it comes to MVC or even forget about the WebForms
The closest thing to a partial view in webforms would be master pages in my opinion. And that is just the aspx markup aspect (View) of it.

Reducing Complexities in Single form Application

I've created a win form application which consist of a single form. We have 8 tabs to access the modules of application.
The problem is we are a team of 4 who works on this project. But since it is a single form application, only one person can use the file at a time. Is there anyother way to build application with more than one file?
Please provide some solution.
Firstly, you should probably have a separate UserControl per tab. That will give you 8 files (at least) since you have 8 tabs.
Secondly, you should be using a Model-View-Controller style architecture for Windows Forms applications. That will give you at least one controller, but likely you will have one controller per UserControl (i.e. per tab). You might even have an overall controller that manages the per-tab controllers.
You might only have one data model for the entire app, or you might have one data model per UserControl (tab).
If you did all that, you'd have a few more source files.
However, it's actually difficult to say without knowing anything about your app.
Try using user controls to make each tab modular.
Figure out what are the parameters that each tab accepts and that it exposes and then create user controls that have that behavior.
Here are couple resources to get you started
http://msdn.microsoft.com/en-us/library/aa302342.aspx
User Control vs. Windows Form
User Controls in Windows Forms - Anything similar to ASP.NET User Controls?
Even if this is a giant ball of wax, your source control tools are shoddy and breaking it up into separate classes is hard to do, you can still take advantage of a Form class being a partial class. Which means that you can spread the code over any number of source code files, not just the two files that the designer creates. So a logical organization is to move code that belongs to a particular tab in its own partial class with the same form class name and its own source code file. Some cut+paste required however when you add event handlers with the designer.
Have you considered using MDI?
MSDN Working with MDI...
Examples are in VB.Net but I'm sure it will be easy to use C# if you really want to - I'm not sure why, but... :)

how to create windows application with mvc

hii is it possible to create windows application with mvc
MVP (Model-View-Presenter) and MVP-VM (Model-View-Presenter-ViewModel) is used most often with WinForms.
See SO question: MVP examples for Windows Forms
Dan Bunea's Blog Post: Model View Presenter
Jeremy D. Miller's Blog Post: A Simple Example of the "Humble Dialog Box"
Referance: Mitch Wheat
MVC the pattern: definitely yes! That's platform independent, really.
See some resources:
Looking for clean WinForms MVC tutorial for C#
Selecting a MVC/MVP Implementation for a Winforms Project
Mvc for Winforms - Mapping the View event to the Controller action
Keep only user interface code in the Form object(s). Any interaction between the UI and the data or manipulation of the data should go in its own controller class(es). The domain objects or the data itself is the model.
Read the http://www.objectmentor.com/resources/articles/TheHumbleDialogBox.pdf but you may want to look at WPF and MVVM

MVC in c# win. form application

I 'am building a small windows form application.
I have a View - a simple form that has some functionality in it.
a Controller - a class that will react to loading, saving, getting data from the model and prepare it form the view etc.
a Model - a class that will get and save data to DB or file.
The controller is creating a form instance an run it, and creating a model instance when needed.
I wonder about how to react in the controller to the view events.
Should I register to the view events (buttons click, combo change etc.) ?
This will make the form controls visible outside the form?
Maybe I have a mistake in the design?
Microsoft has created a framework for Win Forms MVC applications, the Composite UI Application framework.
http://www.codeplex.com/smartclient
It is probably overkill for a small project, but you could look at it and get some ideas.
Can you take a look at this SO post
using MVC MVP patterns in winforms
If you google MVP (Model-View-Presenter) you'll find info on how to implement a seperation of concerns pattern in a Windows form application.

Categories