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>();
}
}
}
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>());
}
}
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.
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:
I'm struggling to do the same with the new composition code. Currently I'm having to manually type out all the dependencies:
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):
Am I barking up completely the wrong tree?
Thanks,
Chris
Hi Chris, I'm on the same quest. Did you find a solution for your question?
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
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:
Hope this helps. Cheers,
Ruben
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
is working on a reply...