dI.Hook - How to ignore hook on specific method?
If you are new to dI.Hook, please visit the di.Hook Product Page for Overview, Release Version and Source Code.
Some posts you might want to read would be:
- How to create a HookRepository?
- How to add hooks to HookRepository?
- How to add hooks via configuration file to a HookRepository?
- How to invoke all hooks using dI.Hook?
- How to invoke hooks that satisfy a condition?
- How to retrieve an object of hook?
- How to remove hooks from HookRepository?
So this is the eighth article in the series which will deal with
- Ignoring one or more hook objects when executing OnInvoke in a method
- but NOT removing the hook from the repository
Step 1 – Creating a repository and add hooks into a repository
The first decision in creating a hook repository is to think whether you want to have lazy loading or not. Once you have decided you can go through the steps – Creating a Hook Repository using dI.Hook
Be selective in adding hooks into a repository if you plan to invoke all of them at one go. If you are thinking of manually adding hooks to a repository, would suggest going through – Adding hooks manually into a repository. However, if you are planning to load them through configuration you can read – Loading a configuration set of hooks into repository
How to ignore a hook being invoked on a method?
The repository created in step 1 consists of 2 hooks.
Method One – Ignoring all hooks
[RemoveAllHooks]
public void Test_Standard_RemoveHooks()
{
int invokedHooks = hookRepository.InvokeAll();
Assert.AreEqual(0, invokedHooks);
}
Method Two – Ignoring hooks with particular name
[RemoveHook(new[] { "LogHook" })]
public void Test_Standard_RemoveHooksWithName()
{
int invokedHooks = hookRepository.InvokeAll();
Assert.AreEqual(1, invokedHooks);
}
or, you can have multiple hook names in the list
[RemoveHook(new[] { "LogHook", "DiagnosticsHook" })]
public void Test_Standard_RemoveHooksWithName_MultipleHooks()
{
int invokedHooks = hookRepository.InvokeAll();
Assert.AreEqual(0, invokedHooks);
}
or, using multiple attributes for doing the same
[RemoveHook("LogHook")]
[RemoveHook("DiagnosticsHook")]
public void Test_Standard_RemoveHooksWithName_MultipleAttributesInMethod()
{
int invokedHooks = hookRepository.InvokeAll();
Assert.AreEqual(0, invokedHooks);
}
Method Three – Ignore a hook of specific type
[RemoveHookType(typeof(LogHook))]
public void Test_Standard_RemoveHooksWithType()
{
int invokedHooks = hookRepository.InvokeAll();
Assert.AreEqual(hookRepository.Hooks.Length - 1, invokedHooks);
}
or
[RemoveHookType(typeof(LogHook))]
[RemoveHookType(typeof(DiagnosticsHook))]
[RemoveHookType(typeof(LogHook))]
public void Test_Standard_AddRemoveHookCombination()
{
int invokedHooks = hookRepository.InvokeAll();
Assert.AreEqual(0, invokedHooks);
}
So using these techniques, you can have a method ignore one or many hooks. These hooks will still remain in the repository and methods without these attributes will continue to invoke their OnInvoke method.