dI.Hook - How to remove hooks from HookRepository?
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?
So this is the seventh article in the series which will deal with removing one or more hook objects 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
Step 2- How to remove an instance of a hook from a repository?
When we are referring to adding an instance of a hook in a repository, there are multiple ways of doing it
Method 1 – Removing hook instance( s ) at a time
public void Test_Standard_RemoveHookObject()
{
hookRepository.Remove(new LogHook());
Assert.AreEqual(1, hookRepository.Hooks.Length);
}
Method 2 – Removing a List of hooks
public void Test_Standard_RemoveHookList()
{
hookRepository.Remove(new List<IHook> { new LogHook(), new DiagnosticsHook() });
Assert.AreEqual(0, hookRepository.Hooks.Length);
}
Method 3 – Removing hooks of a particular type
public void Test_Standard_RemoveHookType()
{
hookRepository.Remove(typeof(LogHook));
hookRepository.Remove(typeof(DiagnosticsHook));
Assert.AreEqual(0, hookRepository.Hooks.Length);
}
Method 4 – Removing all hooks
public void Test_Lazy_RemoveAll()
{
hookRepository.RemoveAll();
Assert.AreEqual(0, hookRepository.Hooks.Length);
}
So there are 4 ways to remove a hook from the repository so that any method using OnInvoke method on the repository will find the removed hook in the repository