dI.Hook - Load Hooks through Configuration

1 minute read


If you are new to dI.Hook, please visit the product page for Overview, Release Version and Source Code.

One of the ways to adding hook(s ) in the HookRepository is to define them in Application Configuration file and then loading an appropriate configuration in repository.  The other ways are mentioned in How to Add Hooks in a Repository post

Step 1 – Defining configuration


Configuration - Sample
<?xml version="1.0" encoding="utf-8" ?>
    <section name="dIHookConfiguration" type="dIHook.Configuration.dIHookConfigurationSection, dI.Hook"/>
      <repository name="default" enabled="true">
          <hook name="h1" type="dIHook.UnitTests.Hooks.LogHook, dI.Hook.UnitTests" />
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
      <repository name="productionRepository" enabled="true">
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
      <repository name="productionRepositoryDisabled" enabled="false">
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />

The above configuration has an element repositories that has multiple repository elements.

A repository element has following attributes

  • Name: This represents a unique name of the repository.  A repository is fetched from configuration using this name.
  • Enabled: If the value is ‘false’ then that repository configuration is not considered when loading the configuration.  In the above configuration, when repository ‘productionRepositoryDisabled’ is loaded into a HookRepository it will not have any hooks.

    Each repository element represents a collection or container of hooks  elements.  Each hooks element has multiple hook elements.

    A hook element has multiple attributes

    • Name: Name of the hook
    • Type: Fully qualified type of the hook to be added to the repository.  This hook should inherit from IHook interface

      This relation can be summarized as:



      Step 2 – Loading Configuration into Repository


      To load configuration of a repository with name ‘default’, the code is fairly simple

      Loading 'default' repository
      1. hookRepository.LoadConfiguration();


      To load a specific repository. Say, productionRepository from configuration from a section named ‘dIHookConfiguration’ in the ApplicationConfiguration the code should be

      Loading custom repository
      1. hookRepository.LoadConfiguration("dIHookConfiguration", "productionRepository");

      If the ‘enabled’ property of a repository is false, there will be no hooks added in the repository.  To verify this, you can try the code below

      Loading disabled repository
      public void Test_Standard_LoadFromConfigFileWithRepositoryNameDisabled()
          hookRepository.LoadConfiguration("dIHookConfiguration", "productionRepositoryDisabled");

          Assert.AreEqual(0, hookRepository.Hooks.Length);