daily pastebin goal
50%
SHARE
TWEET

Get result from command handler

a guest Nov 9th, 2018 99 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1.  public interface IHasResultCommand
  2.     {
  3.         Action SetResult { get; set; }
  4.     }
  5.  
  6.  
  7.     public class AddCategoryCommand : IHasResultCommand
  8.     {
  9.         public int Id { get; set; }
  10.         public string Name { get; set; }
  11.         public string Description { get; set; }      
  12.         public Action SetResult { get; set; } = () => { };
  13.     }
  14.  
  15.     public class Category
  16.     {
  17.         public int Id { get; set; }
  18.         public string Name { get; set; }
  19.     }
  20.  
  21.     public class AddCategoryCommandHandler : ICommandHandler<AddCategoryCommand>
  22.     {
  23.  
  24.         public void Handle(AddCategoryCommand command)
  25.         {
  26.             var cat = new Category
  27.             {
  28.                 Name = command.Name
  29.             };
  30.  
  31.             //ctx.Categories.Add(cat)
  32.  
  33.             command.SetResult = () =>
  34.             {
  35.                 command.Id = cat.Id;
  36.             };
  37.         }
  38.     }
  39.  
  40.  
  41.     public class HasResultCommandHandlerDecorator<TCommand> : ICommandHandler<TCommand>
  42.     {
  43.         ICommandHandler<TCommand> _handler;
  44.  
  45.         public HasResultCommandHandlerDecorator(ICommandHandler<TCommand> handler)
  46.         {
  47.             _handler = handler;
  48.         }
  49.  
  50.         public void Handle(TCommand command)
  51.         {
  52.             _handler.Handle(command);
  53.  
  54.             if(command is IHasResultCommand cmd)
  55.             {
  56.                 cmd.SetResult();
  57.             }
  58.  
  59.         }
  60.     }
  61.  
  62.  
  63. container.RegisterDecorator(
  64.     typeof(ICommandHandler<>),
  65.     typeof(TransactionCommandHandlerDecorator<>));
  66.  
  67. container.RegisterDecorator(
  68.     typeof(ICommandHandler<>),
  69.     typeof(HasResultCommandHandlerDecorator<>));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top