Ejemplo n.º 1
0
        /// <summary>
        /// Appends the specified action to the action sequence that is building now.
        /// </summary>
        /// <param name="actionType">Type of the action.</param>
        /// <param name="action">The action to append.</param>
        /// <exception cref="ArgumentOutOfRangeException"><c>actionType</c> is out of range.</exception>
        /// <exception cref="InvalidOperationException">Invalid <c>Context.DependencyRootType</c>.</exception>
        protected void AddAction(UpgradeActionType actionType, NodeAction action)
        {
            action.Difference = Context.Difference;
            var dependencyRootType = Context.DependencyRootType;

            if (dependencyRootType == null || actionType == UpgradeActionType.Regular)
            {
                action.Execute(CurrentModel);
                Context.Actions.Add(action);
            }
            else if (actionType == UpgradeActionType.Rename)
            {
                var parentDifference = Context.Difference.Parent;
                EnumerableUtils.Unfold(Context, c => c.Parent)
                .Where(c => c.Parent.Difference == parentDifference)
                .Take(1)
                .ForEach(c => c.Renames.Add(action));
            }
            else
            {
                foreach (var ctx in EnumerableUtils.Unfold(Context, c => c.Parent))
                {
                    var difference = ctx.Difference;
                    var any        = difference.Target ?? difference.Source;
                    var type       = any == null ? WellKnownTypes.Object : any.GetType();
                    if (type == dependencyRootType)
                    {
                        switch (actionType)
                        {
                        case UpgradeActionType.PreCondition:
                            action.Execute(CurrentModel);
                            ctx.PreConditions.Add(action);
                            break;

                        case UpgradeActionType.PostCondition:
                            ctx.PostConditions.Add(action);
                            break;

                        default:
                            throw new ArgumentOutOfRangeException(nameof(actionType));
                        }

                        return;
                    }
                }

                throw new InvalidOperationException(string.Format(
                                                        Strings.ExDifferenceRelatedToXTypeIsNotFoundOnTheUpgradeContextStack,
                                                        dependencyRootType.GetShortName()));
            }
        }