lundi 27 novembre 2023

Decorating class having methods internally calling each other

I have a class IMessageProcessor

public interface IMessageProcessor{
   Task ProcessMessages();
   Task ProcessMessage(Message message);
}

with simplified implementation:

public class MessageProcessor : IMessageProcessor {
   public async Task ProcessMessages(){
      ...
      foreach (Message message in messages){
         await ProcessMessage(message)
      }
      ...
   }

   public Task ProcessMessage(Message message){
      // processing message
   }
}

I have also implemented decorator to be kind of error interceptor/handler:

public class ErrorHandlerDecorator : IMessageProcessor {
   IMessageProcessor decorated;

   public async Task ProcessMessages(){
      try{
         decorated.ProcessMessages();
      }
      catch{
         // exception handling logic
      }
   }

   public Task ProcessMessage(Message message){
      try{
         decorated.ProcessMessage(message);
      }
      catch{
         // other exception handling logic (retries etc.)
      }
   }
}

The problem is when I call ProcessMessages and hitting ProcessMessage method, my decorated ProcessMessage error handling logic is not executed as I call it directly on MessageProcessor instance.

Is this possible to somehow make decorator works when I call some methods internally in decorated class?

Thanks

Aucun commentaire:

Enregistrer un commentaire