tags:

views:

1095

answers:

3

So I am creating a C#/Winforms application in a model-view-controller pattern. My view-controller needs to instantiate multiple groups of objects. The object groups are elements of the model and elements of the view. So for example a textbox and the model to go behind that text box.

I'm wondering if the best way to do this is to put everything in some sort of collection and tie them together with a key?

A: 

Have you considered using WPF instead of WinForms? It has a nicer MVC-like model and built-in databinding that is much more powerful. It would probably solve your problem and help you build with more modern technology besides.

Andrew Arnott
I would like to but for the time being I am stuck with Studio 2005 and WinForms. I guess the question is a pretty generic MVC one but I'd like to know the C# best practice.
+1  A: 

In the WinForm MVC apps I've built, I typically don't allow the controller to instantiate anything (I try and keep the "new" keyword out of my controllers).

If I need an object I ask a service for it, and internally that service is going to fetch that object from a data source (repository or the like), or in the case of a new object it will likely utilize some kind of factory to get me a new object (with any necessary pre-filled properties already set to default values, rules run, etc.).

The way I like to think about this sort of problem is this: how would I make this work (and make it reusable) if I didn't have a GUI for a view, but instead had a command-line input for my view? The logic to create/add/delete/update the model should be somewhere in your domain, not in the controller. Then the controller just becomes the mediator between the model and the view. The view becomes an I/O mechanism that is just a prettier version of a command-line interface.

Hope that makes sense.

Chris Holmes
A: 

Perhaps you should design your model to match what the view needs? Then there would be only one model for the controller to pass on to the view.

Thomas Eyde