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

LogHook
  1. public class LogHook : IHook
  2.     {
  3.         public string Name { get; set; }
  4.         public Guid Id { get; set; }
  5.         public LogHook()
  6.         {
  7.             Name = "LogHook";
  8.             Id = new Guid("B3D75F63-F7DA-4939-8777-1A354202B9D2");
  9.         }
  10.  
  11.         public void OnInvoke(params object[] inputParams)
  12.         {
  13.             Console.WriteLine("Hook Called");
  14.         }
  15.  
  16.         public void Dispose()
  17.         {
  18.             Name = null;
  19.             Console.WriteLine("Woah! Hook Disposed!");
  20.         }
  21.     }

 

DiagnosticsHook – Implements multiple interfaces (IHook, required for HookRepository) and IAdditionalInterface

DiagnosticsHook
  1. public class DiagnosticsHook : IHook,
  2.         IAdditionalInterface
  3. {
  4.     public string Name { get; set; }
  5.     public Guid Id { get; set; }
  6.  
  7.     public DiagnosticsHook()
  8.     {
  9.         Name = "DiagnosticsHook";
  10.         Id = new Guid("CB75FCCF-593B-4BCF-871B-298087CDE741");
  11.     }
  12.  
  13.     public void OnInvoke(params object[] inputParams)
  14.     {
  15.         Console.WriteLine("Hook Called");
  16.     }
  17.  
  18.     public void Dispose()
  19.     {
  20.         Name = null;
  21.         Console.WriteLine("Woah! Hook Disposed!");
  22.     }
  23.  
  24.     public string IQ
  25.     {
  26.         get { return "IQ.001"; }
  27.     }
  28. }

 

 

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

Adding one hook at a time
  1. hookRepository.Add(new LogHook());
  2. hookRepository.Add(new DiagnosticsHook());

Method 2 – Adding a List of hooks

Adding a List of hooks
  1. hookRepository.Add(new List<IHook> { new LogHook(), new DiagnosticsHook()}) ;

Method 3 – Adding two different instances of same hook type

Adding 2 LogHooks
  1. LogHook logHook1 = new LogHook(); /* Default Guid */
  2. LogHook logHook2 = new LogHook();
  3. logHook2.Id = new Guid("B9D2D1E3-6BAD-47C3-AEFC-3ABFAAB210F8");
  4. hookRepository.Add(new[] { logHook1, logHook2 });

 

Method 4 – Adding by searching within an Assembly

Searching within an Assembly
  1. hookRepository.Add(SearchScope.CallingAssembly, SearchBy.Name, Operator.Like, "Log");

 

Method 5 – Adding by searching within a directory

Searching within a directory
  1. 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

Add a Hook Type in Repository
  1. hookRepository.Add(typeof(LogHook));
  2. hookRepository.Add(typeof(DiagnosticsHook));

 

So you can add a hook by six different ways in a Hook Repository

Related Posts: