Mehfuz's WebLog

Live crazy, think different!

Sponsors

News

Passionate about cutting edge technologies and facinated by the modern web and phone revolution.Currently working at Telerik Corporation, the leading .net component vendor.
Follow me


Articles


Projects

Writing ASP.NET MVC bootstrapper with AutoBox

This will post will show how you can use AutoBox to easily write a bootstrapper for ASP.NET MVC. I have used the latest version of AutoBox (available from nuget, this version also includes Castle.Windsor internally for managing dependencies rather using its own resolver and does not require interface to type naming convention [IAccountRepository –> AccountRepository]) . To understand what is AutoBox , how you can use it for caching using memcached and let it automatically handle dependencies for controllers and repositories, i would recommend to take a look at this post:

http://weblogs.asp.net/mehfuzh/archive/2011/11/06/introducing-autobox-on-the-fly-dependency-injection-and-caching-container.aspx

Moving forward , let’s consider a simple bootstrapper interface:

  1. public interface IBootStrapperTask
  2. {
  3.     void Execute();
  4. }

The Execute()  method will be invoked during initialization for registering routes, controllers, mappings (AutoMapper), etc. We will have one static factory (Ex. BootStrapper)  that will initiate it through CommonServiceLocator.

  1. public static class BootStrapper
  2. {
  3.     /// <summary>
  4.     /// Executes registered tasks.
  5.     /// </summary>
  6.     public static void Execute()
  7.     {
  8.         ServiceLocator.Current.GetAllInstances<IBootStrapperTask>().ToList().ForEach(task =>task.Execute());
  9.     }
  10. }

A typical example of a bootstrapping task could be RegisterRoutes:

  1. public class RegisterRoutes : IBootStrapperTask
  2. {
  3.     private readonly RouteCollection routes;
  4.  
  5.     /// <summary>
  6.     /// Initializes the new instance of <see cref="RegisterRoutes"/> class.
  7.     /// </summary>
  8.     public RegisterRoutes()
  9.         : this(RouteTable.Routes)
  10.     {
  11.         // intentionally left blank.
  12.     }
  13.  
  14.     /// <summary>
  15.     /// Initializes the new instance of <see cref="RegisterRoutes"/> class.
  16.     /// </summary>
  17.     internal RegisterRoutes(RouteCollection routes)
  18.     {
  19.         this.routes = routes;
  20.     }
  21.  
  22.     public void Execute()
  23.     {
  24.         routes.Clear();
  25.  
  26.         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  27.         routes.IgnoreRoute("{*favicon}", new {favicon = @"(.*/)?favicon.ico(/.*)?"});
  28.  
  29.         Routes.Register(routes);
  30.     }

Here on line 29, i have added Routes.Register which is similar to the BootStrapper.Execute() that further narrows down to specific routing classes. For example. AccountRoutes.

  1. public static void Register(RouteCollection routes)
  2. {
  3.     ServiceLocator.Current.GetAllInstances<IRoutesRegistration>().ToList()
  4.        .ForEach(task => task.Register(routes));
  5. }

The code inside Routes class again pretty straight forward and exactly identical to BootStrapper except the IRoutesRegistration interface. We can also have RegisterControllers but since the dependencies are automatically wired by AutoBox, we only need to specify (if required) what repository methods to be data cached and for what duration.

  1. public class RegisterControllers : IBootStrapperTask
  2. {
  3.     void IBootStrapperTask.Execute()
  4.     {
  5.         Container.Setup<ProductRepository>(x => x.GetProductDetails(0)).Caches(TimeSpan.FromMinutes(1)).VaryByArgs();
  6.         ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
  7.     }
  8. }

More on caching and how MyControllerFactory should look like; please check my previous post as well as the product documentation. Finally, it’s about gluing the whole thing together and we just need to have these lines in global.asax.cs

  1. protected void Application_Start()
  2. {
  3.     // Initializes autobox self.
  4.     Container.Init();
  5.     BootStrapper.Execute();
  6. }

When you will run the project; if you followed the flow then it should work as you have expected it. The project page for AutoBox can be reached here: http://mehfuzh.github.com/AutoBox/ .

 

Hope that helps

Posted: Jan 30 2012, 09:47 PM by mehfuzh | with 3 comment(s) |
Filed under: , , , , ,

Comments

Nova said:

I think that AutoBox could make the code neat, but how do you make sure the sequece of execution?

# January 30, 2012 9:00 PM

mehfuzh said:

Hi Nova,

I think sequence for registration will only be useful in Routes. In that case, i will try to add an option where you can specify which route registration class should go first. However, it will matter a little if you define your route registration classes by section, like buy => BuyRoutes , signup => SignupRoutes, etc.

Hope that helps

# January 31, 2012 4:09 AM