Ninject + ActiveRecord + ASP.NET MVC + Scaffolding

OK.  So using a combination of Ninject for my IOC, ActiveRecord(the Mediator flavor) for my data access objects slash models, and ASP.NET MVC, I have my first cut of scaffolding done.  The source is here, http://svn.chrisjcarter.com/public/ActiveRecordScaffolding/, you can point your favorite SVN client at the url and check it out.

There's one controller, ARController that handles all of the operations of List, Save, and Edit.  It's a generic type that takes the ActiveRecord class as a type parameter.  There is one ARController per ActiveRecord class in any assembly you tell the start up code to look at.  There are 2 shared views, one for listing and one for editing. Only the bare minimum was used for the edit form, pretty just enough to get a form on the screen with zero logic and no concern for the type of field being edited.  Both views use a single mvc style master page.

The ARControllerModule is a Ninject StandardModule that's used to wire up each ActiveRecord classs in the project to an ARController of the same type.  Here's the CreateARControllers method responsible for finding all of ActiveRecord classes in an assembly and creating a new ARController of the same type as the ActiveRecord class:

public IDictionary CreateARControllers(Assembly assembly)
{
  Dictionary controllers = new Dictionary();

  Type[] types = assembly.GetExportedTypes();
  foreach (Type type in types)
  {
    object[] attribs = type.GetCustomAttributes(typeof(ActiveRecordAttribute), false);
    if (attribs != null && attribs.Length == 1)
    {
      Type controller = typeof(ARController).MakeGenericType(type);
      controllers.Add(type.Name, controller);
    }
  }
  return controllers;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

I included the Sql Server CE flavor of the Northwind database.  It works.  But a few screens break for reasons I didn't tackle yet, that'll be another post.  The cool thing is that all you do is load up whatever assemblies contain your active record classes and it'll build a web ui to edit them.  It's a first pass, but it should work with minimal effort on your machine, lemme know otherwise.  You can grab a zip of the code here(it's about 10 megs).

 
Author: , 0000-00-00