A little nuget for Asp.Net MVC and Windsor

This is my very first code-only package for nuget.

This nuget serves as a mere example on how to integrate Castle Windsor to an ASP.Net MVC application.

First create an Asp.Net MVC project:

2011-03-05_2206

The Empty is ok:

2011-03-05_2208

Then add a View and a Controller as shown:

2011-03-05_2212

Use the “Add Library Package Reference”:

2011-03-05_2239

Search for my nuget (the name for now is Jose.MVC.Windsor);

2011-03-05_2215

Install it! and you are done. Castle Windsor is integrated to your very first Asp.Net MVC application.

Lets take a deep look to see what is installed.

My nuget has two dependences on other two packages; Castle.Windsor (which also depends on Castle.Core) and WebActivator. So as expected you can see here the three references:

2011-03-05_2219 

but as I said, my package has only code:

2011-03-05_2221

The bootstraper uses WebActivator to hook in to the application events initialize the container, and run all the installers. Also is responsible to set the ControllerFactory:

[assembly: WebActivator.PreApplicationStartMethod (
    typeof(Bootstrapper), "Wire")]

[assembly: WebActivator.ApplicationShutdownMethod (
    typeof(Bootstrapper), "DeWire")]

namespace SuperShop.App_Start
{
    public static class Bootstrapper
    {
        private static readonly IWindsorContainer container = new WindsorContainer();

        public static void Wire()
        {
            //To be able to inject IEnumerable<T> ICollection<T> IList<T> T[] use this:
            //container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, true));
            //Documentation http://docs.castleproject.org/Windsor.Resolvers.ashx
            
            //To support typed factories add this:
            //container.AddFacility<TypedFactoryFacility>();
            //Documentation http://docs.castleproject.org/Windsor.Typed-Factory-Facility.ashx
            
            container.Install(FromAssembly.This());
            var controllerFactory = new WindsorControllerFactory(container.Kernel);
            ControllerBuilder.Current.SetControllerFactory(controllerFactory);
        }

        public static void DeWire()
        {
            container.Dispose();
        }
    }
}

The controller factory is (I took this code from Castle wiki):

public class WindsorControllerFactory : DefaultControllerFactory
{
    private readonly IKernel kernel;

    public WindsorControllerFactory(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public override void ReleaseController(IController controller)
    {
        kernel.ReleaseComponent(controller);
    }

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            throw new HttpException(404, 
                string.Format("The controller for path '{0}' could not be found.", 
                requestContext.HttpContext.Request.Path));
        }
        return (IController)kernel.Resolve(controllerType);
    }
}

And the controller installer is:

public class ControllersInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(AllTypes.FromThisAssembly()
                            .BasedOn<IController>()
                            .If(Component.IsInSameNamespaceAs<HomeController>())
                            .If(t => t.Name.EndsWith("Controller"))
                            .Configure(c => c.LifeStyle.Transient));
    }
}

One last thing the package did is to install the handler for the “PerRequest” lifestyle on the web.config (as explained here):

2011-03-05_2233

 

Although the process of creating a nuget package is very well explained in the codeplex site, you mind find useful to look directly at my template, the code is here and it is free for everything.

I hope you find useful this little thing ! :)


blog comments powered by Disqus
  • Categories

  • Archives