Dependency Injection with Nuget

Dependency Injection with Nuget

  • 17/09/2021
  • Packages
  • Umbraco 8, Coding, Nugets

Last time we discussed how dependency injection in Umbraco 8 works and that it can become quite tedious the more services and repos you have.

I also told you that I created a package to make my life easier.

Today I wanna share that package with you all, for that purpose I am presenting to you my first package:

UCore.DependencyInjection

Thanks to this package you no longer need to register each of your services in a composer.

Instead what you will do is you will just write the RuntimeImplementation attribute from the namespace CyberSolutions.UCore.DependencyInjection.Attribute on top of your implementation class.

                            
[RuntimeImplementation(typeof(Interface), Umbraco.Core.Composing.Lifetime)]
                                
                            

For a practical use of it let me show you an example:

Let's take our example from the last blog with the NavigationService which implements the INavigationService interface

                            
public interface INavigationService
{
    NavigationModel GetNavigation();
}

public class NavigationService : INavigationService
{
    public NavigationModel GetNavigation()
    {

    }
}
                                
                            

Previously we would need to register the implemenation in a composer for Umbraco to know.

Now, all we need to do is to add the RuntimeImplementation attribute on top of the NavigationService class like that:

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

    }
}
                                
                            

No need for a composer any longer. How does this work?

Basically when Umbraco starts, the package looks for all classes with this attribute and gathers the needed information in order to register it in the composition.

We are already using this package internally for quite a while and we absolutly love it!

It saves us a lot of time and headache as we don't need to care about registering every implemenation within a composer, instead using the attribute integrates so smoothly with our personal coding flow.

We can no longer imagine a project without this package!

You also wanna try it out?

Just get it from nuget: https://www.nuget.org/packages/CyberSolutions.UCore.DependencyInjection/1.0.0 or via console:

                            
Install-Package CyberSolutions.UCore.DependencyInjection -Version 1.0.0
                                
                            

Found problems or have suggestions? Just check out our GitHub and create an Issue: https://github.com/Cyber-Solutions-Software/CyberSolutions.UCore.DependencyInjection

I really hope you gonna like it and that we will see again next time when I will show you how to render a MVC view in the backoffice!

If you have any questions, feel free to contact me