Copied to clipboard

Flag this post as spam?

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


  • Malthe Petersen 68 posts 383 karma points c-trib
    Mar 11, 2019 @ 09:38
    Malthe Petersen
    0

    Umbraco 8 Dependency Injection override/proxy

    Hi.

    I need to connect your DI container with my own SimpleInjector DI container. Right now I have tried to create a concrete class of the IFactory interface and replace your Current.Factory. That is not possible because it can only be set once - and setting it before you, makes your code throw an exception and vice versa.

    I have also tried to create my own WebRunTime and inherit from yours and then pass it my own IFactory - doesn't work either.

    The only working thing is to override ControllerBuilder.Current.SetControllerFactory with my own ControllerFactory and then register all your services in my container. I am not a huge fan of this method, because we are not sure what of your controller specific things we override.

    Do you have any suggestion on how to do this? It is not possible to switch to your LightInjector.

    Many regards Malthe

  • Jesper Rasmussen 5 posts 75 karma points
    Apr 09, 2019 @ 11:36
    Jesper Rasmussen
    0

    Hi Malthe

    Did you find a solution?

  • Malthe Petersen 68 posts 383 karma points c-trib
    Apr 09, 2019 @ 13:38
    Malthe Petersen
    1

    Hi Jesper.

    Yeah we found a solution.

    We are writing our own implementation of IRegister and IFactory and decorate our factory with the default Umbraco 8 factory. And then we are setting the container in the factory.

  • skp 12 posts 80 karma points
    Jul 23, 2019 @ 18:14
    skp
    0

    Hi Malthe Petersen,

    I am using Umbraco 8. I need to implement DI, can you please share me the code.

  • Malthe Petersen 68 posts 383 karma points c-trib
    Aug 24, 2019 @ 06:51
    Malthe Petersen
    0

    Hi Skp.

    Sorry for late answer.

    For the Factory we did something like:

    public class SimpleInjectorUmbracoFactory : IFactory
        {
            private Container _container;
            private readonly IFactory _umbracoDefaultFactory;
            private IEnumerable<Assembly> _mvcControllerAssemblies;
            private IEnumerable<Assembly> _apiControllerAssemblies;
    
            private SimpleInjectorUmbracoFactory(IFactory umbracoDefaultFactory)
            {
                _umbracoDefaultFactory = umbracoDefaultFactory;
            }
    
            public static SimpleInjectorUmbracoFactory Current { get; private set; }
            public static SimpleInjectorUmbracoFactory Create(IFactory umbracoDefaultFactory)
            {
                return Current ?? (Current = new SimpleInjectorUmbracoFactory(umbracoDefaultFactory));
            }
    ...
    ...
    }
    

    And the Registry looks something like this:

    public class SimpleInjectorUmbracoRegistry : IRegister
        {
            private readonly LightInjectContainer _lightInjectContainer;
    
            public SimpleInjectorUmbracoRegistry()
            {
                _lightInjectContainer = LightInjectContainer.Create();
            }
    
            public static IRegister Create()
                => new SimpleInjectorUmbracoRegistry();
    ...
    ...
    }
    

    It is a bit complext sadly and I can't show you everything due to copyright, but it is possible to use two different DI frameworks at once. :-) And the IRegister and IFactory is the way in.

    Regards Malthe

Please Sign in or register to post replies

Write your reply to:

Draft