views:

129

answers:

1
+1  Q: 

Windows Form MVC

Hi, I am building a Windows form application using .NET 3.5. I would like to use the MVC design pattern. The app has two forms: a quick launch and a view showing the details. The quick launch just has a tree with 2 levels, so the tree structure looks something like

  • Category 1
    • Subcategory 1-1
    • Subcategory 1-2
  • Category 2
    • Subcategory 2-1 etc,

When the user clicks on a category or subcategory, the details view displays all items belonging to that category or subcategory. An item can belong to only one of category or subcategory (the items in my app are really documents such as orders, invoices, etc). Category's item and subcategory's item have almost identical properties but there is still a bit difference.

I have a controller class that handles the click event raised by the quick launch. The controller determines whether the user clicks on a category and subcategory, then gets all the items that belong to that category or subcategory. It then sends all the items to the details view to display them.

My questions is that is it better to use one generalized details view or create a view for each of category and subcategory? If I were to use one generalized view, I need to show/hide a few fields depending on whether the items belong to a category or subcategory, which might make it harder to maintain if category's item structure and subcategory's item structure becomes more different in the future.

If I were to create a view for each of category and subcategory, the controller needs to add the category view and remove the subcategory view when the user clicks on a category node. This is what bugs me because the controller needs to add/remove view from the main form when the user clicks on a node, which doesn't look like a good design to me.

So neither seems like a good design. What's the correct solution to this problem?

EDIT: Which part is too vague. Basically, the user can see two types of items: CategoryItem and SubCategoryItem. SubCategoryItem is a child class of CategoryItem. In MVC pattern, do I need 2 views (call CategoryView and SubCategoryView) and the controller determine which view to render to the user? Or just one view so the controller always render this view and it's the view's job to determine whether to display CategoryView or SubCategoryItem?

+1  A: 

Without knowing much about the specifics of your code, it seems to me like the problem isn't your controller, but your view.

It sounds like your view is a prime candidate for subclassing. The controller sounds like it knows far too much about the different types of views. (Then again, that may be the way you want it.) But if you have two different views that are so similar in design except for a few specific aspects, it sounds like they should share a common base class, and implement their differences in subclasses. The controller should probably invoke a common method and tell them to differentiate themselves through that method invocation, passing them whatever information they need.

That's just a wild guess based on what little information I can glean from your post.

Mike Hofer
Yes, I think you got what I am asking. Please see my edit. So the controller shouldn't know there exist CategoryView and SubCategoryView?"The controller should probably invoke a common method ... " - where does this common method belong to? The controller or the view? It seems like I need another layer between the controller and the view.Thanks for your help
David
Create a base class for your views. But a virtual method on this base class. Each distinct view must override it. The controller will instantiate an appropriate instance of the view, and invoke the method on the distinct instance, and let the view handle the specifics.If the distinct instances need more information to get the job done, pass that data in as an argument (perhaps a struct or a small class).
Mike Hofer
That should have been "Put a virtual method on this base class." Further, the info you pass to the derived classes should be fairly generic; probably a reference to a class that implements an interface. That way, the base class's method declaration is nice and generic, and the derived classes are free to cast appropriately and extract what they need.
Mike Hofer
Sounds a good and clean implementation. Thanks.
David