Exemple #1
0
        public FibSequence(IMemento memento)
        {
            if (memento == null)
            {
                throw new ArgumentNullException("Memento is null");
            }
            var mem = (memento as FibSequenceMemento);

            if (mem == null)
            {
                throw new ArgumentException($"Memento's type mismatch: {memento.GetType().Name}");
            }
            _a = mem.A;
            _b = mem.B;
        }
        public static SoftwareProgrammingSagaState FromSnapshot(IMemento m)
        {
            Snapshot s = m as Snapshot;

            if (s == null)
            {
                throw new WrongSnapshotTypeReceivedException(m.GetType(), typeof(Snapshot));
            }
            var aggregate = new SoftwareProgrammingSagaState(s.Id, s.State);

            aggregate.RememberPerson(s.PersonId);
            aggregate.RememberBadCoffeMachine(s.CoffeMachineId);
            aggregate.Version = s.Version;
            aggregate.ClearEvents();
            return(aggregate);
        }
Exemple #3
0
        public static SagaDataAggregate <TSagaData> FromSnapshot(IMemento m)
        {
            Snapshot s = m as Snapshot;

            if (s == null)
            {
                throw new WrongSnapshotTypeReceivedException(m.GetType(), typeof(Snapshot));
            }
            var sagaDataAggregate = new SagaDataAggregate <TSagaData>(s.Id, s.Data)
            {
                Version = s.Version
            };

            ((IAggregate)sagaDataAggregate).ClearUncommittedEvents();
            return(sagaDataAggregate);
        }
        /// <summary>
        ///   Saves a snapshot of the specified event source.
        /// </summary>
        public void SaveShapShot(IEventSource source)
        {
            using (var ctx = new EventStoreContext())
            {
                IMemento memento = source.GetMemento();

                ctx.AddToSnapshots(new Snapshot
                {
                    Data        = ToBinary(converter.Convert(memento)),
                    AggregateId = source.Id,
                    Version     = source.Version,
                    Type        = memento.GetType().Name
                });

                ctx.SaveChanges();
            }
        }
        public IAggregate Build(Type type, Guid id, IMemento snapshot)
        {
            var typeParam = snapshot != null?snapshot.GetType() : typeof(Guid);

            var paramArray = snapshot != null
                ? new object[] { snapshot }
                : new object[] { id };

            var constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeParam }, null);

            if (constructor == null)
            {
                throw new InvalidOperationException(string.Format("Aggregate {0} cannot be created: constructor with proper parameter not provided", type.Name));
            }

            return(constructor.Invoke(paramArray) as IAggregate);
        }
 public void ApplyMemento(IMemento memento)
 {
     if (memento == null) throw new ArgumentNullException("memento");
     TextBoxMemento textBoxMemento;
     try
     {
         textBoxMemento = (TextBoxMemento) memento;
     }
     catch (InvalidCastException ex)
     {
         throw new IncompatibleMementoException(
             string.Format(
                 "Only memento of type [{0}] be applied to [{1}] ",
                 memento.GetType(),
                 GetType()),
             ex);
     }
     Text = textBoxMemento.Text;
     MoveCaretTo(textBoxMemento.CaretPostition);
     Select(textBoxMemento.Selection);
 }
Exemple #7
0
 public async Task UpdateSnapshotAsync(Guid streamId, int currentVersion, IMemento snapshot, CancellationToken cancellationToken = new CancellationToken())
 {
     var contractName = _settings.ContractsRegistry.GetContractName(snapshot.GetType());
     var payload      = _settings.Serializer.Serialize(snapshot, snapshot.GetType());
     await _settings.Persistor.UpdateSnapshotAsync(streamId, currentVersion, contractName, payload, cancellationToken);
 }
        public IAggregate Build(Type type, Guid id, IMemento snapshot)
        {
            if (snapshot == null)
            {
                return(BuildFromStart(type));
            }

            ConstructorInfo constructor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { snapshot.GetType() }, null);

            if (constructor == null)
            {
                throw new InvalidOperationException(string.Format("Aggregate {0} cannot be created: no non-public constructor that accepts IMemento has been provided", type.Name));
            }
            return(constructor.Invoke(new object[] { snapshot }) as IAggregate);
        }