BatchAsync <TPayload, TResponse>(params TPayload[] payload) where TPayload : Request.WithResponse <TResponse> where TResponse : class { object batch; if (!_batches.TryGetValue(typeof(TPayload), out batch)) { batch = new BatchOf <TPayload, TResponse>(); _batches.Add(typeof(TPayload), batch); } var payloads = (BatchOf <TPayload, TResponse>)batch; if (payloads.AddPayload(payload) >= BatchSize) { _batches.Remove(typeof(TPayload)); return(SendAsync(payloads)); } return(Task.FromResult(null as BatchResponse <TResponse>)); }
protected virtual async Task <BatchResponse <TResponse> > HandleBatch( BatchOf <TPayload, TResponse> batchOf, Func <TPayload, Task <TResponse> > sendPayload) { var responses = new List <TResponse>(); if (batchOf.Batch != null) { foreach (var payload in batchOf.Batch) { var response = await sendPayload(payload); if (response != null) { responses.Add(response); } } } return(new BatchResponse <TResponse>(responses.ToArray())); }
public BatchResponse <TResponse> Batch <TPayload, TResponse>(params TPayload[] payload) where TPayload : Request.WithResponse <TResponse> where TResponse : class { object batch; if (!_batches.TryGetValue(typeof(TPayload), out batch)) { batch = new BatchOf <TPayload, TResponse>(); _batches.Add(typeof(TPayload), batch); } var payloads = (BatchOf <TPayload, TResponse>)batch; if (payloads.AddPayload(payload) < BatchSize) { return(null); } _batches.Remove(typeof(TPayload)); return(Send(payloads)); }
public Task <BatchResponse <TResponse> > Handle(BatchOf <TPayload, TResponse> batchOf) { return(HandleBatch(batchOf, payload => _mediator.SendAsync(payload))); }