예제 #1
0
        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>));
        }
예제 #2
0
        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()));
        }
예제 #3
0
        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));
        }
예제 #4
0
 public Task <BatchResponse <TResponse> > Handle(BatchOf <TPayload, TResponse> batchOf)
 {
     return(HandleBatch(batchOf, payload => _mediator.SendAsync(payload)));
 }