Monday, July 8, 2013

Ninject with MVC4: No parameterless constructor defined for this object

I've been a big fan of StructureMap, but a few days ago I decided to investigate a simpler framework for DI. I heard about Ninject a long time ago and decided it was time to learn it by applying it to the controllers in MVC4. There are a lot of usefule posts out there that explain how to use it, just keep in mind 2 things:

1) Put your code in App_Start/NinjectWebCommon.cs instead of the global.asax
2) After finishing the tutorials, I got the error "No parameterless constructor defined for this object". This post from StackOverflow has the correct answer! Basically you need these 2 code snippets:

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;
    public NinjectControllerFactory(IKernel kernel)
    {
        ninjectKernel = kernel;
    }
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        return (controllerType == null) ? null : (IController) ninjectKernel.Get(controllerType);
    }
}
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(Kernel));

Friday, July 5, 2013

Setting the password for a Git repository in Continuous Integration systems

Continuous Integration systems like CC.NET or Hudson don't let you specify the username/password with which you'll connect to Git. But there's an easy way, just set the repository url to:

https://username:password@github.com/Test/MyProject.git