Opinions: View() or Window()/Page()?

I am after opinions on an API change to Magellan.

When you write actions on a controller, you can return a ViewResult with the name of the view you want to show. For example:

public ActionResult Show(int customerId)
    var customer = repository.GetCustomer(customerId);
    Model = new CustomerDetailsModel(customerId);
    return View("CustomerDetails");

Magellan will then invoke a list of View Engines to track down the view. For example, it will look for:

  • CustomerDetails.xaml
  • CustomerDetailsWindow.xaml
  • CustomerDetailsPage.xaml
  • CustomerDetailsView.xaml

When it finds a match, it will use the view type to "render" the view. Windows will be shown via Window.Show(), pages will be navigated to via the NavigationService, and so on.

What this means from the Controllers point of view however is that the controller action can't be sure what kind of view will get shown; and therefore can't (easily) add view-specific arguments. For example, with a Window, it would be nice to state whether the Window should be shown as a modal dialog or as a non-modal window.

Now, the ViewResult class does store a dictionary of custom parameters that the view engines can use. So this means extension methods can easily be used to specify the settings above. For example:

return View("CustomerDetails").AsDialog();

What I don't like about this is we're mixing the generic concept of a view, with the specific concept of a dialog. Which makes the first kind of useless. So I'm considering changing the API's to use more specific results:

return Window("CustomerDetails");
return Dialog("CustomerDetails");
return Page("CustomerDetails");

Unit testing the controllers will be just as easy as they currently are, but it should make the controllers a little less ambiguous. What do you think?

Update: based on feedback I have gone with the seperate return actions instead of just View(). You'll find this in the latest build.

A picture of me

Welcome, my name is Paul Stovell. I live in Brisbane and work on Octopus Deploy, an automated deployment tool.

Prior to founding Octopus Deploy, I worked for an investment bank in London building WPF applications, and before that I worked for Readify, an Australian .NET consulting firm. I also worked on a number of open source projects and was an active user group presenter. I was a Microsoft MVP for WPF from 2006 to 2013.

26 Nov 2009

I think that it's ok, because the controller should know what kind of view it is returning.

01 Dec 2009

I would altso prefer the return Window(); Page() Dialog(); pattern. It would translate well to return Json(); or return Partial(); in asp.net mvc. Great work sofar!