Heitor Giacomini
Heitor Giacomini

Reputation: 484

Correct way to use DomainManager Class on insert and update entity (Aggregate Root)

This question it's about ABP framework implementation of DDD,in the Domain layer there a class named "Entity"Manager, this class is a factory for the domain entity and have the method CreateAsync for entity instantiation. I want to reuse this method (or a better approach) to update my entity without redoing the same logic on Application layer. This is the method on AporteManager.

public async Task<Aporte> CreateAsync(
            AporteValidation valid

        )
        {

            var icontaBancaria = await _contaBancariaRepository.WithDetailsAsync(
                x => x.Banco, x => x.FonteDeRecurso, x => x.CodigoDeAplicacao, x => x.Fundo, x => x.FonteDeRecursoSICONFI);
            var contaBancaria = icontaBancaria.Where(x => x.Id == valid.ContaBancariaId).FirstOrDefault();

            valid.FonteDeRecursoId = contaBancaria.FonteDeRecurso?.Id;
            valid.CodigoDeAplicacaoId = contaBancaria.CodigoDeAplicacao?.Id;

            return new Aporte(
                valid
            );
        }

And this is my update method on Application:

[Authorize(ContabilidadePermissions.Aporte.Edit)]
        public async Task UpdateAsync(int id, CreateUpdateAporteDto input)
        {
            try
            {

                var aporte = await _aporteRepository.GetAsync(id);
                if (aporte is not null)
                {
                    var validation = ObjectMapper.Map<CreateUpdateAporteDto, AporteValidation>(input);
                    var aporteValidado = await _aporteManager.CreateAsync(validation);
                    aporte = ObjectMapper.Map(aporteValidado, aporte);

                    await _aporteRepository.UpdateAsync(aporte);

                    //aporte = ObjectMapper.Map(input, aporte);
                    //await _aporteRepository.UpdateAsync(aporte);
                }
                else
                {
                    throw new EntidadeNaoExisteParaSerAtualizadaException();
                }
            }
            catch (Exception ex)
            {
                throw new RegistroJaExisteGenericoException("Aporte", "Código");
            }
        }

Although UpdateAsync is not working, I'm not just looking to a solution, but a correct, maintainable and simple DDD solution. Here is a sample of the project: https://github.com/heitorgiacominibrasil/DomainManager

Upvotes: 0

Views: 473

Answers (1)

Related Questions