Beispiel #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);
        }
Beispiel #2
0
        public SpecificationScope <T> Build <T>(Specification <T> specification, IScopeBuilderContext context)
        {
            ThrowHelper.NullArgument(specification, nameof(specification));
            ThrowHelper.NullArgument(context, nameof(context));

            var specificationApi = (SpecificationApi <T>)specification(new SpecificationApi <T>());

            var commandScopes = new List <ICommandScope <T> >();

            var presenceInfo = ParsePresenceInfo(specificationApi.Commands, context, out var index);

            for (var i = index; i < specificationApi.Commands.Count; ++i)
            {
                var scopeCommand = (IScopeCommand)specificationApi.Commands[i];

                var scopeBuilder = scopeCommand.GetScopeBuilder();

                while (++i < specificationApi.Commands.Count)
                {
                    if (!scopeBuilder.TryAdd(specificationApi.Commands[i]))
                    {
                        break;
                    }
                }

                --i;

                var commandScope = (ICommandScope <T>)scopeBuilder.Build(context);

                commandScopes.Add(commandScope);
            }

            return(new SpecificationScope <T>
            {
                Presence = presenceInfo.Presence,
                ForbiddenErrorId = presenceInfo.ForbiddenErrorId,
                RequiredErrorId = presenceInfo.RequiredErrorId,
                CommandScopes = commandScopes
            });
        }
        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);
        }
Beispiel #4
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));
            }
        }