Ejemplo n.º 1
0
        public async Task <Member> CreateMemberAsync(MemberApiModel memberApi)
        {
            try
            {
                var validationResult = await _validator.ValidateAsync(memberApi);

                if (!validationResult.Errors.Any(x => x.Severity == Severity.Error))
                {
                    var existMemberWithEmail = await _memberRepository.GetByEmailAsync(memberApi.Email);

                    if (existMemberWithEmail != null)
                    {
                        throw new MemberDomainException($"Ya existe un usuario con el email: '{memberApi.Email}'", "ValidationErrors");
                    }

                    Guid id     = Guid.NewGuid();
                    var  member = Member.Create(id,
                                                memberApi?.FirstName,
                                                memberApi?.LastName,
                                                memberApi.Email,
                                                memberApi.Password,
                                                memberApi.Country,
                                                memberApi.PhoneNumber,
                                                memberApi.PostCode);

                    //Se publican los eventos de dominio en caso por ejemplo, que se quiera notificar al usuario mediante correo o sms añadiremos, un manejador que haga el envío de la notificación
                    //si quisieramos que este fuese completamente asíncrono, en dicho manejador añadiriamos al bus de servicio o a la cola el envío de dicho mensaje y otro microservicio estaría a
                    //la escucha para realizar el envío
                    await _mediator?.DispatchDomainEventsAsync(member);

                    await _memberRepository.CreateAsync(member);

                    return(await _memberRepository.GetAsync(id));
                }
                else
                {
                    throw new MemberDomainException(string.Join(";", validationResult.Errors), "ValidationErrors");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error al crear el usuario: {ex.Message}", ex);
                throw;
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> SaveEntitiesAsync(CancellationToken cancellationToken = default)
        {
            // Dispatch Domain Events collection.
            // Choices:
            // A) Right BEFORE committing data (EF SaveChanges) into the DB will make a single transaction including
            // side effects from the domain event handlers which are using the same DbContext with "InstancePerLifetimeScope" or "scoped" lifetime
            // B) Right AFTER committing data (EF SaveChanges) into the DB will make multiple transactions.
            // You will need to handle eventual consistency and compensatory actions in case of failures in any of the Handlers.
            await _mediator?.DispatchDomainEventsAsync(this);

            // After executing this line all the changes (from the Command Handler and Domain Event Handlers)
            // performed through the DbContext will be committed
            await base.SaveChangesAsync(cancellationToken);

            return(true);
        }
Ejemplo n.º 3
0
        public async Task <bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            await _mediator.DispatchDomainEventsAsync(this);

            return(true);
        }