Esempio n. 1
0
        protected override ExecutionResult ExecuteCommand(AddProjectCommand command)
        {
            var agg = ProjectAggregationRoot.CreateFrom(command.Description, EntityId.GetNext());

            _dbSession.Repository.Add(agg.GetChange());
            _dbSession.SaveChanges();
            Publisher.Publish(agg.GetEvents());

            return(new ExecutionResult(true));
        }
        protected override ExecutionResult ExecuteCommand(UpdateProjectCommand command)
        {
            var project   = _dbSession.Repository.Get(EntityId.From(command.Id));
            var agg       = ProjectAggregationRoot.ReconstructFrom(project);
            var isSucceed = false;

            if (agg.ValidationResults.IsValid)
            {
                agg.UpdateDetail(command.ToProjectDetail());

                _dbSession.Repository.Add(agg.GetChange());
                _dbSession.SaveChanges();
                agg.GetEvents().ToImmutableList().ForEach(ev => Publisher.Publish(ev));
                isSucceed = true;
            }

            return(new ExecutionResult(isSucceed, agg.ValidationResults.Errors.ToImmutableList()));
        }
        protected override CommandResult <Guid> ExecuteCommand(AddProjectCommand command)
        {
            var isSucceed     = false;
            var aggregationId = Guid.Empty;

            _logger.LogDebug("Criada agregação a partir do comando {CommandName} com valores {Valores}",
                             nameof(command), command);

            var client = _dbUserSession.Repository.Get(EntityId.From(command.ClientId));

            var agg = ProjectAggregationRoot.CreateFrom(
                ProjectName.From(command.Name),
                ProjectCode.From(command.Code),
                Money.From(command.Budget),
                DateAndTime.From(command.StartDate),
                client.Id);

            if (agg.ValidationResults.IsValid)
            {
                // _logger.LogInformation($"Agregação Project valida id gerado", agg.GetChange().Id);

                using (_logger.BeginScope("Persistencia"))
                {
                    _dbSession.Repository.Add(agg.GetChange());
                    _dbSession.SaveChanges();
                }

                // _logger.LogInformation($"Project persistido ID: {agg.GetChange().Id}");
                using (_logger.BeginScope("Publicacão de Eventos"))
                {
                    agg.GetEvents().ToImmutableList().ForEach(ev => Publisher.Publish(ev));
                }

                isSucceed     = true;
                aggregationId = agg.GetChange().Id.Value;
            }

            return(new CommandResult <Guid>(isSucceed, aggregationId, agg.ValidationResults.Errors.ToImmutableList()));
        }