Archive for May, 2009

11 May 2009

Creating Your First MVC ViewEngine

13 Comments Uncategorized

A question that I have been hearing a lot lately is:

How do I change the view location in MVC?

But what they really mean to say is:

How do I create a new ViewEngine that uses the view locations of my choosing?

It is actually very simple to do, and once you see it, I think you will agree with my assessment.  The first thing we are going to do to create our custom ViewEngine, is define the paths that we want to use for our master pages, view pages, and shared pages.  I have taken the liberty to define the following paths, you can customize them however you wish:

  • Master Pages:
    ~/Templates
    it use to be ~/Views/Shared or the controllers view
  • View Pages:
    ~/Views
  • Shared Pages:
    ~/Common
    it use to be ~/Views/Shared

The next thing we need to do is create a new class for our ViewEngine, for this example we are going to call it SimpleViewEngine.

public class SimpleViewEngine : VirtualPathProviderViewEngine
{
}

As you might have noticed from above our SimpleViewEngine inherits from VirtualPathProviderViewEngine, this is the root ViewEngine that uses the VirtualPathProvider (VPP). The VPP provides a way for web applications to read files off the file system in their local web application, so it is perfect for what we are doing. If you don’t want a file system based ViewEngine, and maybe want a ViewEngine based from the database, you can use the IViewEngine interface to create your own custom ViewEngine that fits your needs. (MVC is very flexible, by design)

The next thing we need to do is code our paths in to our SimpleViewEngine. We will do this in the constructor, so that they only have to be initialized once for the entire life span of our SimpleViewEngine.

public SimpleViewEngine () 
{
	/* {0} = view name or master page name
	 * {1} = controller name
	 */

	// create our master page location
	MasterLocationFormats = new[] {
		"~/Templates/{0}.master"
	};

	// create our views and common shared locations
	ViewLocationFormats = new[] {
		"~/Views/{1}/{0}.aspx",
		"~/Common/{0}.aspx",
	};

	// create our partial views and common shared locations
	PartialViewLocationFormats = new[] {
		"~/Views/{1}/{0}.ascx",
		"~/Common/{0}.ascx"
	};
}

As you can see the format is pretty straight forward. We create a string[] array with the paths of where our master pages, views, and common views are located. The only thing that we need to do is set place holders in our path so the the VirtualPathProviderViewEngine can replace the master name, view name, and controller name to construct our appropriate path.

  • {0}: is the view name or master page name.
  • {1}: is the controller name.

After we have done the hard part, which honestly wasn’t that hard, of creating the constructor with the paths, we just need to return the view objects from the constructed partial paths. Since we are using the standard ASP.NET Web Form (ASPX/ASCX) rendering engine. We are able to leverage the work already done by the MVC team and just return a new instance of the WebFormView object.

protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
	return new WebFormView(partialPath, null);
}

protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
	return new WebFormView(viewPath, masterPath);
}

Nothing really earth shattering here, just simply filling out the constructor with the proper parameters from our method, and then returning the newly created view. If you wanted to create a view based out of the database, or off your own syntax (meaning not ASP.NET syntax) then you would have to create your own view based off of the IView interface. But for this example we are only concerned with changing where our views are located.

There is one more thing that we need to do, and that is register our new SimpleViewEngine for use in the framework. The registration of view engines is done in the Global.asax, similar to the same way we register new routes.

public static void RegisterViewEngines(ViewEngineCollection viewEngines)
{
	viewEngines.Clear();
	viewEngines.Add(new SimpleViewEngine());
}

public static void RegisterRoutes(RouteCollection routes) { ... }

protected void Application_Start()
{
	RegisterRoutes(RouteTable.Routes);
	RegisterViewEngines(ViewEngines.Engines);
}

So we are now done. You have created a new view engines, defined your own routes, and registered this view engine with the MVC framework. Some other types of paths you may want to consider trying for your applications, using a custom ViewEngine, are special folders for your mobile or Facebook versions of your website.

  • Mobile: ~/Views/{1}/Mobile/{0}.aspx
  • Facebook: ~/Views/{1}/Facebook/{0}.aspx

I told you it was simple and straight forward, and I hope you agree that the MVC team has done an awesome job at providing a very flexible framework for us to tweak and customize it so it fits our applications.

07 May 2009

Google Alerts for Real Life

No Comments Uncategorized

I have always been driven by the idea that computers and to the greater extent the internet should be as transparent as possible to the users.  People shouldn’t care about what the hot new websites are to meet people with similar interests.  In fact I think this is most of the problem why people are becoming more and more disconnected from each other on a personal level.  And more and more disconnected from the human experience, of social interaction.  There is so much communication that is lost when you cannot see a person face to face, and experience things in the same environment that they do.  Wouldn’t it be great if you could find and meet new local people with similar interests, hobbies, and ambitions, whatever they maybe.  The Internet currently excels at all of this even getting people physically together to meet.

However, this requires a lot of active participation of the person searching and trying to find the right website where everybody of a particular interest meets, and then you need to find somebody locally who is willing to meet up for dinner or a gathering of some kind.  This is a lot of work to find people who are interested in the same stuff as you are.  And it is no wonder so many people just give up.  Now imagine that barrier is removed and finding people with similar interests, hobbies, and ambitions is a passive activity.  Where you are automatically alerted to a new person or group in your area that shares your interests, sort of like Google Alerts for real life.

I would really like to start working on this problem with a small group of developers and people that share a similiar passion to physically connect people in a meaningful way with other people that share their interests.

03 May 2009

Windows 7 Blame Feature

1 Comment Uncategorized

Over this past weekend I installed Windows 7 (64-bit), and I have to say that it is a great upgrade from Windows Vista that nobody should be disappointed with. Even as a release candidate it is rock stable and has been able to cope and recover from many of the failing drivers I tried this weekend, with out the need for a single reboot. This is impressive, because I remember when I first tried Windows Vista as a beta almost 3 years ago, that I received blue screens left and right from faulty drivers that didn’t yet support Windows Vista. Blue screens have always really bothered me, because as a avid Windows supporter, I have heard all too often the mantra of the uneducated.

It’s Microsofts fault that Windows blue screens every time I try to play a game on my graphics card.

What this uneducated user doesn’t understand is that Microsoft isn’t really at fault, it is the fault of the hardware manufacurer that poorly developed the drivers.  But up until Windows 7 a poorly programmed driver would result in a blue screen and Windows would have no way of letting the user know what was going on until the operating system had been restarted.  Now in Windows 7 bad programming by hardware manufactures are caught in real time with out rebooting.  Windows 7 gracefully handles the error, and displays a “blame message” line the one listed below.

windows-7-blame

Windows 7 shows a small dialog explaining that something has happened and that it was able to recover from the error without effecting the user.  It even displays the name of the device that caused the error, so that users can better understand what has happened to their system.  I love this feature because it provides transparency to the user, so that they can blame the correct party for the failure of their computer.