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?

So this is the fifth article in the series which will deal with invoking all the hooks in the repository.  This article will be a deviation from the fourth article on invoking all hooks.

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

Step 2 – Invoking filtered hooks in the repository

 

Variation One – InvokeWhere

InvokeWhere takes 2 parameters. First – A predicate that returns Boolean value and input as IHook object. Second – Input parameters to the OnInvoke method of each hook.   The OnInvoke method of a hook is called only when the predicate returns true value.

An example of this would be,

InvokeWhere with predicate
int invokedCount = hookRepository.InvokeWhere(x =>
{
if (x is IAdditionalInterface)
{
return (x as IAdditionalInterface).IQ == "IQ.001";
}
return false;
});

 

Variation Two – InvokeWhen with a predicate that takes no input

 

InvokeWhere takes 2 parameter. First - A predicate that returns Boolean value without any input. Second – Input parameters to the OnInvoke method of each hook.   The OnInvoke method of a hook is called only when the predicate returns true value.

An example of this would be,

InvokeWhen with predicate
int invokedCount = hookRepository.InvokeWhen(() =>
{
return false;
});

 

Variation Three – InvokeWhen with a predicate that takes no input, another that takes IHook object

InvokeWhere takes 3 parameter. First - A predicate that returns Boolean value without any input. Second - predicate that returns Boolean value and input as IHook object. Third – Input parameters to the OnInvoke method of each hook.   The OnInvoke method of a hook is called only when both the predicates return true value.

An example of this would be,

InvokeWhen with 2 predicates
int invokedCount = hookRepository.InvokeWhen(() => { return true; },
(x) =>        {
if (x is IAdditionalInterface)
{
return (x as IAdditionalInterface).IQ == "IQ.001";
}
return false;
});

 

So using InvokeWhere and InvokeWhen you can filter the hooks to be invoked.