MVC Dependency Injection - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

MVC Dependency Injection

  • MVC Dependency Injection
  • Can anyone provide a working example of wiring up MVC controllers with dependency injection? Only the default constructor is called on the controller and my dependencies are never injected. Something current preferably. 

    Will the documentation ever provide samples for these common scenarios?

    Thanks.
  • I'll add that it seems Sitefinity refuses to use my custom controller factory. How do I used dependency injection with controllers in Sitefinity? 
  • Nothing? 
  • Should I use support to answer this question?
  • I would also like a working example.

    The only way I've been able to get it to work is to have a parameterless constructor and to manually use my own dependency resolver to create my dependency.

    For example,

    public MyController()
       myDependency = new MyDependencyResolver().GetService<MyDependency>();


    Another approach I tried but didn't work was to add the following to Application_Start:

    DependencyResolver.SetResolver( new MyDependencyResolver());

    and call this from my constructor:

    DependencyResolver.Current.GetService<MyService>();

    But DependencyResolver.Current returns DefaultDependencyResolver.
  • Hi,

    Thank you for contacting us.

    Unfortunately at this very moment Sitefinity doesn't provide such functionality out of the box. We will consider implementing it in the future. 

    However there is one possible workaround which requires a bit more code.

    For its controllers creation Sitefinity uses custom controller factory class called SitefinityControllerFactory. Its a public class which inherits the DefaultControllerFactory that comes with the System.Web.Mvc.

    You can create your own Controller factory which inherits the SitefinityControllerFactory and override its CreateController method where you have complete control over the creation of controllers so you can inject something or call the base CreateController method of the SitefinityControllerFactory class.

    Your ControllerFactory class then need to be registered as a current controller factory. In order to do that you can register a event handler for the Telerik.Sitefinity.Abstractions.Bootstrapper.Initialized event in your Global.asax file and then in the handler you can call this method:

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

    Please be aware that this is just a workaround not a best practice or something that we encourage because overriding core Sitefinity functionality can cause an unexpected behavior.  

    Regards,

    Desislav Bonchev
    Telerik
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  • This is what I did:

     Created custom Controller Factory

    public class CustomControllerFactory : SitefinityControllerFactory

    public override IController CreateController(RequestContext requestContext, string controllerName)

    IController c = null;
    try

    c = base.CreateController(requestContext, controllerName);

    catch (MissingMethodException ex)

    Type controllerType = base.ResolveControllerType(controllerName);
    Controller controller = (Controller)DependencyResolver.Current.GetService(controllerType);
    controller.ControllerContext = new ControllerContext(requestContext, (ControllerBase) controller);
    c = (IController)controller;

    return c;

     

    Then in Global.asax I did this (though I might try what Desislav recommends)

    ObjectFactory.Initialized += ObjectFactory_Initialized; (in Application_Start)

    void ObjectFactory_Initialized(object sender, Telerik.Sitefinity.Data.ExecutedEventArgs e)

    if (e.CommandName == "RegisterCommonTypes")

    Telerik.Microsoft.Practices.Unity.UnityContainerExtensions.RegisterType<ISitefinityControllerFactory, CustomControllerFactory>(ObjectFactory.Container, new InjectionMember[0]);

     

    And followed this for the normal MVC dependency injection:

    www.asp.net/.../aspnet-mvc-4-dependency-injection

     

    I just started the project, so I'm not sure what issues I might come across

  • Hi,

    Any intention to add this feature to Sitefinity in the short term future? I am surprised how Sitefinity doesn't support dependency injection in the controllers like it does asp.net mvc. This makes unit testing hard, not to say impossible..

  • Hello Asier,

    Can you please take a look at the following blog post for more details about unit tests using unity dependency injection in Sitefinity.

    Regards,
    Sabrie Nedzhip
    Telerik
     
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items