Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Maciek 6 posts 26 karma points
    May 10, 2012 @ 21:16
    Maciek
    0

    Umbraco 5.1 : Replacing Autofac with Ninject?

    Hi there,

    I'm new to Umbraco and have decided to learn it by "doing". 

    Ninject is my tool of choice, so my first move was to set it up as the main container solution for my project. I could use 2 containers at the same time (Autofac+Ninject) however this just doesn't seem right to me.

    I'd very much apreciate assistance with replacing Autofac with Ninject, sadly I know very little of Autofac so some of the intricacies - elude me.

     

    Cheers.

  • Jon 36 posts 59 karma points
    May 10, 2012 @ 21:56
    Jon
    0

    I have done a similar thing with Castle.

    You can create a new class that inherits from MvcApplication and then change your global.asax file to point to this new class. So your new class will look something like this:

    public class YourUmbracoWebApplication : MvcApplication {
            protected override void Application_Start() 
            {
                // Do your IoC initialisation here...
                base.Application_Start();
            }
        }

    And your Global.asax will look like this:

     <%@ Application Codebehind="Global.asax.cs" Inherits="YourUmbracoWebApplication" Language="C#" %>

    Hope this helps :)

  • Maciek 6 posts 26 karma points
    May 11, 2012 @ 00:13
    Maciek
    0

    Right, that's simple enough. However I still need to initialize the container properly, so that Umbraco can use Ninjecvt instead of Autofac - how do I go about that?

  • Jon 36 posts 59 karma points
    May 11, 2012 @ 10:32
    Jon
    0

    I have never used Ninject so I cannot really provide code samples for it.

    Using Castle you'd initialize your container and register your types (I am assuming Ninject is not too dissimilar). So the Application_Start (using a very crude example) would look like this:

    protected void Application_Start
    {
      IWindsorContainer = new WindsorContainer();
      container.Register(
        Component.For<IYourService>().ImplementedBy<YourConcreteService>().LifeStyle.Singleton
      );
    }
  • Maciek 6 posts 26 karma points
    May 11, 2012 @ 11:34
    Maciek
    0

    That's absolutely correct. 

     

    Is there a list of types that needs to be registered for Umbraco to run?

    Is there some kind of a adapter to the container ? For instance Umbraco might wish to resolve a named instance of a given type etc.

  • Jon 36 posts 59 karma points
    May 11, 2012 @ 11:44
    Jon
    0

    I have just re-read your first post and you are actually looking to completely replace AutoFaq with Ninject not just control your own custom objects with a different IoC container, sorry I should have read it more carefully.

    Having a look at the Umbraco 5 source it appears that the DI has been abstracted and there is a IContainerBuilder interface in Umbraco.Framework.DependencyManagement that is implemented in a seperate AutoFac library (Umbraco.Framework.DependencyManagement.Autofac).

    I'd probably start with the source from this library and adapt it to use Ninject (sorry that's a really obvious statement!).

  • Maciek 6 posts 26 karma points
    May 11, 2012 @ 11:52
    Maciek
    0

    I'll have a look at it later tonight, and get back to the topic ;) Cheers for the hints.

  • Jon 36 posts 59 karma points
    May 11, 2012 @ 11:55
    Jon
    0

    I am curious as to how easily you'll be able to do this :)

Please Sign in or register to post replies

Write your reply to:

Draft