+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / MVC Dependency Injection

MVC Dependency Injection

9 posts, 0 answered
  1. Christian
    Christian avatar
    10 posts
    Registered:
    22 Aug 2013
    26 Sep 2013
    Link to this post
    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.
  2. Christian
    Christian avatar
    10 posts
    Registered:
    22 Aug 2013
    26 Sep 2013
    Link to this post
    I'll add that it seems Sitefinity refuses to use my custom controller factory. How do I used dependency injection with controllers in Sitefinity? 
  3. Christian
    Christian avatar
    10 posts
    Registered:
    22 Aug 2013
    30 Sep 2013
    Link to this post
    Nothing? 
  4. Christian
    Christian avatar
    10 posts
    Registered:
    22 Aug 2013
    03 Oct 2013
    Link to this post
    Should I use support to answer this question?
  5. Bo Chulindra
    Bo Chulindra avatar
    35 posts
    Registered:
    17 Dec 2013
    02 Jan 2014
    Link to this post
    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.
  6. Desislav Bonchev
    Desislav Bonchev avatar
    25 posts
    Registered:
    19 Jul 2012
    06 Jan 2014
    Link to this post
    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
  7. Jason
    Jason avatar
    12 posts
    Registered:
    18 Sep 2006
    04 Jun 2014 in reply to Desislav Bonchev
    Link to this post

    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:

    http://www.asp.net/mvc/tutorials/hands-on-labs/aspnet-mvc-4-dependency-injection

     

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

  8. Asier
    Asier avatar
    1 posts
    Registered:
    18 Nov 2014
    27 Nov 2014 in reply to Desislav Bonchev
    Link to this post

    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..

  9. Sabrie Nedzhip
    Sabrie Nedzhip avatar
    534 posts
    Registered:
    02 Dec 2016
    02 Dec 2014
    Link to this post
    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
     
9 posts, 0 answered