Skip to the content

Different Ways to Inject Events, Helpers and Services into Umbraco 9

After having a good play around with Umbraco 9 & .Net Core Dependency Injection I realized there is a few ways to inject your events, helpers are services.

  1. Injecting directly into the startup.cs
  2. Composing, similar to how we used to do in Umbraco 8
  3. Creating an extension method for IUmbracoBuilder and injecting a collection into startup.cs

I have broken down and added examples of each below.

Injecting directly into the startup.cs

The easy way is to inject your events, helpers, or services directly into the startup.cs. I believe this is the default .Net Core way and possibly the way you should go unless writing a package which I think might be better composing that.

Here are some examples of injecting directly into the startup.cs file.

public void ConfigureServices(IServiceCollection services)
{
     #pragma warning disable IDE0022 // Use expression body for methods
     services.AddUmbraco(_env, _config)
          .AddBackOffice()
          .AddWebsite()
          .AddComposers()
          .AddNotificationHandler<ContentPublishingNotification, StartupTestEvent>()
          .Build();
    services.AddSingleton<ISiteService, SiteService>();
    services.AddSingleton<IBlogService, BlogService>();
     #pragma warning restore IDE0022 // Use expression body for methods
}

As you can see I have injected injected an ContentPublishingNotification(PublishingEvent) and two services SiteService and BlogService.

Composing, similar to how we used to do in Umbraco 8

You can also include your events (notifications, helpers, and services using the same technique we used to use for Umbraco 8 called composing. I think this technique is probably the best way to go when you are building packags. My assumption when building packages is it might not be that easy to maintain the startup.cs file without potentially creating issues. Therefore, I think composing might be the better way to go about it with Packages. This way I think it will work great as it did with Umbraco 8.

The code looks slightly different but not in a major way. Here is an example of Composing a service in Umbraco 9.

public class SiteHelperComposer : IUserComposer
{
     public void Compose(Composition composition)
     {
          composition.Register<ISiteHelper, SiteHelper>(Lifetime.Singleton);
     }
}

I actually wrote a more details blog about composing a service in Umbraco 9 here.

Creating an extension method for IUmbracoBuilder and injecting a collection into startup.cs

Personally, if I am not creating a package, I think I like this way the most. If you are like me I like to store a lot of code in your own helper / service classes then you will soon find that startup.cs file is going to start getting very messy. Some projects have a lot of events, helpers, services, providers etc. You can imagine the startup.cs file is going to start filling up and looking messy.

Instead, you can create an extension method for IUmbracoBuilder to inject a collection of events or services in one. Let me explain better by showing you some code.

public static class EventExtensions
{
     public static IUmbracoBuilder AddEvents(this IUmbracoBuilder builder)
     {
           builder
           .AddNotificationHandler<ContentPublishingNotification, PublishingEvent>()
           .AddNotificationHandler<ContentPublishedNotification, PublishedEvent>()
           .AddNotificationHandler<ContentMovedToRecycleBinNotification, ContentMovedToRecycleBinEvent>();
           return builder;
     }
}
public static class ServiceExtensions
{
    public static IUmbracoBuilder AddServices(this IUmbracoBuilder builder)
    {
         builder.Services
         .AddSingleton<ISiteService, SiteService>()
         .AddSingleton<IBlogService, BlogService>();
         return builder;
     }
}
public void ConfigureServices(IServiceCollection services)
{
#pragma warning disable IDE0022 // Use expression body for methods
services.AddUmbraco(_env, _config)
.AddBackOffice()
.AddWebsite()
.AddComposers()
.AddServices()
.AddEvents()
.Build();
#pragma warning restore IDE0022 // Use expression body for methods
}

As you can see I create two extention methods. The first injecting all my events. The second injecting all my services. I then add one line of code for each in my statup.cs code. If I decide to add any more services or events I simply update the the corresponding extension method rather than editing the startup.cs.

I think over time this will be much neater.

Let me know your thoughts by commenting below. What method do you prefer and why?

I am interested in hearing your reasons why.

I did consider composing everything like we did with Umbraco 8. I imagined just having one way to do all. Packages and my base code. I just wasn't sure how this effects performance and what other negatives there might be of doing this.

Maybe some one on the Umbraco Core team and shed some light on this?

About the author

David Armitage

.Net MVC Developer
.Net Core Developer
Umbraco Certified Master
Recruitment Professional

Hey Peeps,

I'm an entrepreneur and web developer with a passion for coding. I absolutely love working with Umbraco CMS and appreciate the Umbraco community even more.

I've got 10 years+ .Net experience and 7 years+ experience working in the recruitment industry, both coding and marketing job websites. I wanted to use my skills to help give something back to this awesome community and so I decided to build UmbraJobs.com.

My vision & fundamentals

I want this website to be a place where Umbraco Professionals and Umbraco Partners can advertise their services but at the same time I want to filter out all the under-qualified freelancers & agencies that take up the biggest part of other job & freelancer websites. We've all worked hard to gain our Umbraco certifications, partnership status, and help build the Umbraco community to what it is today. I want to see everyone get rewarded for their efforts.

Follow me on social media

If you're an Umbraco professional, partner, employer, recruiter or a passionate Umbraco community builder then I'm more than happy to hear from you. Follow me on my social media channels and please reach out if you have any needs, want help or consultation with anything Umbraco related or just want a general chat.

comments powered by Disqus

Blog Filter


How we can help?

Need help with an Umbraco project?

Need help with a project but not too sure who to approach? Send us your project brief and budget. We will provide a free consultation and can help you gather quotes from the best and most suitable freelancers or agencies.

Looking to hire an Umbraco Professional?

Have you got job vacancy and want to hire an Umbraco professional? Post a job on our website for free. Alternatively let us know your requirements and we will send suitable prospects your way.

Claim your free profile!

Are you an Umbraco Freelance Developer or Umbraco partner that wants to advertise on our site? If you work with Umbraco or registered as an Umbraco partner then you can create a profile for free.

Let's build the Umbraco Community

We're big on building the Umbraco community and we think you guys are awesome! If there's anyway at all we can help then please reach out.