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()));
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.
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!
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:
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.
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.
I really appreciate if someone can help!
Cheers
Ali
Hi Ali
You can get underlying reference to LightInject in a composition via:
and lightinject has Assembly Scanning capabilities:
https://www.lightinject.net/#assembly-scanning
So I'm thinking you could call something like:
or
if that is the kind of thing you are after?
regards
marc
Hi Marc,
Thanks for your post.
However, what I am after is a little bit different, it looks like
does not register IHandler
Still no luck with that.
Cheers
Ali
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.
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
for open generic Autofac components equivalence...
regards
Marc
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<,>
andIHandler<,,>
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:
Because I have many of these classes that implements
IHandler<,>
and same forIHandler<>
&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:
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
Relevant github issue https://github.com/umbraco/Umbraco-CMS/issues/7502
is working on a reply...