Example #1
0
        public async Task <int> BulkImportAsync(TImportDto fileImportDto)
        {
            _logger.LogInformation($"Processing {typeof(TImportDto).Name}.");

            var dataDtos = await _fileReader.ValidateAndParseFileAsync(fileImportDto);

            if (dataDtos == null || !dataDtos.Any())
            {
                _logger.LogInformation("No Data Imported.");

                return(0);
            }

            var comparer = GetEqualityComparer();

            var entities = _mapper.Map <IList <TEntity> >(dataDtos).Distinct(comparer).ToList();

            _dataProcessor.PreProcessingHandler(entities);

            _logger.LogInformation($"Saving {entities.Count} {typeof(TImportDto).Name}.");

            var numberOfRecordsAffected = 0;

            if (entities.Count < BatchingThreshold)
            {
                await _repository.BulkInsertAsync(entities);

                numberOfRecordsAffected = await _repository.MergeFromStagingAsync();
            }
            else
            {
                for (var i = 0; i < entities.Count; i += BatchSize)
                {
                    var batch = entities.Skip(i).Take(BatchSize);

                    await _repository.BulkInsertAsync(batch.ToList());

                    var recordsAffected = await _repository.MergeFromStagingAsync(false);

                    numberOfRecordsAffected += recordsAffected;
                }
            }

            _dataProcessor.PostProcessingHandler(entities);

            return(numberOfRecordsAffected);
        }