protected async override Task <List <GeneratedObserver> > HandleCore(ObserverGenerateCommand command)
        {
            List <Observer>          dbObservers        = new List <Observer>();
            List <GeneratedObserver> generatedObservers = new List <GeneratedObserver>();


            for (int i = 0; i < command.NrObservers; ++i)
            {
                RandomObserverBuilder builder = new RandomObserverBuilder();
                dbObservers.Add(builder.build(command.IdNgo));
            }

            try
            {
                using (var tran = await _voteMonitorContext.Database.BeginTransactionAsync())
                {
                    int latestId = _voteMonitorContext.Observers
                                   .OrderByDescending(o => o.Id)
                                   .FirstOrDefault()
                                   .Id;

                    dbObservers = dbObservers
                                  .Select(o => { o.Id = ++latestId; return(o); })
                                  .ToList();

                    _voteMonitorContext.Observers.AddRange(dbObservers.ToArray());
                    var result = await _voteMonitorContext.SaveChangesAsync();

                    tran.Commit();

                    return(dbObservers
                           .Select(o => _mapper.Map <GeneratedObserver>(o))
                           .ToList());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error during generation of random observers", ex, ex.Message);
            }

            return(await Task.FromResult(generatedObservers));
        }
        public async Task <List <GeneratedObserver> > Handle(ObserverGenerateCommand request, CancellationToken cancellationToken)
        {
            List <Entities.Observer> dbObservers        = new List <Entities.Observer>();
            List <GeneratedObserver> generatedObservers = new List <GeneratedObserver>();


            for (int i = 0; i < request.NrObservers; ++i)
            {
                dbObservers.Add(RandomObserverBuilder.Instance(_hashService).Build(request.IdNgo));
            }

            try
            {
                using (var tran = await _voteMonitorContext.Database.BeginTransactionAsync(cancellationToken))
                {
                    int latestId = _voteMonitorContext.Observers
                                   .OrderByDescending(o => o.Id)
                                   .First()
                                   .Id;

                    dbObservers = dbObservers
                                  .Select(o => { o.Id = ++latestId; return(o); })
                                  .ToList();

                    _voteMonitorContext.Observers.AddRange(dbObservers.ToArray());
                    await _voteMonitorContext.SaveChangesAsync(cancellationToken);

                    tran.Commit();

                    return(dbObservers
                           .Select(o => _mapper.Map <GeneratedObserver>(o))
                           .ToList());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Error during generation of random observers", ex, ex.Message);
            }

            return(await Task.FromResult(generatedObservers));
        }