4 minute read

 

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:

  1. How to create a HookRepository?
  2. How to add hooks to HookRepository?
  3. How to add hooks via configuration file to a HookRepository?
  4. How to invoke all hooks using dI.Hook?
  5. How to invoke hooks that satisfy a condition?
  6. How to retrieve an object of hook?
  7. 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

 

Ignoring all hooks
[TestMethod]
[RemoveAllHooks]
public void Test_Standard_RemoveHooks()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(0, invokedHooks);
}

 

Method Two – Ignoring hooks with particular name

Ignore hooks with name LogHook
[TestMethod]
[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

Ignore multiple hooks
[TestMethod]
[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

Ignoring multiple hooks
[TestMethod]
[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

 

Ignoring a hook type
[TestMethod]
[RemoveHookType(typeof(LogHook))]
public void Test_Standard_RemoveHooksWithType()
{
    int invokedHooks = hookRepository.InvokeAll();
    Assert.AreEqual(hookRepository.Hooks.Length - 1, invokedHooks);
}

or

Ignoring multiple hook types
[TestMethod]
[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.