Composing with Umbraco Commerce
# help-with-umbraco
c
Hi all! Using Umbraco Commerce, it seems like all the documentations seem to point into having to register custom Notification Events (like OrderCreatedNotification) using the IUmbracoCommerceBuilder to register said handler. The only way it seems you can access this is through the startup.cs pipeline's IUmbracoBuilder ".addUmbracoCommerce" and extending on the .IUmbracoCommerceBuilder config you get from that... Is it not possible to register Commerce NotificationEvents through IComposers so you don't have to touch the startup.cs? đŸ¤”
j
Couldn't you do the same in a composer? Something like:
Copy code
csharp
public class ExampleComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
    {
        builder.AddUmbracoCommerce(commerceBuilder =>
        {
            // Register MyOrderFinalizedHandler to execute before the SomeOtherNotificationHandler handler
            commerceBuilder.WithNotificationEvent<OrderFinalizedNotification>()
                .RegisterHandlerBefore<SomeOtherNotificationHandler, MyOrderFinalizedHandler>();

            // Register MyOrderFinalizedHandler to execute after the SomeOtherNotificationHandler handler
            commerceBuilder.WithNotificationEvent<OrderFinalizedNotification>()
                .RegisterHandlerAfter<SomeOtherNotificationHandler, MyOrderFinalizedHandler>();
        });
    }
}
So have a composer where you add the .AddUmbracoCommerce extension to the Umbraco builder and then within that add the IUmbracoCommerceBuilder registrations? Or set up several builder extensions to be called there if you want to split it up more
c
Hi @Jemayn, in theory I could do that, but the problem is that calling "builder.addUmbracoCommerce" already registers a bunch of services, notification and such by itself, making it so that (I think?) you cannot/should not call builder.addUmbracoCommerce twice in an application... or am I wrong with that? I was hoping I could ship something that dynamically registered the notification into an already existing instance of UmbracoCommerceBuilder so that you won't run into a case where an application may call .addUmbracoCommerce twice đŸ˜„
(Scope: Wanting to make a NuGet package you can install that relies on said notifications, without having the installing user have to mess with configuring said notificationhandlers themselves
j
I think you are correct, my example would work if you set it up yourself / guide a package user to change their DI setup. But automatically registering would probably require something similar to the IComposer that scans and adds from multiple sources, don't know if that is possible - I'd probably post the question on the issue tracker đŸ™‚
c
I fear so too... thanks for thinking along, it's much appreciated! I'll head over to the issue tracker! đŸ™‚
c
Hmm it appears as though you can actually register events through an instance of IBuilder.... that's very good to know!
I'll give that a go
Hah okay, that method is marked obsolete đŸ˜‚
Darn
though I don't think that really matters for me.... really thinking out loud here... I'll continue!
j
Haha yea indeed, but in that example they just ignore the warning - could wait and see what they do for newer versions, and potentially open an issue on the commerce issue tracker to let the team know this is a needed feature? Then if it gets obsoleted they will probably find a new way to do it đŸ™‚
c
It works!! đŸ™Œ Once again a huge thank you Jesper! I'll be sure to open an issue so that the team knows that this is a needed feature! đŸ™‚
30 Views