Exemplo n.º 1
0
        public ICommandScope Build(IScopeBuilderContext context)
        {
            ThrowHelper.NullArgument(context, nameof(context));

            var ruleCommandScope = new RuleCommandScope <T>
            {
                IsValid = _ruleCommand.Predicate
            };

            ruleCommandScope.Path = _path;
            ruleCommandScope.ExecutionCondition = _executionCondition;
            ruleCommandScope.ErrorMode          = _errorsBuilder.Mode;

            if (_errorsBuilder.IsEmpty)
            {
                ruleCommandScope.ErrorId = context.DefaultErrorId;
            }
            else
            {
                var error = _errorsBuilder.Build();

                ruleCommandScope.ErrorId = context.RegisterError(error);
            }

            return(ruleCommandScope);
        }
Exemplo n.º 2
0
        public ICommandScope Build(IScopeBuilderContext context)
        {
            ThrowHelper.NullArgument(context, nameof(context));

            var core = _coreBuilder(_command, context);

            ThrowHelper.NullArgument(core, nameof(core));

            if (_errorsBuilder != null)
            {
                var error = _errorsBuilder.Build();

                core.ErrorMode = _errorsBuilder.Mode;

                if (!_errorsBuilder.IsEmpty)
                {
                    core.ErrorId = context.RegisterError(error);
                }
                else if (_errorsBuilder.Mode == ErrorMode.Override)
                {
                    core.ErrorId = context.DefaultErrorId;
                }
            }

            if (_path != null)
            {
                core.Path = _path;
            }

            if (_executionCondition != null)
            {
                core.ExecutionCondition = _executionCondition;
            }

            return(core);
        }
Exemplo n.º 3
0
        private static PresenceInfo ParsePresenceInfo(IReadOnlyList <ICommand> commands, IScopeBuilderContext context, out int index)
        {
            index = 0;

            if (commands.Count == 0 || !IsPresenceCommand(commands[0]))
            {
                return(new PresenceInfo
                {
                    Presence = Presence.Required,
                    RequiredErrorId = context.RequiredErrorId,
                    ForbiddenErrorId = context.ForbiddenErrorId
                });
            }

            if (commands[0] is OptionalCommand)
            {
                index = 1;

                return(new PresenceInfo
                {
                    Presence = Presence.Optional,
                    RequiredErrorId = context.RequiredErrorId,
                    ForbiddenErrorId = context.ForbiddenErrorId
                });
            }

            if (commands[0] is RequiredCommand)
            {
                var requiredErrorId = GetErrorId(context.RequiredErrorId, MessageKey.Global.Required, out index);

                return(new PresenceInfo
                {
                    Presence = Presence.Required,
                    RequiredErrorId = requiredErrorId,
                    ForbiddenErrorId = context.ForbiddenErrorId
                });
            }

            var forbiddenErrorId = GetErrorId(context.ForbiddenErrorId, MessageKey.Global.Forbidden, out index);

            return(new PresenceInfo
            {
                Presence = Presence.Forbidden,
                RequiredErrorId = context.RequiredErrorId,
                ForbiddenErrorId = forbiddenErrorId
            });

            int GetErrorId(int defaultErrorId, string baseMessageKey, out int i)
            {
                var errorsBuilder = baseMessageKey != null
                    ? new ErrorBuilder(baseMessageKey)
                    : new ErrorBuilder();

                var somethingAdded = false;

                for (i = 1; i < commands.Count; ++i)
                {
                    if (!errorsBuilder.TryAdd(commands[i]))
                    {
                        break;
                    }

                    somethingAdded = true;
                }

                if (!somethingAdded || errorsBuilder.IsEmpty)
                {
                    return(defaultErrorId);
                }

                var error = errorsBuilder.Build();

                return(context.RegisterError(error));
            }
        }