views:

553

answers:

2

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?

+1  A: 

You have to override the TurbineApplication.AutoComponentSetup-method, something like this:

protected override void AutoComponentSetup(IServiceLocator locator)
{
    //Remove this if you want to skip all other previous registrations
    base.AutoComponentSetup(locator);

    AutoRegistration<IMyCustomInterface>((loc, type) => loc.Register<IMyCustomInterface>(type));
}

See also Auto Registration for Components

Thomas Eyde
Ah yes, that probably fills in the gap :) Thanks
borisCallens
after testing it out, it didn't work after all. Please ref update in OP.
borisCallens
+1  A: 

Hi Boris,

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!

Javier Lozano
Thanks. I haven't had time for this pet project lately, but next week I will have some time to spare I think. I will definitely check it out then. Thanks :)
borisCallens
No problem, Boris. I completely understand. One question, are you using Turbine with MVC2 with .NET4 or .NET3.5? Reason I ask is because I'm in the process of getting Turbine compiled against the MVC2 RC bits that run on top of .NET3.5, so you could benefit from that update as well.
Javier Lozano
Hi Javier, thanks for the support so far in my various questions :)It's still plain old 3.5 ;)
borisCallens
Ah, only now do I see the meaning of your post. As the project is in 3.5 (C#3) I'll have to wait around for the bits to hit the web.If I'm not mistaking there is no SVN branch I can use to compile it myself.
borisCallens