I need to connect your DI container with my own SimpleInjector DI container. Right now I have tried to create a concrete class of the IFactory interface and replace your Current.Factory. That is not possible because it can only be set once - and setting it before you, makes your code throw an exception and vice versa.
I have also tried to create my own WebRunTime and inherit from yours and then pass it my own IFactory - doesn't work either.
The only working thing is to override ControllerBuilder.Current.SetControllerFactory with my own ControllerFactory and then register all your services in my container. I am not a huge fan of this method, because we are not sure what of your controller specific things we override.
Do you have any suggestion on how to do this? It is not possible to switch to your LightInjector.
We are writing our own implementation of IRegister and IFactory and decorate our factory with the default Umbraco 8 factory. And then we are setting the container in the factory.
public class SimpleInjectorUmbracoFactory : IFactory
{
private Container _container;
private readonly IFactory _umbracoDefaultFactory;
private IEnumerable<Assembly> _mvcControllerAssemblies;
private IEnumerable<Assembly> _apiControllerAssemblies;
private SimpleInjectorUmbracoFactory(IFactory umbracoDefaultFactory)
{
_umbracoDefaultFactory = umbracoDefaultFactory;
}
public static SimpleInjectorUmbracoFactory Current { get; private set; }
public static SimpleInjectorUmbracoFactory Create(IFactory umbracoDefaultFactory)
{
return Current ?? (Current = new SimpleInjectorUmbracoFactory(umbracoDefaultFactory));
}
...
...
}
And the Registry looks something like this:
public class SimpleInjectorUmbracoRegistry : IRegister
{
private readonly LightInjectContainer _lightInjectContainer;
public SimpleInjectorUmbracoRegistry()
{
_lightInjectContainer = LightInjectContainer.Create();
}
public static IRegister Create()
=> new SimpleInjectorUmbracoRegistry();
...
...
}
It is a bit complext sadly and I can't show you everything due to copyright, but it is possible to use two different DI frameworks at once. :-) And the IRegister and IFactory is the way in.
Umbraco 8 Dependency Injection override/proxy
Hi.
I need to connect your DI container with my own SimpleInjector DI container. Right now I have tried to create a concrete class of the IFactory interface and replace your Current.Factory. That is not possible because it can only be set once - and setting it before you, makes your code throw an exception and vice versa.
I have also tried to create my own WebRunTime and inherit from yours and then pass it my own IFactory - doesn't work either.
The only working thing is to override
ControllerBuilder.Current.SetControllerFactory
with my own ControllerFactory and then register all your services in my container. I am not a huge fan of this method, because we are not sure what of your controller specific things we override.Do you have any suggestion on how to do this? It is not possible to switch to your LightInjector.
Many regards Malthe
Hi Malthe
Did you find a solution?
Hi Jesper.
Yeah we found a solution.
We are writing our own implementation of IRegister and IFactory and decorate our factory with the default Umbraco 8 factory. And then we are setting the container in the factory.
Hi Malthe Petersen,
I am using Umbraco 8. I need to implement DI, can you please share me the code.
Hi Skp.
Sorry for late answer.
For the Factory we did something like:
And the Registry looks something like this:
It is a bit complext sadly and I can't show you everything due to copyright, but it is possible to use two different DI frameworks at once. :-) And the IRegister and IFactory is the way in.
Regards Malthe
is working on a reply...