Model *View* Controller
July 31, 2008 · 4 minute read
A lot is being made of Microsoft’s Model View Controller (or MVC) framework at the moment.
I have only spent a couple of days working with it so far but it does look promising. If you are a TDD convert or just interested in solid OOP principles such as separation of concerns it may well be right up your street.
As with all change, the emergence of this new approach to asp.net programming has caused its fair share of controversy and debate but that’s for another day. Lets just say there are those who are yet to be convinced of it’s value and those who are already sold on it.
For today though I want to focus on the View part of the equation.
Interestingly I think Scott Guthrie may have had a bit of a revelation one day when he was posting about MS AJAX on his blog. Whether MVC was already in development or not, this post does seem to lean neatly into what we’re now all talking about.
Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel Scenarios
I have recently adapted the technique mentioned in that article for an AJAX web site. Lets look at some code (heavily based on Scott’s original post).
- public static string RenderView(string path, object[] data)
- {
- HtmlForm tempForm = new HtmlForm();
- Page pageHolder = new Page();
- pageHolder.EnableEventValidation = false;
- pageHolder.EnableViewState = false;
- UserControl viewControl = (UserControl) pageHolder.LoadControl(path);
- if (data != null)
- {
- Type viewControlType = viewControl.GetType();
- PropertyInfo propertyInfo = viewControlType.GetProperty("Data");
- if (propertyInfo != null)
- {
- propertyInfo.SetValue(viewControl, data, null);
- }
- else
- {
- throw new Exception("View file: " + path + " does not have a public Data property");
- }
- }
- tempForm.Controls.Add(viewControl);
- pageHolder.Controls.Add(tempForm);
- StringWriter output = new StringWriter();
- try
- {
- HttpContext.Current.Server.Execute(pageHolder, output, false);
- }
- catch (Exception ex)
- {
- Debug.WriteLine(ex.Message);
- throw;
- }
- string outputToReturn = output.ToString();
- outputToReturn = outputToReturn.Substring(outputToReturn.IndexOf(""));", StartPoint) + 6;
- outputToReturn = outputToReturn.Substring(0, outputToReturn.IndexOf(""));
- int StartPoint = outputToReturn.IndexOf(");
- if (StartPoint >= 0)
- {
- int endPoint = outputToReturn.IndexOf("
- outputToReturn = outputToReturn.Substring(endPoint);
- }
- return outputToReturn;
- }
This function is pretty straightforward. Given a path to a user control and some data in the form of an object array it will attempt to render the user control and spit out an html string.
NB: To avoid tripping up asp.net we wrap our user control contents in a form prior to rendering, then remove the form afterwards.
Expose this as a method in a class (ViewManager) and you’re ready to go.
Now to an example of a view.
- <table>
- <tr>
- <td>
- <b>Customer Name:b>
- td>
- <td>
- <%=data.Name %>
- td>
- tr>
- <tr>
- <td>
- <b>Date:b>
- td>
- <td>
- <%=DateTime.Now%>
- td>
- tr>
- <tr>
- <td>
- <b>Date Registeredb>
- td>
- <td>
- <%=data.DateRegistered%>
- td>
- tr>
- table>
Note the strongly typed access to the data’s fields. This is possible due to the code-behind for this view.
- public Customer data;
- public object[] Data
- {
- get { return new object[] {data}; }
- set { data = value[0] as Customer; }
- }
I have explicitly stated the type of the data object (Customer).
You’ll also note I have used an object array rather than a single object. This just adds a bit of flexibility as you can pass in more than one object to bind the view to.
Interestingly ASP.NET MVC supports strongly typed data access using generics (example below)…
- using System.Web.Mvc;
- using Shop.Data.Model;
- namespace PartsMatching.App.Views.Rule
- {
- public partial class CustomerDetails : ViewPage
{} - }
The real beauty of this templating approach though (and what Scott originally alluded to) is that you can render this html and then view it however you want. Once class I’ve used sends the rendered view out as an email.
With MS AJAX I’ve used a ModalPopup control from the ASP.NET AJAX Control Toolkit to display the html.
- function ShowPopup(contents) {
- var infoPanel = $get('modalContents');
- infoPanel.innerHTML = contents;
- $find("ModalPopup").show();
- }
The similarities between this approach and MVC are fairly obvious. You can have controllers which render the view using the RenderView method and then do what you like with the resulting HTML.