Example #1
0
        public User Add(AddUser user)
        {
            var agg = UserEntityBasedAggregationRoot.CreateFrom(Name.From(user.Name), Email.From(user.Mail));

            if (!agg.ValidationResults.IsValid)
            {
                throw new ArgumentException("One or more parameters informed to create a user are not valid.");
            }

            return(agg.GetChange());
        }
Example #2
0
        public User Add(AddUser user)
        {
            var agg = UserEntityBasedAggregationRoot.CreateFrom(Name.From(user.Name), Email.From(user.Mail));

            if (!_specification.IsSatisfiedBy(agg))
            {
                throw new ArgumentException(agg.Failures[0].Message);
            }

            return(agg.GetChange());
        }
Example #3
0
        protected override Task <CommandResult <Guid> > ExecuteCommand(AddUserCommand command, CancellationToken cancellationToken)
        {
            var agg = UserEntityBasedAggregationRoot.CreateFrom(command.Name, command.Mail);

            var isSucceed = agg.IsValid;
            var okId      = Guid.Empty;

            if (agg.IsValid)
            {
                agg.GetEvents().ToImmutableList()
                .ForEach(ev => Publisher.Publish(ev));

                okId = agg.GetChange().Identity.Value;
            }

            return(Task.FromResult(new CommandResult <Guid>(isSucceed, okId, agg.Failures)));
        }
Example #4
0
        protected override async Task <CommandResult <Guid> > ExecuteCommand(AddUserCommand command, CancellationToken cancellationToken)
        {
            var agg = UserEntityBasedAggregationRoot.CreateFrom(command.Name, command.Mail);

            var isSucceed = agg.ValidationResults.IsValid;
            var okId      = Guid.Empty;

            if (agg.ValidationResults.IsValid)
            {
                _sessionDb.Repository.Add(agg.GetChange());
                await _sessionDb.SaveChangesAsync(cancellationToken);

                agg.GetEvents().ToImmutableList()
                .ForEach(async ev => await Publisher.Publish(ev, cancellationToken));

                okId = agg.GetChange().Id.Value;
            }

            return(await Task.FromResult(new CommandResult <Guid>(isSucceed, okId, agg.ValidationResults.Errors.ToImmutableList()))
                   .ConfigureAwait(false));
        }