Exemplo n.º 1
0
        protected static async Task ScheduleCommandAgainstEventSourcedAggregate(
            string targetId,
            string etag,
            DateTimeOffset?dueTime          = null,
            IPrecondition deliveryDependsOn = null)
        {
            var aggregateId = Guid.Parse(targetId);

            var repository = Configuration.Current.Repository <Order>();

            if (await repository.GetLatest(aggregateId) == null)
            {
                await repository.Save(new Order(new CreateOrder(Any.FullName())
                {
                    AggregateId = aggregateId
                }));
            }

            var command = new AddItem
            {
                ETag        = etag,
                ProductName = Any.Word(),
                Price       = 10m
            };

            var scheduledCommand = new ScheduledCommand <Order>(
                command,
                aggregateId,
                dueTime,
                deliveryDependsOn);

            var scheduler = Configuration.Current.CommandScheduler <Order>();

            await scheduler.Schedule(scheduledCommand);
        }
Exemplo n.º 2
0
        private void FromIfThrowToRequires(IPrecondition assertion, bool isGeneric)
        {
            // Convertion from if-throw precondition to Contract.Requires
            // contains following steps:
            // 1. Negate condition from the if statement (because if (s == null) throw ANE means that Contract.Requires(s != null))
            // 2. Create Contract.Requires expression (with all optional generic argument and optional message
            // 3. Add required using statements if necessary (for Contract class and Exception type)
            // 4. Replace if-throw statement with newly created contract statement

            var ifThrowAssertion = (IfThrowPrecondition)assertion;

            ICSharpExpression negatedExpression =
                CSharpExpressionUtil.CreateLogicallyNegatedExpression(ifThrowAssertion.IfStatement.Condition);

            Contract.Assert(negatedExpression != null);

            string predicateCheck = negatedExpression.GetText();

            ICSharpStatement newStatement = null;

            if (isGeneric)
            {
                newStatement = CreateGenericContractRequires(ifThrowAssertion.ExceptionTypeName, predicateCheck,
                                                             ifThrowAssertion.Message);
            }
            else
            {
                newStatement = CreateNonGenericContractRequires(predicateCheck, ifThrowAssertion.Message);
            }

            ReplaceStatements(ifThrowAssertion.CSharpStatement, newStatement);
        }
 public ConvertToContractRequiresFix(ValidationResult currentStatement, ValidatedContractBlock validatedContractBlock)
     : base(currentStatement, validatedContractBlock)
 {
     // TODO: refactoring required!! Not clear logic
     _precondition =
         ContractsEx.Assertions.ContractStatementFactory.TryCreatePrecondition(currentStatement.Statement);
 }
 public ConvertToContractRequiresFix(ValidationResult currentStatement, ValidatedContractBlock validatedContractBlock)
     : base(currentStatement, validatedContractBlock)
 {
     // TODO: refactoring required!! Not clear logic
     _precondition =
         ContractsEx.Assertions.ContractStatementFactory.TryCreatePrecondition(currentStatement.Statement);
 }
Exemplo n.º 5
0
 protected abstract IScheduledCommand <T> CreateScheduledCommand <T>(
     ICommand <T> command,
     Guid aggregateId,
     DateTimeOffset?dueTime          = null,
     IPrecondition deliveryDependsOn = null,
     IClock clock = null)
     where T : class, IEventSourced;
Exemplo n.º 6
0
 /// <summary>
 /// Creates a new package for an IPrecondition contract.
 /// </summary>
 public XPrecondition(IMetadataHost host, IPrecondition precondition, string inheritedFrom, string inheritedFromTypeName, DocTracker docTracker)
     : base(host, inheritedFrom, inheritedFromTypeName, docTracker)
 {
     Contract.Requires(precondition != null);
     Contract.Requires(docTracker != null);
     this.precondition = precondition;
     this.exception    = GetExceptionId(precondition);
 }
 protected override Task Schedule(
     string targetId,
     string etag,
     DateTimeOffset? dueTime = null,
     IPrecondition deliveryDependsOn = null) =>
         ScheduleCommandAgainstEventSourcedAggregate(targetId,
             etag,
             dueTime,
             deliveryDependsOn);
Exemplo n.º 8
0
 public void Visit(IPrecondition precondition)
 {
     Contract.Assert(precondition != null);
     Visit((IContractElement)precondition);
     if (precondition.ExceptionToThrow != null)
     {
         Visit(precondition.ExceptionToThrow);
     }
 }
Exemplo n.º 9
0
 protected override Task Schedule(
     string targetId,
     string etag,
     DateTimeOffset?dueTime          = null,
     IPrecondition deliveryDependsOn = null) =>
 ScheduleCommandAgainstNonEventSourcedAggregate(targetId,
                                                etag,
                                                dueTime,
                                                deliveryDependsOn);
Exemplo n.º 10
0
 /// <summary>
 ///     Traverses the pre condition.
 /// </summary>
 public void Traverse(IPrecondition precondition)
 {
     Contract.Requires(precondition != null);
     preorderVisitor.Visit(precondition);
     if (StopTraversal)
     {
         return;
     }
     TraverseChildren(precondition);
 }
Exemplo n.º 11
0
 public ScheduledCommand(
     ICommand <TTarget> command,
     Guid aggregateId,
     DateTimeOffset?dueTime             = null,
     IPrecondition deliveryPrecondition = null) :
     this(command,
          aggregateId.ToString(),
          dueTime,
          deliveryPrecondition)
 {
 }
Exemplo n.º 12
0
        public ITransitionBuilder <TState, TInput> WithPrecondition(
            ConnectorKey connectorKey,
            System.Action <IPreconditionBuilder> guard = null)
        {
            var preconditionBuilder = new PreconditionBuilder(connectorKey);

            guard?.Invoke(preconditionBuilder);

            Procondition = preconditionBuilder;

            return(this);
        }
Exemplo n.º 13
0
 /// <summary>
 ///     Traverses the children of the pre condition.
 /// </summary>
 public virtual void TraverseChildren(IPrecondition precondition)
 {
     Contract.Requires(precondition != null);
     TraverseChildren((IContractElement)precondition);
     if (StopTraversal)
     {
         return;
     }
     if (precondition.ExceptionToThrow != null)
     {
         Traverse(precondition.ExceptionToThrow);
     }
 }
Exemplo n.º 14
0
        private void FromRequiresToGenericRequires(IPrecondition assertion)
        {
            var requiresAssertion = (ContractRequires)assertion;

            Contract.Assert(!requiresAssertion.IsGenericRequires);

            var exceptionType = requiresAssertion.PotentialGenericVersionException();

            string predicate    = requiresAssertion.OriginalPredicateExpression.GetText();
            var    newStatement = CreateGenericContractRequires(exceptionType, predicate, requiresAssertion.Message);

            ReplaceStatements(requiresAssertion.CSharpStatement, newStatement);
        }
Exemplo n.º 15
0
        private void FromGenericRequiresToRequires(IPrecondition assertion)
        {
            var requiresAssertion = (ContractRequires)assertion;

            Contract.Assert(requiresAssertion.IsGenericRequires);

            string predicateCheck = requiresAssertion.OriginalPredicateExpression.GetText();
            var    newStatement   = CreateNonGenericContractRequires(predicateCheck, requiresAssertion.Message);

            ReplaceStatements(requiresAssertion.CSharpStatement, newStatement);

            RemoveSystemNamespaceUsingIfPossible();
        }
Exemplo n.º 16
0
 public static Task <IScheduledCommand <TTarget> > Schedule <TTarget>(
     ConstructorCommand <TTarget> command,
     DateTimeOffset?dueTime          = null,
     IPrecondition deliveryDependsOn = null,
     IClock clock = null)
     where TTarget : class =>
 Configuration
 .Current
 .CommandScheduler <TTarget>()
 .Schedule(
     command,
     dueTime,
     deliveryDependsOn,
     clock);
Exemplo n.º 17
0
 private ScheduledCommand(
     ICommand <TTarget> command,
     string targetId                    = null,
     Guid?aggregateId                   = null,
     DateTimeOffset?dueTime             = null,
     IPrecondition deliveryPrecondition = null) :
     this(command,
          targetId
          .IfNotNull()
          .Else(() => aggregateId?.ToString()),
          dueTime,
          deliveryPrecondition)
 {
 }
Exemplo n.º 18
0
 protected override IScheduledCommand <T> CreateScheduledCommand <T>(
     ICommand <T> command,
     Guid aggregateId,
     DateTimeOffset?dueTime          = null,
     IPrecondition deliveryDependsOn = null,
     IClock clock = null)
 {
     return(new ScheduledCommand <T>(command,
                                     aggregateId,
                                     dueTime,
                                     deliveryDependsOn)
     {
         Clock = clock
     });
 }
Exemplo n.º 19
0
 /// <summary>
 /// Schedules a constructor command on the specified scheduler.
 /// </summary>
 public static async Task <IScheduledCommand <TTarget> > Schedule <TTarget>(
     this ICommandScheduler <TTarget> scheduler,
     ConstructorCommand <TTarget> command,
     DateTimeOffset?dueTime          = null,
     IPrecondition deliveryDependsOn = null,
     IClock clock = null)
     where TTarget : class
 {
     return(await scheduler.Schedule(
                command : command,
                targetId : command.AggregateId.ToString(),
                dueTime : dueTime,
                deliveryDependsOn : deliveryDependsOn,
                clock : clock));
 }
        public PreconditionConverterExecutor(ICSharpStatement preconditionStatement, IPrecondition precondition,
            PreconditionType sourcePreconditionType,
            PreconditionType destinationPreconditionType)
            : base(preconditionStatement)
        {
            Contract.Requires(precondition != null);

            Contract.Requires(sourcePreconditionType != destinationPreconditionType);

            _precondition = precondition;
            _sourcePreconditionType = sourcePreconditionType;
            _destinationPreconditionType = destinationPreconditionType;

            InitializeConverters();
        }
Exemplo n.º 21
0
        public PreconditionConverterExecutor(ICSharpStatement preconditionStatement, IPrecondition precondition,
                                             PreconditionType sourcePreconditionType,
                                             PreconditionType destinationPreconditionType)
            : base(preconditionStatement)
        {
            Contract.Requires(precondition != null);

            Contract.Requires(sourcePreconditionType != destinationPreconditionType);

            _precondition                = precondition;
            _sourcePreconditionType      = sourcePreconditionType;
            _destinationPreconditionType = destinationPreconditionType;

            InitializeConverters();
        }
Exemplo n.º 22
0
 protected override IScheduledCommand <T> CreateScheduledCommand <T>(
     ICommand <T> command,
     Guid aggregateId,
     DateTimeOffset?dueTime          = null,
     IPrecondition deliveryDependsOn = null,
     IClock clock = null)
 {
     return(new CommandScheduled <T>
     {
         Command = command,
         AggregateId = aggregateId,
         DueTime = dueTime,
         DeliveryPrecondition = deliveryDependsOn,
         Clock = clock
     });
 }
Exemplo n.º 23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ScheduledCommand{TTarget}"/> class.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="targetId">The target identifier.</param>
        /// <param name="dueTime">The due time.</param>
        /// <param name="deliveryPrecondition">The delivery precondition.</param>
        /// <param name="clock">The clock.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        /// <exception cref="System.ArgumentException">Parameter targetId cannot be null, empty or whitespace.</exception>
        public ScheduledCommand(
            ICommand <TTarget> command,
            string targetId,
            DateTimeOffset?dueTime             = null,
            IPrecondition deliveryPrecondition = null,
            IClock clock = null)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (string.IsNullOrWhiteSpace(targetId))
            {
                throw new ArgumentException("Parameter targetId cannot be null, empty or whitespace.");
            }

            var constructorCommand = command as ConstructorCommand <TTarget>;

            if (constructorCommand != null)
            {
                if (typeof(IEventSourced).IsAssignableFrom(typeof(TTarget)))
                {
                    if (constructorCommand.AggregateId != Guid.Parse(targetId))
                    {
                        throw new ArgumentException($"ConstructorCommand.AggregateId ({constructorCommand.AggregateId}) does not match ScheduledCommand.AggregateId ({targetId})");
                    }
                }
                else
                {
                    if (constructorCommand.TargetId != targetId)
                    {
                        throw new ArgumentException($"ConstructorCommand.TargetId ({constructorCommand.TargetId}) does not match ScheduledCommand.TargetId ({targetId})");
                    }
                }
            }

            Command              = command;
            TargetId             = targetId;
            DueTime              = dueTime;
            DeliveryPrecondition = deliveryPrecondition;
            Clock = clock;

            this.EnsureCommandHasETag();
        }
Exemplo n.º 24
0
        /// <summary>
        /// Schedules a command on the specified scheduler.
        /// </summary>
        public static async Task <IScheduledCommand <TTarget> > Schedule <TCommand, TTarget>(
            this ICommandScheduler <TTarget> scheduler,
            string targetId,
            TCommand command,
            DateTimeOffset?dueTime          = null,
            IPrecondition deliveryDependsOn = null)
            where TCommand : ICommand <TTarget>
        {
            var scheduledCommand = new ScheduledCommand <TTarget>(
                command,
                targetId,
                dueTime,
                deliveryDependsOn);

            await scheduler.Schedule(scheduledCommand);

            return(scheduledCommand);
        }
Exemplo n.º 25
0
        public IStateBuilder <TState, TInput> WithPrecondition(ConnectorKey connectorKey, System.Action <IPreconditionBuilder> precondition = null)
        {
            if (connectorKey == null)
            {
                throw new ArgumentNullException(nameof(connectorKey));
            }
            if (string.IsNullOrWhiteSpace(connectorKey.Identifier))
            {
                throw new ArgumentException("ConnectorKey must have a valid value", nameof(connectorKey));
            }

            var preconditionBuilder = new PreconditionBuilder(connectorKey);

            precondition?.Invoke(preconditionBuilder);

            Precondition = preconditionBuilder;

            return(this);
        }
Exemplo n.º 26
0
 public Queue()
 {
     foreach (Type t in Resolver.Instance.GenericPreconditionToString.Keys)
     {
         ConstructorInfo ctor = t.GetConstructor(new Type[0]);
         if (ctor == null)
         {
             continue;
         }
         IPrecondition prec = (IPrecondition)ctor.Invoke(new object[0]);
         if (prec == null)
         {
             continue;
         }
         _genericPreconditions.Add(prec, new PreConditionState {
             Name = Resolver.Instance.GenericPreconditionToString[t], CanExecute = true, CheckTime = DateTime.Now
         });
     }
 }
Exemplo n.º 27
0
 private static string GetExceptionId(IPrecondition precondition)
 {
     Contract.Requires(precondition != null);
     if (precondition.ExceptionToThrow != null)
     {
         ITypeOf        asTypeOf = precondition.ExceptionToThrow as ITypeOf;
         ITypeReference unspecializedExceptionType;
         if (asTypeOf != null)
         {
             unspecializedExceptionType = MethodHelper.Unspecialize(asTypeOf.TypeToGet);
         }
         else//Legacy contracts package exceptions differently then regular contracts.
         {
             unspecializedExceptionType = MethodHelper.Unspecialize(precondition.ExceptionToThrow.Type);
         }
         return(TypeHelper.GetTypeName(unspecializedExceptionType, NameFormattingOptions.DocumentationId));
     }
     return(null);
 }
Exemplo n.º 28
0
        public ScheduledCommand(
            ICommand <TTarget> command,
            string targetId,
            DateTimeOffset?dueTime             = null,
            IPrecondition deliveryPrecondition = null)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }
            if (string.IsNullOrWhiteSpace(targetId))
            {
                throw new ArgumentException("Parameter targetId cannot be null, empty or whitespace.");
            }

            Command              = command;
            TargetId             = targetId;
            DueTime              = dueTime;
            DeliveryPrecondition = deliveryPrecondition;

            this.EnsureCommandHasETag();
        }
Exemplo n.º 29
0
        protected async Task ScheduleCommandAgainstNonEventSourcedAggregate(
            string targetId,
            string etag,
            DateTimeOffset?dueTime          = null,
            IPrecondition deliveryDependsOn = null)
        {
            var repository = Configuration.Current.Store <CommandTarget>();

            if (await repository.Get(targetId) == null)
            {
                await repository.Put(new CommandTarget(targetId));
            }

            var command = new ScheduledCommand <CommandTarget>(
                new TestCommand(etag),
                targetId,
                dueTime,
                deliveryDependsOn);

            var scheduler = Configuration.Current.CommandScheduler <CommandTarget>();

            await scheduler.Schedule(command);
        }
Exemplo n.º 30
0
 /// <summary>
 /// Visits the given precondition.
 /// </summary>
 public virtual void Visit(IPrecondition precondition)
 {
     this.Visit((IContractElement)precondition);
 }
Exemplo n.º 31
0
 public void Visit(IPrecondition precondition)
 {
     this.traverser.Traverse(precondition);
 }
Exemplo n.º 32
0
 /// <summary>
 /// Creates a new package for an IPrecondition contract.
 /// </summary>
 public XPrecondition(IMetadataHost host, IPrecondition precondition, string inheritedFrom, string inheritedFromTypeName, DocTracker docTracker)
   : base(host, inheritedFrom, inheritedFromTypeName, docTracker) {
   Contract.Requires(precondition != null);
   Contract.Requires(docTracker != null);
   this.precondition = precondition;
   this.exception = GetExceptionId(precondition);
 }
Exemplo n.º 33
0
 /// <summary>
 /// Rewrites the given pre condition.
 /// </summary>
 public virtual IPrecondition Rewrite(IPrecondition precondition)
 {
     var mutablePrecondition = precondition as Precondition;
       if (mutablePrecondition == null) return precondition;
       this.RewriteChildren(mutablePrecondition);
       return mutablePrecondition;
 }
        private void FromGenericRequiresToRequires(IPrecondition assertion)
        {
            var requiresAssertion = (ContractRequires)assertion;
            Contract.Assert(requiresAssertion.IsGenericRequires);

            string predicateCheck = requiresAssertion.OriginalPredicateExpression.GetText();
            var newStatement = CreateNonGenericContractRequires(predicateCheck, requiresAssertion.Message);
            
            ReplaceStatements(requiresAssertion.CSharpStatement, newStatement);

            RemoveSystemNamespaceUsingIfPossible();
        }
Exemplo n.º 35
0
    /// <summary>
    /// Makes a shallow copy of the given precondition.
    /// </summary>
    public virtual Precondition Copy(IPrecondition precondition) {
      Contract.Requires(precondition != null);
      Contract.Ensures(Contract.Result<Precondition>() != null);

      return new Precondition(precondition);
    }
Exemplo n.º 36
0
    /// <summary>
    /// Makes a deep copy of the given precondition.
    /// </summary>
    public Precondition Copy(IPrecondition precondition) {
      Contract.Requires(precondition != null);
      Contract.Ensures(Contract.Result<Precondition>() != null);

      var mutableCopy = this.shallowCopier.Copy(precondition);
      this.Copy((ContractElement)mutableCopy);
      if (mutableCopy.ExceptionToThrow != null)
        mutableCopy.ExceptionToThrow = this.Copy(mutableCopy.ExceptionToThrow);
      return mutableCopy;
    }
Exemplo n.º 37
0
 /// <summary>
 /// Creates a precondition that shares all of the information in <paramref name="precondition"/>.
 /// </summary>
 /// <param name="precondition"></param>
 public Precondition(IPrecondition precondition)
   : base(precondition) {
   this.alwaysCheckedAtRuntime = precondition.AlwaysCheckedAtRuntime;
   this.exceptionToThrow = precondition.ExceptionToThrow;
 }
 public PreconditionNot(IPrecondition precondition)
 {
     mPrecondition = precondition;
 }
Exemplo n.º 39
0
 public void Visit(IPrecondition precondition)
 {
     this.result = this.copier.Copy(precondition);
 }
Exemplo n.º 40
0
 /// <summary>
 /// Makes a shallow copy of the given precondition.
 /// </summary>
 public virtual Precondition Copy(IPrecondition precondition)
 {
     return new Precondition(precondition);
 }
Exemplo n.º 41
0
 /// <summary>
 /// Visits the specified precondition.
 /// </summary>
 /// <param name="precondition">The precondition.</param>
 public virtual IPrecondition Visit(IPrecondition precondition)
 {
     Precondition mutablePrecondition = precondition as Precondition;
       if (!this.copyOnlyIfNotAlreadyMutable || mutablePrecondition == null)
     mutablePrecondition = new Precondition(precondition);
       return this.Visit(mutablePrecondition);
 }
Exemplo n.º 42
0
 public void Visit(IPrecondition precondition)
 {
     Contract.Requires(precondition != null);
       throw new NotImplementedException();
 }
 public PreconditionAnd(IPrecondition leftPrecondition, IPrecondition rightPrecondition)
 {
     mLeftPrecondition  = leftPrecondition;
     mRightPrecondition = rightPrecondition;
 }
Exemplo n.º 44
0
 public static Precondition Clone(this IPrecondition precondition) =>
 new Precondition
 {
     ConnectorKey  = precondition.ConnectorKey,
     Configuration = new Dictionary <string, string>((IDictionary <string, string>)precondition.Settings)
 };
Exemplo n.º 45
0
 //^ ensures this.path.Count == old(this.path.Count);
 /// <summary>
 /// Traverses the given pre condition.
 /// </summary>
 public virtual void Visit(IPrecondition precondition)
 {
     if (this.stopTraversal) return;
       //^ int oldCount = this.path.Count;
       this.path.Push(precondition);
       this.Visit(precondition.Condition);
       if (precondition.Description != null)
     this.Visit(precondition.Description);
       if (precondition.ExceptionToThrow != null)
     this.Visit(precondition.ExceptionToThrow);
       //^ assume this.path.Count == oldCount+1; //True because all of the virtual methods of this class promise not decrease this.path.Count.
       this.path.Pop();
 }
 public PreconditionNotMetException(IPrecondition precondition)
     : base("Precondition was not met: " + precondition)
 {
 }
Exemplo n.º 47
0
 /// <summary>
 /// Visits the given precondition.
 /// </summary>
 public virtual void Visit(IPrecondition precondition)
 {
 }
        private void FromRequiresToGenericRequires(IPrecondition assertion)
        {
            var requiresAssertion = (ContractRequires) assertion;
            Contract.Assert(!requiresAssertion.IsGenericRequires);

            var exceptionType = requiresAssertion.PotentialGenericVersionException();
            
            string predicate = requiresAssertion.OriginalPredicateExpression.GetText();
            var newStatement = CreateGenericContractRequires(exceptionType, predicate, requiresAssertion.Message);

            ReplaceStatements(requiresAssertion.CSharpStatement, newStatement);
        }
Exemplo n.º 49
0
 /// <summary>
 /// Traverses the pre condition.
 /// </summary>
 public void Traverse(IPrecondition precondition)
 {
     Contract.Requires(precondition != null);
       if (this.preorderVisitor != null) this.preorderVisitor.Visit(precondition);
       if (this.StopTraversal) return;
       this.TraverseChildren(precondition);
       if (this.StopTraversal) return;
       if (this.postorderVisitor != null) this.postorderVisitor.Visit(precondition);
 }
        private void FromIfThrowToRequires(IPrecondition assertion, bool isGeneric)
        {
            // Convertion from if-throw precondition to Contract.Requires
            // contains following steps:
            // 1. Negate condition from the if statement (because if (s == null) throw ANE means that Contract.Requires(s != null))
            // 2. Create Contract.Requires expression (with all optional generic argument and optional message
            // 3. Add required using statements if necessary (for Contract class and Exception type)
            // 4. Replace if-throw statement with newly created contract statement

            var ifThrowAssertion = (IfThrowPrecondition) assertion;

            ICSharpExpression negatedExpression = 
                CSharpExpressionUtil.CreateLogicallyNegatedExpression(ifThrowAssertion.IfStatement.Condition);
            
            Contract.Assert(negatedExpression != null);

            string predicateCheck = negatedExpression.GetText();

            ICSharpStatement newStatement = null;
            if (isGeneric)
            {
                newStatement = CreateGenericContractRequires(ifThrowAssertion.ExceptionTypeName, predicateCheck,
                    ifThrowAssertion.Message);
            }
            else
            {
                newStatement = CreateNonGenericContractRequires(predicateCheck, ifThrowAssertion.Message);
            }

            ReplaceStatements(ifThrowAssertion.CSharpStatement, newStatement);
        }
Exemplo n.º 51
0
 /// <summary>
 /// Traverses the children of the pre condition.
 /// </summary>
 public virtual void TraverseChildren(IPrecondition precondition)
 {
     Contract.Requires(precondition != null);
       this.TraverseChildren((IContractElement)precondition);
       if (this.StopTraversal) return;
       if (precondition.ExceptionToThrow != null)
     this.Traverse(precondition.ExceptionToThrow);
 }
Exemplo n.º 52
0
 /// <summary>
 /// 设置节点的前提条件(外在前提)
 /// </summary>
 /// <param 前提条件="precondition"></param>
 public NodeBase SetPrecondition(IPrecondition precondition)
 {
     mPrecondition = precondition;
     return(this);
 }
        protected async Task ScheduleCommandAgainstNonEventSourcedAggregate(
            string targetId,
            string etag,
            DateTimeOffset? dueTime = null,
            IPrecondition deliveryDependsOn = null)
        {
            var repository = Configuration.Current.Store<CommandTarget>();

            if (await repository.Get(targetId) == null)
            {
                await repository.Put(new CommandTarget(targetId));
            }

            var command = new ScheduledCommand<CommandTarget>(
                new TestCommand(etag),
                targetId,
                dueTime,
                deliveryDependsOn);

            var scheduler = Configuration.Current.CommandScheduler<CommandTarget>();

            await scheduler.Schedule(command);
        }
        protected static async Task ScheduleCommandAgainstEventSourcedAggregate(
            string targetId,
            string etag,
            DateTimeOffset? dueTime = null,
            IPrecondition deliveryDependsOn = null)
        {
            var aggregateId = Guid.Parse(targetId);

            var repository = Configuration.Current.Repository<Order>();

            if (await repository.GetLatest(aggregateId) == null)
            {
                await repository.Save(new Order(new CreateOrder(Any.FullName())
                {
                    AggregateId = aggregateId
                }));
            }

            var command = new AddItem
            {
                ETag = etag,
                ProductName = Any.Word(),
                Price = 10m
            };

            var scheduledCommand = new ScheduledCommand<Order>(
                command,
                aggregateId,
                dueTime,
                deliveryDependsOn);

            var scheduler = Configuration.Current.CommandScheduler<Order>();

            await scheduler.Schedule(scheduledCommand);
        }
Exemplo n.º 55
0
 /// <summary>
 /// Get the mutable copy of a precondition.
 /// </summary>
 /// <param name="precondition"></param>
 /// <returns></returns>
 public virtual Precondition GetMutableCopy(IPrecondition precondition)
 {
     object cachedValue;
       if (this.cache.TryGetValue(precondition, out cachedValue))
     return (Precondition)cachedValue;
       var result = new Precondition(precondition);
       // Probably not necessary, no two postconditions are shared.
       this.cache.Add(precondition, result);
       this.cache.Add(result, result);
       return result;
 }
Exemplo n.º 56
0
 /// <summary>
 /// Returns a deep copy of the <param name="precondition"/>.
 /// </summary>
 public virtual IPrecondition Substitute(IPrecondition precondition)
 {
     this.coneAlreadyFixed = true;
       return this.DeepCopy(new Precondition(precondition));
 }
Exemplo n.º 57
0
 /// <summary>
 /// Makes a deep copy of the given precondition.
 /// </summary>
 public Precondition Copy(IPrecondition precondition)
 {
     var mutableCopy = this.shallowCopier.Copy(precondition);
       this.Copy((ContractElement)mutableCopy);
       if (mutableCopy.ExceptionToThrow != null)
     mutableCopy.ExceptionToThrow = this.Copy(mutableCopy.ExceptionToThrow);
       return mutableCopy;
 }
Exemplo n.º 58
0
 private static string GetExceptionId(IPrecondition precondition) {
   Contract.Requires(precondition != null);
   if (precondition.ExceptionToThrow != null) {
     ITypeOf asTypeOf = precondition.ExceptionToThrow as ITypeOf;
     ITypeReference unspecializedExceptionType;        
     if (asTypeOf != null) 
       unspecializedExceptionType = MethodHelper.Unspecialize(asTypeOf.TypeToGet);
     else//Legacy contracts package exceptions differently then regular contracts.
       unspecializedExceptionType = MethodHelper.Unspecialize(precondition.ExceptionToThrow.Type);
     return TypeHelper.GetTypeName(unspecializedExceptionType, NameFormattingOptions.DocumentationId);
   }
   return null;
 }
Exemplo n.º 59
0
 /// <summary>
 /// Visits the specified precondition.
 /// </summary>
 /// <param name="precondition">The precondition.</param>
 public virtual IPrecondition Visit(IPrecondition precondition)
 {
     if (this.stopTraversal) return precondition;
       Precondition mutablePrecondition = precondition as Precondition;
       if (mutablePrecondition == null) return precondition;
       mutablePrecondition.Condition = this.Visit(mutablePrecondition.Condition);
       if (mutablePrecondition.Description != null)
     mutablePrecondition.Description = this.Visit(mutablePrecondition.Description);
       if (mutablePrecondition.ExceptionToThrow != null)
     mutablePrecondition.ExceptionToThrow = this.Visit(mutablePrecondition.ExceptionToThrow);
       return mutablePrecondition;
 }