Anil C
Anil C

Reputation: 1065

Modify Controllers (Dependency Injection) in Smart-Store.Net

I want to modify some Controllers for which i need to add few more parameters to the Constructor of the Controller. If I add more parameters, it won't work. Can you please suggest where do I need to map the parameters (for ioc) so that it works fine?

Upvotes: 0

Views: 403

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233150

The best approach is to implement the IControllerFactory interface, particularly the GetControllerInstance method. Often, the easiest way to do that is to derive from DefaultControllerfactory.

protected override IController GetControllerInstance(
    RequestContext requestContext, Type controllerType)
{
    if (controllerType == typeof(FooController))
    {
        return new FooController(new BarService(), new BazProvider());
    }
    if (controllerType == typeof(QuxController))
    {
        return new QuxController(new QuuxRepository, new CorgeProvider());
    }
    // etc.
    return base.GetControllerInstance(requestContext, controllerType);
}

Then you need to tell ASP.NET MVC about your new custom factory:

ControllerBuilder.Current.SetControllerFactory(new MyCustomControllerFactory());

You can read more details about this in chapter 7 in my book.

Upvotes: 1

Related Questions