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. Each Hook calls a specific API. You can define custom logic to run before or after the API call.

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. You can add logic to call before the base functionality.


Register the Hook

You must register any Hook used in the ExtensionEntry:

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