Copied to clipboard

Flag this post as spam?

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


  • Chris Thwaites 8 posts 93 karma points
    Aug 07, 2019 @ 13:27
    Chris Thwaites
    1

    Dependency Injection - registering multiple types in Umbraco 8

    I'm trying to get DI working in Umbraco 8.1.1.

    In Umbraco 7 I used Autofac, and was able to inject all types in my repository and manager assemblies with a couple of lines of code:

       var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();
       builder.RegisterAssemblyTypes(assemblies.ToArray()).Where(a => a.Name.EndsWith("Repository")).AsImplementedInterfaces().InstancePerLifetimeScope();
    

    I'm struggling to do the same with the new composition code. Currently I'm having to manually type out all the dependencies:

                composition.Register<IExampleManager, ExampeManager>();
                composition.Register<IAnotherManager, AnotherManager>();
                composition.Register<IOneTypeRepository, OneTypeRepository>();
                composition.Register<IAnotherTypeRepository, AnotherTypeRepository>();
                composition.Register<IThirdTypeRepository, ThirdTypeRepository>();
    

    This is problematic for us since it means the website code will have to change and be refreshed whenever anything changes in any of our supporting libraries, which happens a lot. Right now we are able to separate our website development nicely from our back-end management and data access libraries.

    Also, there are a few hundred of these types to go through, so one line instead of a few hundred lines of code is always nicer!

    I'm trying to use reflection, but I can't work out how to do that and automate the registration. I was thinking something like this (obviously not going to work, but hopefully you'll see what I'm driving at):

                var assemblies = BuildManager.GetReferencedAssemblies().Cast<Assembly>();
                foreach(Assembly assembly in assemblies)
                {
                    //get all the types
                    foreach (Type ttt in assembly.GetTypes())
                    {
                        if (ttt.Name.EndsWith("Repository"))
                        {
                            var inf = ttt.GetInterface("I" + ttt.Name);
                            composition.Register<inf,ttt>();
                        }
                    }
                }
    

    Am I barking up completely the wrong tree?

    Thanks,

    Chris

  • Rasmus Eeg 91 posts 454 karma points c-trib
    Aug 27, 2019 @ 17:47
    Rasmus Eeg
    1

    Hi Chris, I'm on the same quest. Did you find a solution for your question?

  • Chris Thwaites 8 posts 93 karma points
    Aug 28, 2019 @ 17:37
    Chris Thwaites
    1

    Sorry, no. I never did. I ended up writing it out long-hand. If/when you do hit on an automated way to do it, I'd love to hear!

    Cheers, Chris

  • Ruben Meintema 9 posts 100 karma points
    Apr 22, 2020 @ 11:34
    Ruben Meintema
    0

    Hi Chris,

    I think what you need here is "type scanning". Here is a great explanation from the Umbraco documentation.

    You could try to let your interfaces implement "IDiscoverable", and then register them using the code that is also explained in the said documentation:

    public class MyThingComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            // Add types from assemblies - be conscious of doing type scanning
            // as this adds time to boot up of Umbraco
            // If you still need to use type scanning, ensure your Interface implements `IDiscoverable`
            composition.MyThings().Add(() => composition.TypeLoader.GetTypes<IMyThing>());
        }
    }
    

    Hope this helps. Cheers,

    Ruben

  • Chris Thwaites 8 posts 93 karma points
    May 04, 2020 @ 09:33
    Chris Thwaites
    0

    Hi Ruben,

    Thanks for the update. Seems like it might be what I was after. I'll definitely take a proper look once our lockdown is over and I'm back working with the client.

    Cheers, Chris

Please Sign in or register to post replies

Write your reply to:

Draft