Saturday, December 28, 2013

Creating an MVC framework part 2 - First Decisions and Code

In writing the first bit of code a quickly came across a few key decisions. The very first one was what sort of routing I wanted to include. This lead to a deeper question of what sort of applications I wanted the framework to be suitable for.

My career has largely been spent creating line of business applications and that is the priority focus of the framework. Eventually I would like to expand on that but with limited time you have to simplify or get no where. I also didn't intend for it to be particularly opinionated but I'm naturally getting pushed that way. Again I hope to make it more flexible in future but for now it's simply not a priority.

The first casualty of this is routing, otherwise known as pretty URL's. This feature is simply not that important in a line of business app and it simplifies things to not have to worry about it at this point. For now, the only route is /{controller}/{action}/{querystring}.

The other big decision I made is project structure. I'm sick of having various parts of an application strewn about the project, or several projects, based on type. The grouping of this framework is going to be done by function. A standard project layout will be along the lines of:

  1. MvcProject
  2. -\_Common
  3. ---_Layout.cshtml
  4. -\Product
  5. ---_Controller.cs
  6. ---List.js
  7. ---List.cshtml

Let there be Light

So I created a new web application and in the global.asax.cs I put:

  1. protected void Application_Start(object sender, EventArgs e) {
  2.   RouteTable.Routes.Add(new Route("{*url}", new RequestHandler()));
  3. }
This tells ASP that we want to handle all incoming URL's with our request handler, which is all I want for now. RequestHandler looks like this:

  1. public class RequestHandler : IRouteHandler, IHttpHandler {
  2.   IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext) { return this; }
  3.   bool IHttpHandler.IsReusable { get { return true; } }

  4.   void IHttpHandler.ProcessRequest(HttpContext context) {
  5.     context.Response.Write("Hello World!");
  6.   }
  7. }
IRouteHandler and IHttpHandler are the interfaces we need to implement to recieve requests from ASP. I'm not sure if it's a good idea to combine them or not but it works for now. The real meat and potatoes starts with ProcessRequest though, it might not look like much but this is the entry point to our framework. This is where the adventure begins.

No comments:

Post a Comment