dI.Hook - Load Hooks through Configuration
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>
<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:
Step 2 – Loading Configuration into Repository
To load configuration of a repository with name ‘default’, the code is fairly simple
- hookRepository.LoadConfiguration();
To load a specific repository. Say, productionRepository from configuration from a section named ‘dIHookConfiguration’ in the ApplicationConfiguration the code should be
- 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
public void Test_Standard_LoadFromConfigFileWithRepositoryNameDisabled()
{
hookRepository.RemoveAll();
hookRepository.LoadConfiguration("dIHookConfiguration", "productionRepositoryDisabled");
Assert.AreEqual(0, hookRepository.Hooks.Length);
}