Copied to clipboard

Flag this post as spam?

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


  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 01, 2019 @ 14:06
    Ali Sheikh Taheri
    0

    LightInject

    Hi

    I am upgrading/migrating a project from Umbraco 7.15.3 solution to 8.2 and the Umbraco 7 solution is using Ninject and I would like to use Umbraco 8 built-in LightInject DI.

    I have the below lines of code in Umbraco 7 and would like to rewrite it in LightInject, however, I have tried a few different ways but still no luck.

    Kernel.Bind(x => x
                .FromAssembliesMatching("MyProject.dll")
                .SelectAllClasses().InheritedFrom(typeof(IHandler<,>))
                .BindAllInterfaces()
                .Configure(y => y.InRequestScope()));
    

    I really appreciate if someone can help!

    Cheers

    Ali

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Nov 03, 2019 @ 22:49
    Marc Goodson
    0

    Hi Ali

    You can get underlying reference to LightInject in a composition via:

    var container = composition.Concrete as LightInject.ServiceContainer;
    

    and lightinject has Assembly Scanning capabilities:

    https://www.lightinject.net/#assembly-scanning

    So I'm thinking you could call something like:

      container.RegisterAssembly(typeof(IHandler).Assembly);
    

    or

    container.RegisterAssembly("MyProject.dll");
    

    if that is the kind of thing you are after?

    regards

    marc

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 04, 2019 @ 08:34
    Ali Sheikh Taheri
    0

    Hi Marc,

    Thanks for your post.

    However, what I am after is a little bit different, it looks like

    container.RegisterAssembly("MyProject.dll");
    

    does not register IHandler

    Still no luck with that.

    Cheers

    Ali

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Nov 04, 2019 @ 09:22
    Marc Goodson
    1

    Any luck with the predicate?

    To filter out the services to be registered with the container, we can provide a predicate that makes it possible to inspect the service type and the implementing type.

    container.RegisterAssembly(typeof(IFoo).Assembly, (serviceType, implementingType) => serviceType.NameSpace == "SomeNamespace");
    

    To be honest I am guessing, my main input is you can do things you can normally do in lightinject by the trick with casting the Umbraco concrete property to the lightinject container type... so if the method is missing in Umbraco's wrapper of the container but possible in lightinject, you can call it...and make it work.

    but maybe lightinject users would have more knowledge of whether this is possible in lightinject - it's certainly becoming a commonly asked question from those moving from their favourite DI framework in V7 to V8... so would be great to work out if it is possible in lightinject and put a snippet in the Umbraco docs!

    There is some info in the comments of this blog post too that might help: https://www.zpqrtbnk.net/posts/composing-umbraco-v8/

    particularly this comment

    composition.Register(typeof(IGenericRepository<>), typeof(GenericRepository<>), Lifetime.Singleton);
    

    for open generic Autofac components equivalence...

    regards

    Marc

  • Ali Sheikh Taheri 470 posts 1648 karma points c-trib
    Nov 04, 2019 @ 09:44
    Ali Sheikh Taheri
    0

    Hi Marc,

    I have tried both codes but still no luck, also read Stephans blog.

    Sorry, I should have elaborated the situation better in my original post.

    So basically here is the situation:

    I have these interfaces IHandler<> , IHandler<,> and IHandler<,,>

    There are many classes that implement these interfaces with different parameters. so they DI should register them all and match the type of parameters. so for instance:

    Example1: 
    MyHandlerImplementation1 : IHandler<MyClass1,MyClass2>
    
    
    
    Example2:
       MyHandlerImplementation2 : IHander<MyClass3,MyClass4>
    

    Because I have many of these classes that implements IHandler<,> and same for IHandler<> & IHandler<,,>. I don't want to bind each individual classes in Composer class. so it makes sense to register them all based on the convention and the type of class.

    With that line of code, I had in Ninject I could register and bind all classes that implemented IHandler<,>

    I am sure this is possible in LightInject but I am not sure how and what the syntax is :)

    As you mentioned in order to get the entire capability of LightInject I have defined the below line:

                var container = composition.Concrete as LightInject.ServiceContainer;
    

    so let's see if anyone else with more experience in LightInject can help with this post.

    Thanks again for your time and post.

    Cheers

    Ali

  • Paul Johnson 18 posts 109 karma points
    Jan 24, 2020 @ 14:16
Please Sign in or register to post replies

Write your reply to:

Draft