Decoration with several interface

前端 未结 3 1412
闹比i
闹比i 2021-02-09 20:19

I am always struggling with decoration + interfaces. Say I have the following \'behavior\' interfaces :

interface IFlyable { void Fly();}
interface ISwimmable {          


        
3条回答
  •  我寻月下人不归
    2021-02-09 20:42

    You will need to create separate decorators for each interface. An alternative is to use a generic interface for your services and a generic decorator. For example:

    public interface ICommandService
    {
       Task Execute(TCommand command);
    }
    
    public class LoggingCommandService : ICommandService
    {
      public LoggingCommandService(ICommandService command, ILogger logger)
      {
        ...
      }
    
      public async Task Execute(TCommand command)
      {
        // log
        await this.command.Execute(command);
        // log
      }
    }
    

提交回复
热议问题