Friday, August 24, 2012

Draupner: Full Stack ASP.NET MVC Scaffolding

A little while back I announced on twitter that Mjølner had open sourced it's ASP.NET MVC scaffolding tool, Draupner:

Since then Draupner has been updated to produce ASP.NET MVC 4 apps, and all the dependencies have been updated too. So now since like a good time an introductory post about Draupner.

Why Did we Build Draupner?
Scaffolding is not a new concept. It's been used for while in other web frameworks, and moreover Steven Sandersons ASP.NET MVC scaffolding has been around for a while too. That begs the question why another scaffolder? Basically because the existing ASP.NET MVC one does not do what we (Mjølner) want the way we want it: We want a simple command line tool that can

  • Set up a new ASP.NET MVC solution with a "Web" project along with an associated "Core" project and a "Test" project.
  • Add further entities to the solution as needed throughout the solutions lifetime
  • Add CRUD operations and GUIs to existing entities
  • Add tests for all the other stuff it adds
  • Allow us to code away happily on the solution without having to think about making the scaffolding tool happy

Furthermore, and importantly, we wanted everything the tool produced to follow an architecture we've used successfully time and again. This means setting up a certain structure in terms of projects and folders, as well as building on a certain technology stack that we like to work with. In other words the tool, Draupner, is a set of practices we at Mjølner have had succes with put into code. These last bits are what sets Draupner apart.

What Technology Stack does Draupner Set Up?
Draupner projects uses a bunch of technologies, that we've found to work well together. All of them are set up as NuGet dependencies (except Rake). The stack includes:

You can see the full list on the Draupner page on Github.

What Does a Draupner Solution Look Like?
Let's have a quick glance at a solution built with Draupner. This screenshot shows which projects such a solution consists of:

The .Web project is the ASP.NET MVC site: It includes views and some thin controllers, that rather quickly call into the .Core project.

The .Core project is where the domain model goes and where the persistence of said domain model is handled. We like intelligent domain models, so this is where the smarts of the application is meant to go.

The .Test project contains xUnit tests for both the .Web and the .Core projects.

This is all set up by Draupner during the initial project creation. Moving on from there Draupner can add further entities to the domain model and add CRUD operations/GUIs to those enitites. Taking a look inside the .Core project we see:

This gives a peek into the technolgoy stack used by Draupner projects: Entities are persisted to a SQL Server database via NHibernate (you can reconfigure NHibernate all you want if you e.g. want to change to MySql), and the NHibernate mappings are set up with Fluent.NHibernate. Draupner also create repositories for the enities it creates which the the controllers in the .Web project can use.

Also notice the Castle.Windsor dependency: All the code produced by Draupner uses dependency injection and inversion of control.Caslte.Windows is the IoC/DI container of choice used by Draupner proejcts. E.g. the repositories mentioned above expect an IUnitOfWork into which it can enroll operations. This is injected into them by Windsor. Skipping ahead a bit let me mention that the .Web project sets up an NHibernate unit of work per web request and registers it with Windsor.

Lastly we can notice that Draupner sets up Log4Net, so that it's ready to go.

Let's move up the stack and open up the .Web project:

We can see that Draupner has created controllers for the enities it created. These each allow for simple CRUD operations.

Draupner has also created simple, but nice Ajaxy CRUD views for the entities. Neither of these are really expected to be used in production, but act as placeholders until "the real thing" is implemented.

Draupner has also created a few view models, which are used in the CRUD GUIs and has set up Automapper configurations to map between the view models and the enitites in .Core.

Worth mentioning is also that the .Web project uses Elmah for error logging in the web layer.

As mentioned Draupner creates tests for all this as well. They end up the .Test project:

The tests are xUnit tests and use AutoFixture and Rhino.Mocks.

So What Now?
If this caught you're interest go clone the Draunper sample project on Github and take a harder look at how things are set up or take Draupner for a spin, following the instructions in the readme. If you like it, but find something missing or not working let us know. We're not making any promises with regards to support and bug fixes though, so an even better idea is to send a pull request. Those we do welcome.


  1. Nice article.., I really want to learn Asp.NET. Could you please guide me if is good to start for the basics of .NET. Will appreciate your prompt reply!

  2. @Abhay: Try there is tons of content there. Including some good getting started guides.

  3. Very cool scaffolder, I made something similar (but only half as good) to speed up my university project.
    A couple of questions if you don't mind:
    1) How is concurrency control and authentication and authorization handled?
    2) I noticed there are no references to company libraries etc. Do you add those afterwards, or is every project self-standing?
    3)You don't use ajax to create a popup dialog for adding and editing records and instead send the user to the actual page, yet you use ajax for deletes?

    1. @Lee: Thanks!
      To answer your questions:

      1) I'm not sure what you mean by concurrency control in this context...but every http request gets it's own UnitOfWork, which in turn means it has it's own NHibernate session. Apart from state in the DB the generated application is practically stateless. Hope this is what you were asking about.
      1) There is a Draupner command for scaffolding out authentication/authorization. It generates a basic forms-based authentication implementation, including GUIs for CRUD'ing user, controllers, DB schema, NH mappings and so on. Once that is in place authorization can be done using the usual ASP.NET MVC authorization attributes.

      2) Yes any other dependencies are added after project creation. Notice that adding such dependencies does not mess up Draupner. You can still happily go on scaffolding new enities and CRUD functions for them. Doing so will not mess with your extra dependencies.

      3)True :-) No special reason for that. Either way the CRUD views are only meant to be temporary. Something to set up early in development but to replace later with an implementation that suits end user needs and mental models.

    2. This comment has been removed by the author.

    3. Thanks for the answers. I meant concurrency where you open a record, I open the same one, we both edit and save, and then what happens -seems like last in wins:) Next month after my project is due I am going to fork/clone Draupner(whatever it is called in GitHub) because I want to use EF and Ninject instead, should be an easy task to do.

  4. This comment has been removed by the author.

  5. Hey! Have you ever noticed, have your writting skills upgraded so far?