Copied to clipboard

Flag this post as spam?

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


  • Matthew 14 posts 84 karma points
    Oct 02, 2019 @ 02:32
    Matthew
    0

    Named and Keyed services (Autofac IIndex) equivalent in LightInject ....

    Following from another question I had, I can see Umbraco8 provides the lightinject DI setup. I could possibly switch to use this for basic DI.

    What I was particularly struggling with is that I am factory class (pattern) which worked great with AutoFac and the named and keyed services. Autofac adds this to a IIndex.

    In the DI I had this ...

                builder
                .RegisterAssemblyTypes(assembly)
                .Where(type => type.GetInterfaces().Contains(typeof(ITransform)))
                .Keyed<ITransform>(transform => transform.Name);
    

    then in the factory class constructor I had ...

    public TransformFactory(IMyAwesomeContext awesomeContext, IIndex

    I had a number of classes implementing ITransform. Within the factory I could then simply do this ... to get at the required object.

                switch (nodeTypeAlias.ToLowerInvariant())
                {
                    case "user":
                        _transforms.TryGetValue("UserTransform", out transformObject);
                        break;
                }
    

    so my question is, how do I do that with lightinject within Umbraco8 ? ...

    I am hoping I can stick with Autofac to be consistent with my Umbraco7 project. Will be a little confused if I need to change the DI to sit with lighinject (seeing Umbraco let us use our own DI packages).

    Thanks ...

    Matt

Please Sign in or register to post replies

Write your reply to:

Draft