Umbraco Commerce Builder

Learn more about the different options for configured Umbraco Commerce.

When it comes to configuring and extending Umbraco Commerce, such as by registering your own event handlers, we achieve this with the IUmbracoCommerceBuilder interface that can be accessed via a delegate function passed into the AddUmbracoCommerce() extension method called on the IUmbracoBuilder interface when explicitly registering Umbraco Commerce.

public class Startup
{
    ...
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddUmbraco(_env, _config)
            .AddBackOffice()
            .AddWebsite()
            .AddUmbracoCommerce(umbracoCommerceBuilder => {
                // Configure Umbraco Commerce here
            })
            .AddComposers()
            .Build();
    }
    ...
}

Registering Dependencies

The IUmbracoCommerceBuilder interface gives you access to the current IServiceCollection and IConfiguration to allow you to register dependencies like you would with the IUmbracoBuilder interface but its primary use case would be to access Umbraco Commerce's own collection builders, such as for registering validation or notification events, and any other Umbraco Commerce-specific configuration APIs.

...
.AddUmbracoCommerce(umbracoCommerceBuilder => {
    
    // Register validation events
    umbracoCommerceBuilder.WithValidationEvent<ValidateOrderProductAdd>()
            .RegisterHandler<MyOrderProductAddValidationHandler>();

})
...

As per the Dependency Injection docs, whilst you can register your dependencies directly within this configuration delegate, you may prefer to group your dependencies registration code into an extension method.

public static class UmbracoCommerceUmbracoBuilderExtensions
{
    public static IUmbracoCommerceBuilder AddMyDependencies(this IUmbracoCommerceBuilder builder)
    {
        // Register my dependencies here via the builder parameter
        ...

        // Return the builder to continue the chain
        return builder;
    }
}
...
.AddUmbracoCommerce(umbracoCommerceBuilder => {
    
    umbracoCommerceBuilder.AddMyDependencies();

})
...

Last updated