3 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" ?>
<configuration>
  <configSections>
    <section name="dIHookConfiguration" type="dIHook.Configuration.dIHookConfigurationSection, dI.Hook"/>
  </configSections>
  <dIHookConfiguration>
    <repositories>
      <repository name="default" enabled="true">
        <hooks>
          <hook name="h1" type="dIHook.UnitTests.Hooks.LogHook, dI.Hook.UnitTests" />
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
        </hooks>
      </repository>
      <repository name="productionRepository" enabled="true">
        <hooks>
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
        </hooks>
      </repository>
      <repository name="productionRepositoryDisabled" enabled="false">
        <hooks>
          <hook name="h2" type="dIHook.UnitTests.Hooks.DiagnosticsHook, dI.Hook.UnitTests" />
        </hooks>
      </repository>
    </repositories>
  </dIHookConfiguration>
</configuration>

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:

      repositoryConfig

       

      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
      [TestMethod]
      public void Test_Standard_LoadFromConfigFileWithRepositoryNameDisabled()
      {
          hookRepository.RemoveAll();
          hookRepository.LoadConfiguration("dIHookConfiguration", "productionRepositoryDisabled");

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