Dependency Injection package updated to Umbraco 9!

Dependency Injection package updated to Umbraco 9!

  • 13/10/2021
  • Packages
  • Umbraco 9, Nugets, Coding

Took me a bit longer than expected but I finally managed to upgrade my package to Umbraco 9.

For those who don't know what package I am talking about I recommend reading this article first: https://michaels-ugarage.cyber-solutions.at/packages/dependency-injection-with-nuget

You are wondering what changed in the upgrade? Luckily not much!

Only real change is that instead of the Umbraco Lifetime enum the Microsoft ServiceLifetime enum is now used.

                            
[RuntimeImplementation(typeof(Interface), Microsoft.Extensions.DependencyInjection.ServiceLifetime)]
                                
                            

Let's look at an example:

                            
[RuntimeImplementation(typeof(INavigationService), ServiceLifetime.Scope)]
public class NavigationService : INavigationService
{
    public NavigationModel GetNavigation()
    {

    }
}
                                
                            

Looks almost like the Umbraco 8 version, doesn't it?

But there is more to this update than just this little change!

As I encountered some cases where I wasn't able to inject the services I needed, mostly because of reflection, I decided to add a static accessible ServiceLocator.

Its usage is basically like the old Factory you could use in Umbraco 8.

                            
INavigationService service = CyberSolutions.UCore.DependencyInjection.Services.ServiceLocator.Current.GetInstance<INavigationService>();
INavigationService service = CyberSolutions.UCore.DependencyInjection.Services.ServiceLocator.Current.GetInstance(typeof(INavigationService));
                                
                            

Keep in mind the ServiceLocator should never be your go-to and only be used if there is no way to get the services via dependency injection!

With that beeing said here are the links for you to download the new version:

Install via Nuget: https://www.nuget.org/packages/CyberSolutions.UCore.DependencyInjection/2.0.0 or via console:

                            
Install-Package CyberSolutions.UCore.DependencyInjection -Version 2.0.0
                                
                            

I hope it will not only make your life easier but that I will also see you on my next blog post!

If you have any questions, feel free to contact me