I want o bind my controller with a parameter that is lazy evaluated.
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
try
{
return controllerType == null
? null
: (IController) _ninjectKernel.Get(controllerType);
}
catch (Exception ex)
{
throw;
}
}
And I have the next binding:
_ninjectKernel.Bind<IFilesRepository>().To<FilesManager>().WithConstructorArgument("storageFolderAbsolutePath", c => c.ToString());
The problem is at the lambda function. I want to return Server.MapPath(“/”) … but I don’t have the request context in the c object. How can I sent it?
I’m not overly familiar with Ninject, but you should be able to register a provider with the container to be able to resolve a
HttpContextBase. By doing so, theIFilesRepositorycan now take aHttpContextBaseas a constructor argument, which will be injected by the container, using the provider, when creating an instance ofIFilesRepository.To register a provider (using a delegate to resolve the service),
Be aware however, that the life style of an
IFilesRepositorywould most likely need to change to a “per Web Request” lifestyle, as theHttpContext.Currentis created per web request, so you wouldn’t want to go holding onto that in anIFilesRepositorythat had a longer life style. You may want to abstract out the “mapping paths” functionality so that you can have anIFilesRepositorywith a longer lifestyle.