Craig’s Framework

5/20/2012

For the past two weeks I've been working on a project that I mentioned here.  Basically what I want to ultimately do is create a system that combines my data from various sources into one location so I can easily access it.  Depending, at least with some of the items, I may simply develop a replacement that better suits my needs but much of it is going to be simply calling 3rd party APIs.  So for two weeks (in the little spare time that I've had), I've been setting the foundation that I'm going to use to build the system.  This is a rather simple process for me, because for the past three years or so, I've had a basic framework that I put together to simplify my development process. This time around though I need to make some modifications to it (it was aimed at web forms and this time around I'm using MVC for the project) and since I'm making modifications anyway, I figured I might as well make it open source (similar to my utility library).

Unlike my utility library, this is going to include a number of 3rd party libraries (Json.Net, etc). As such it's going to be a bit more opinionated and a little less open than the utility library. Also it's being retooled to be aimed more at a single page style application (not using the SPA template that's going to be coming out in MVC 4). So it might not be as useful to everyone else and simply be a public NuGet package that I use myself... Anyway, it still needs a bit of work but I might as well show a bit of code:

   1: /// <summary>
   2: /// Sets the content type
   3: /// </summary>
   4: [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
   5: public class ContentType : ActionFilterAttribute
   6: {
   7:     #region Properties
   8:  
   9:     /// <summary>
  10:     /// Content type to set for this item
  11:     /// </summary>
  12:     public virtual string Type { get; set; }
  13:  
  14:     #endregion
  15:  
  16:     #region Functions
  17:  
  18:     /// <summary>
  19:     /// OnActionExecuted
  20:     /// </summary>
  21:     /// <param name="filterContext">filter context</param>
  22:     public override void OnActionExecuted(ActionExecutedContext filterContext)
  23:     {
  24:         filterContext.HttpContext.Response.ContentType = Type;
  25:         base.OnActionExecuted(filterContext);
  26:     }
  27:  
  28:  
  29:     #endregion
  30: }

Many of the classes in the framework are simply helper classes, like the one above. So, for instance, above is an ActionFilter that simply lets me place an attribute on a class so I can set the ContentType that will be returned. I have others that force all requests be forced over to SSL, one that adds a variable to a controller saying what the HTTP verb is, one that tells the client to cache the content that it receives, etc. These sorts of classes probably comprise about 50% of what's in there at present. The other half is base classes. For instance, I have two base classes for controllers. The first is aimed at APIs and controllers that will mostly be dealing with JSON:

   1: /// <summary>
   2: /// API Controller base class
   3: /// </summary>
   4: [ContentType(Type = "application/json")]
   5: public class APIControllerBase : System.Web.Mvc.Controller
   6: {
   7:     /// <summary>
   8:     /// Encoding used for the controller (defaults to UTF8)
   9:     /// </summary>
  10:     public virtual Encoding Encoding { get; set; }
  11:  
  12:     /// <summary>
  13:     /// Initializes the controller
  14:     /// </summary>
  15:     /// <param name="requestContext">Request context</param>
  16:     protected override void Initialize(System.Web.Routing.RequestContext requestContext)
  17:     {
  18:         requestContext.HttpContext.HTTPCompress();
  19:         requestContext.HttpContext.Response.ContentEncoding = Encoding.NullCheck(new UTF8Encoding());
  20:         base.Initialize(requestContext);
  21:     }
  22:  
  23:     /// <summary>
  24:     /// Returns Json encoded object
  25:     /// </summary>
  26:     /// <param name="Object">Object to encode</param>
  27:     /// <returns>The object as a Json.Net result</returns>
  28:     protected virtual JsonNetResult JsonNet(object Object)
  29:     {
  30:         JsonNetResult Result = null;
  31:         JsonSerializerSettings Settings = new JsonSerializerSettings();
  32:         IsoDateTimeConverter Converter = new IsoDateTimeConverter();
  33:         Settings.Converters.Add(Converter);
  34:         Result = new JsonNetResult(Object, Settings, Formatting.None);
  35:         return Result;
  36:     }
  37:  
  38:     /// <summary>
  39:     /// On Action Executed
  40:     /// </summary>
  41:     /// <param name="filterContext">filter context</param>
  42:     protected override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
  43:     {
  44:         base.OnActionExecuted(filterContext);
  45:     }
  46: }

I'm still adding to it, but at present it compresses the data sent back (using GZip by default), sets the encoding, sets the content type to ???application/json' and adds a function JsonNet that serializes an object to JSON (using Json.Net) and returns it as a result. The other controller base class deals primarily with controllers that deal primarily with HTML:

   1: /// <summary>
   2: /// Base class for controllers
   3: /// </summary>
   4: [CSRF]
   5: public class ControllerBase : System.Web.Mvc.Controller
   6: {
   7:     /// <summary>
   8:     /// Encoding used for the controller (defaults to UTF8)
   9:     /// </summary>
  10:     public virtual Encoding Encoding { get; set; }
  11:  
  12:     /// <summary>
  13:     /// Initializes the controller
  14:     /// </summary>
  15:     /// <param name="requestContext">Request context</param>
  16:     protected override void Initialize(System.Web.Routing.RequestContext requestContext)
  17:     {
  18:         requestContext.HttpContext.HTTPCompress(true);
  19:         requestContext.HttpContext.Response.ContentEncoding = Encoding.NullCheck(new UTF8Encoding());
  20:         base.Initialize(requestContext);
  21:     }
  22:  
  23:     /// <summary>
  24:     /// On Action Executed
  25:     /// </summary>
  26:     /// <param name="filterContext">filter context</param>
  27:     protected override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)
  28:     {
  29:         base.OnActionExecuted(filterContext);
  30:     }
  31: }
This one, using a couple of functions from my utility library, compresses the data sent back and removes white space (basically removes "pretty printing"), sets the encoding, and adds a cross site request forgery token on any form (that's the CSRF attribute on the class). Past that, there are classes that help add themes to MVC, automatically compress CSS and Javascript files, classes that will help with plugins, etc. In fact, my ultimate goal with this framework is to make it so that once I drop it in place, all I have to do is define my models and it will set up my REST API and services for me so all I need to do is the HTML, CSS, Javascript, etc. basically the front end stuff (minus the one off items). Anyway, I'm hoping to have this done soon so I can put it up on CodePlex/Github with an initial release and then start on the main project.  So I'll keep people posted and happy coding.


Comments