title: Best way to use shared components in Chain of Responsibility pattern
body: <p>I have a problem with Chain of Responsibility pattern.
  All handlers implements this class:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Chain of Responsibility pattern
  /// &lt;/summary&gt;
  abstract public class ChainHandler
  {
      protected LogWrapper Log;
      protected ChainHandler successor;
  
      /// &lt;summary&gt;
      /// SetSuccessor
      /// &lt;/summary&gt;
      private void SetSuccessor(ChainHandler successor)
      {
         this.successor = successor;
      }
  
      protected oAPI NotImplemented
      {
          get { return new oAPI(HTTPCodes.NotImplemented); }
      }
  
      /// &lt;summary&gt;
      /// Set Successor to the end of chain
      /// &lt;/summary&gt;
      /// &lt;param name="successor"&gt;Handler&lt;/param&gt;
      public void Add(ChainHandler successor)
      {
          if (this.successor == null) SetSuccessor(successor);
          else
          {
              this.successor.Add(successor);
          }
      }
      protected oAPI ReferToSuccessor (iAPI request)
      {
          if (successor != null) return successor.HandleRequest(request);
          return NotImplemented;
      }
      /// &lt;summary&gt;
      /// output API builder
      /// &lt;/summary&gt;
      /// &lt;param name="code"&gt;HTTP code&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      protected oAPI RB(HTTPCodes code, string messsage = null, string data = null, bool hasError = false)
      {
          return new oAPI(Shared.User, code, message) { Data = data, HasError = hasError };
      }
      /// &lt;summary&gt;
      /// Serializer (JSON)
      /// &lt;/summary&gt;
      public Func&lt;object, string&gt; Serializer { get; set; }
      /// &lt;summary&gt;
      /// Handle request
      /// &lt;/summary&gt;
      /// &lt;param name="request"&gt;request&lt;/param&gt;
      /// &lt;returns&gt;result&lt;/returns&gt;
      public abstract oAPI HandleRequest(iAPI request);
  }
  </code></pre>
  
  <p>Then I implement <code>DocHandler</code></p>
  
  <pre><code>public class DocHandler:ChainHandler
      {
       public DocChain()
       {
        Log = new LogWrapper(this);
       }
       public override oAPI HandleRequest(iAPI request)
       {
        switch (request.Comand)
        {
         case iAPI.Target.GetModel:
          return GetModel(request.DocID);
        }
        return ReferToSuccessor(request);
        }
   private Doc GetDoc(int id)
      {
       Log.Debug("get document by id: " + id);
       using (var unit = UnitOfWork.Create())
       {
        var repository = unit.GetRepository&lt;Doc&gt;();
        Doc doc = repository.Get(id);
        if (doc == null)
        {
         Log.Error("Document not found");
         throw new DocumentNotFoundException();
        }
        return doc;
       }
      }
  
   public oAPI GetModel(int DocId)
        {
         var Model = GetDoc();
         return RB(HTTPCodes.OK, data: Serializer(
         Model));
         }
  }
  </code></pre>
  
  <p>And <code>CloudHandler</code></p>
  
  <pre><code> public class CloudHandler:ChainHandler
      {
          private IDAVService _service;
          private string RemoteRepository;
          public CloudChain(IDAVService service)
          {
              Log=new LogWrapper(this);
              _service=service;
          }
  
          public override oAPI HandleRequest(iAPI request)
          {
              switch (request.Comand)
                      {
                      case iAPI.Target.UploadModel:
                      return Upload(request.DocID,request.VersionID);
                      case iAPI.Target.DownloadModel:
                      return Download(request.VersionID, request.DocID);
                      }
              return ReferToSuccessor(request);
          }
          public oAPI Upload(int DocID,int VersionID)
          {
              // Need to get model from DocHandler
              var model = ???
              service.Upload(model);
              return RB(HTTPCodes.OK);
          }
          public oAPI Download(int DocID,int VersionID)
          {
              // Need to get model from DocHandler
              var model = ???
              service.Download(model);
              return RB(HTTPCodes.OK);
          }
      }
  </code></pre>
  
  <p>And my problem in finding the best way to share methods and properties between handlers.
  Now I use static class SharedComponents where each handler delegate own method.</p>
  
  <pre><code>public static class SharedComponents
      {
          public static Func&lt;int, Doc&gt; GetDoc;
      }
  </code></pre>
  
  <p>In DocHandler I delegate method <code>SharedComponents.GetDoc = this.GetDoc;</code> and then use it in CloudHandler <code>var Model = SharedComponents.GetDoc(docid)</code>. This is spagetti to write delegates to hundred of shared methods.</p>
  
  <p>But how I tested this? I will must initialize <strong>all</strong> handlers (because <code>A</code> uses method of <code>B</code> and <code>B</code> may use methods of <code>C</code> etc.) to test <strong>one</strong> method in one handler. Horror!</p>
  
  <p>I try to set shared methods as static to use like <code>var Model = DocHandler .GetDoc(docid)</code>. But this solution breaking Dependency inversion principle. And if some shared method use context (like <code>session</code> in <code>UnitOfWork</code>) to I need in test initialise all handlers again!</p>