Extension Hooks

Available for Premium tier

Whenever base code is run, it looks at the Extension to see if any custom code overrides or changes it. The primary way that communication happens is by using Hooks.

Here's an example of a Hook implementation. We'll show you how to invoke a Hook in your ExtensionEntry.cs for the basic implementation. This is the CreateAutoship() Hook using DirectScale.Disco.Extension.Hooks.Autoships;

using Microsoft.Extensions.DependencyInjection;
using System;
using DirectScale.Disco.Extension;
using DirectScale.Disco.Extension.Hooks;
using DirectScale.Disco.Extension.Hooks.Autoships;

namespace ExampleClient
  public class CreateAutoship : IHook<CreateAutoshipHookRequest, CreateAutoshipHookResponse>
  public CreateAutoship() { }

  public CreateAutoshipHookResponse Invoke(CreateAutoshipHookRequest request, Func<CreateAutoshipHookRequest, CreateAutoshipHookResponse> func)
    return func(request);

With func(request), the request is the raw data used to create the AutoShip (items, day of the month, etc.). The func() is a method pointer to the base/product function. When you call func(request), you're calling that function without doing anything extra. To use the Hook, you'll need to add your custom logic.

Register the Hook

In the ExtensionEntry class, in ConfigureServices(), register the Hook service.

public class ExtensionEntry : IExtension
  public void ConfigureServices(IServiceCollection services)
    services.AddTransient<IHook<CreateAutoshipHookRequest, CreateAutoshipHookResponse>, CreateAutoship>();

Did this page help you?