dI.Hook - How to add hooks to HookRepository?
If you are new to dI.Hook, please visit the product page for Overview, Release Version and Source Code.
Hook – A hook is a .NET object derived from the interface IHook. If you already have classes that have business/technical functionalities, implementing IHook will not impact their functionality. If your classes have similar function names, it is advisable to explicitly implement interface IHook.
To illustrate this, let’s create two hooks LogHook and DiagnosticsHook. We will not focus on their implementation and limit our scope to their definition and adding them to HookRepository
- public class LogHook : IHook
- {
- public string Name { get; set; }
- public Guid Id { get; set; }
- public LogHook()
- {
- Name = "LogHook";
- Id = new Guid("B3D75F63-F7DA-4939-8777-1A354202B9D2");
- }
- public void OnInvoke(params object[] inputParams)
- {
- Console.WriteLine("Hook Called");
- }
- public void Dispose()
- {
- Name = null;
- Console.WriteLine("Woah! Hook Disposed!");
- }
- }
DiagnosticsHook – Implements multiple interfaces (IHook, required for HookRepository) and IAdditionalInterface
- public class DiagnosticsHook : IHook,
- IAdditionalInterface
- {
- public string Name { get; set; }
- public Guid Id { get; set; }
- public DiagnosticsHook()
- {
- Name = "DiagnosticsHook";
- Id = new Guid("CB75FCCF-593B-4BCF-871B-298087CDE741");
- }
- public void OnInvoke(params object[] inputParams)
- {
- Console.WriteLine("Hook Called");
- }
- public void Dispose()
- {
- Name = null;
- Console.WriteLine("Woah! Hook Disposed!");
- }
- public string IQ
- {
- get { return "IQ.001"; }
- }
- }
How to add an instance of a hook in 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 – Adding one hook instance at a time
- hookRepository.Add(new LogHook());
- hookRepository.Add(new DiagnosticsHook());
Method 2 – Adding a List of hooks
- hookRepository.Add(new List<IHook> { new LogHook(), new DiagnosticsHook()}) ;
Method 3 – Adding two different instances of same hook type
- LogHook logHook1 = new LogHook(); /* Default Guid */
- LogHook logHook2 = new LogHook();
- logHook2.Id = new Guid("B9D2D1E3-6BAD-47C3-AEFC-3ABFAAB210F8");
- hookRepository.Add(new[] { logHook1, logHook2 });
Method 4 – Adding by searching within an Assembly
- hookRepository.Add(SearchScope.CallingAssembly, SearchBy.Name, Operator.Like, "Log");
Method 5 – Adding by searching within a directory
- hookRepository.Add(SearchScope.CurrentDirectory, SearchBy.Name, Operator.Like, "Log");
How to add a type of a hook in a repository?
If your repository supports lazy loading, then adding a type of hook in a repository is preferred over adding an instance of a hook. By adding a type in the repository, the instance of hook is not created until it is invoked or searched for.
However, if your repository does not support lazy loading you can choose any mechanism of adding hook into repository
- hookRepository.Add(typeof(LogHook));
- hookRepository.Add(typeof(DiagnosticsHook));
So you can add a hook by six different ways in a Hook Repository