Advertisement
Guest User

Alternative CommandProcessor

a guest
Sep 26th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. public interface ICommand
  2.     {
  3.         int ResultId { get; }
  4.     }
  5.  
  6. public class CommandProcessor : ICommandProcessor
  7.     {
  8.         private readonly IKernel _kernel;
  9.         private readonly IDatabase _db;
  10.         private readonly Queue<CommandData> _commandSet = new Queue<CommandData>();
  11.  
  12.         public CommandProcessor(IKernel kernel, IDatabase database)
  13.         {
  14.             _kernel = kernel;
  15.             _db = database;
  16.         }
  17.  
  18.         public async Task Process<T>(T cmd) where T : ICommand
  19.         {
  20.             using (var connection = await _db.CreateConnection())
  21.             {
  22.                 var transaction = connection.BeginTransaction();
  23.  
  24.                 var handler = _kernel.Get<IHandleCommand<T>>();
  25.                 await handler.Handle(cmd, connection, transaction);
  26.  
  27.                 transaction.Commit();
  28.             }
  29.         }      
  30.  
  31.         public void Set<TCommand>(TCommand cmd) where TCommand : ICommand
  32.         {
  33.             var commandData = new CommandData();            
  34.             var handler = _kernel.Get<IHandleCommand<TCommand>>();
  35.             commandData.Process = (conn, tran) => handler.Handle(cmd, conn, tran);            
  36.             _commandSet.Enqueue(commandData);
  37.         }
  38.  
  39.         public async Task ProcessMultiple()
  40.         {
  41.             if (_commandSet.Count == 0)
  42.                 throw new Exception("Set commands");
  43.  
  44.             using (var connection = await _db.CreateConnection())
  45.             {
  46.                 var transaction = connection.BeginTransaction();
  47.                 while (_commandSet.Count > 0)
  48.                 {
  49.                     var commandData = _commandSet.Dequeue();
  50.                     await commandData.Process(connection, transaction);                    
  51.                 }
  52.                 transaction.Commit();
  53.             }
  54.         }
  55.  
  56.         private class CommandData
  57.         {            
  58.             public Func<SqlConnection, SqlTransaction, Task> Process { get; set; }            
  59.         }
  60.     }
  61.  
  62.    
  63. public class TicketService
  64. {
  65.     public async Task AddTicket(){
  66.         //Some validationt to see if multipel add commands needed
  67.         var courierAddCommand = new CourierAddCommand();
  68.         _commandProcess.Set(courierAddCommand);
  69.  
  70.         await _commandProcessor.ProcessMultiple();
  71.  
  72.         await _commandProcessor.Process(new TicketAddCommand{CourierId = courierAddCommand.Result});
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement