With my pet project I'm trying to learn to use Turbine as a DI container.
I'm registering unity as locatorprovider as such:
static MvcApplication()
{
ServiceLocatorManager.SetLocatorProvider(() => new UnityServiceLocator());
}
My user repository has a parameterless constructor and I'm registering it as such:
public class UserRepositoryRegistration : IServiceRegistration
{
public void Register(IServiceLocator locator)
{
locator.Register<IUserRepository, UserRepository>();
}
}
Then I have my HomeController which should accept an IUserRepository
public class HomeController : Controller
{
private readonly IUserRepository userRepository;
public HomeController(IUserRepository repository)
{
userRepository = repository;
}
}
If I leave out the parameterless ctor (like in the above code snippet) I get this (full here):
Server Error in '/' Application. No parameterless constructor defined for this object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.MissingMethodException: No parameterless constructor defined for this object. [InvalidOperationException: An error occurred when trying to create a controller of type 'Boris.BeekProject.Guis.Web.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
So my HomeController is obligated to have a parless ctor. It seems it's not the Unity controller factory that's instantiating, but rather the default one.
UPDATE
My MVCApplication is inheriting TurbineApplication and since the RouteRegistration is picked up just fine I think the problem lies somewhere else.
UPDATE
As suggested by Thomas Eyde I wanted to override the TurbineApplication.AutoComponent
method, but checking the Object Browser, I can't see any reference to this method.
Furthermore when I look at the NerdDinner example, it doesn't seem to override this method either.
After checking the online documentation about it I failed to get any the wiser and following the link to documentation about doing the registration manually serves me a placeholder page.
Can anybody fill me on on what I'm doing wrong?
Am I missing something?
Your build of Turbine doesn't support MVC2, in which the ControllerFactory is changed to support an extra parameter for the creation of controllers. This is explains why your controller never gets instantiated correctly. The breaking changes of MVC2 list this change.
If you're wanting to use MVC2 Beta (for .NET4) for your development, I suggest you get the new MVC2 (.NET4) bits I just released.
Hope that helps you out!