Пример #1
0
        public void DeleteEvent(IStoredEvent storedEvent)
        {
            var eventId  = ((StoredEvent)storedEvent).Id;
            var theEvent = _session.Load <StoredEvent>(eventId);

            _session.Delete(theEvent);
        }
Пример #2
0
        public async Task Append(IStoredEvent[] @events)
        {
            await Task.Run(() =>
            {
                var handler = BeforeSave;
                if (handler != null)
                {
                    @events.ForEach(e => handler(this, e));
                }

                lock (this.events)
                {
                    foreach (var storedEvent in events)
                    {
                        if (this.events.Contains(storedEvent))
                        {
                            throw new ConcurrencyException(string.Format("There was a concurrency violation.\n  Existing:\n{0}\nAttempted:\n{1}",
                                this.events.Single(e => e.AggregateId == storedEvent.AggregateId &&
                                                        e.SequenceNumber == storedEvent.SequenceNumber)
                                    .ToDomainEvent(Name)
                                    .ToDiagnosticJson(),
                                storedEvent.ToDiagnosticJson()));
                        }
                        this.events.Add(storedEvent);
                    }
                }
            });
        }
Пример #3
0
 /// <summary>
 /// Creates a domain event from a <see cref="IStoredEvent" />.
 /// </summary>
 /// <param name="storedEvent">The storable event.</param>
 /// <returns>A deserialized domain event.</returns>
 public static IEvent ToDomainEvent(this IStoredEvent storedEvent) =>
 Serializer.DeserializeEvent(
     aggregateName: storedEvent.StreamName,
     eventName: storedEvent.Type,
     aggregateId: Guid.Parse(storedEvent.AggregateId),
     sequenceNumber: storedEvent.SequenceNumber,
     etag: storedEvent.ETag,
     timestamp: storedEvent.Timestamp,
     body: storedEvent.Body,
     uniqueEventId: storedEvent.Timestamp.Ticks,
     serializerSettings: serializerSettings.Value);
Пример #4
0
 /// <summary>
 /// Creates a domain event from a <see cref="IStoredEvent" />.
 /// </summary>
 /// <param name="storedEvent">The storable event.</param>
 /// <returns>A deserialized domain event.</returns>
 public static IEvent ToDomainEvent(this IStoredEvent storedEvent, string streamName)
 {
     return(Serializer.DeserializeEvent(
                streamName,
                storedEvent.Type,
                Guid.Parse(storedEvent.AggregateId),
                storedEvent.SequenceNumber,
                storedEvent.Timestamp, storedEvent.Body,
                storedEvent.Timestamp.Ticks,
                serializerSettings.Value));
 }
Пример #5
0
 /// <summary>
 ///     Introduces a set of steps that mimic setting a property with event notification.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'stored' step is added.</param>
 /// <param name="propertyChangedEvent">The event step used to store <see cref="PropertyChangedEventHandler" /> instances.</param>
 /// <param name="initialValue">The initial value of the property.</param>
 /// <param name="comparer">
 ///     An optional comparer used to determine if the value of the property has changed. An event will
 ///     only be raised if it has.
 /// </param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static IStoredProperty <TValue> StoredWithChangeNotification <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     IStoredEvent <PropertyChangedEventHandler> propertyChangedEvent,
     TValue initialValue = default,
     IEqualityComparer <TValue>?comparer = null)
 {
     return(caller
            .OnlySetIfChanged(comparer)
            .RaisePropertyChangedEvent(propertyChangedEvent)
            .Stored(initialValue));
 }
Пример #6
0
 internal static InMemoryStoredEvent ToStoredEvent(this IStoredEvent e)
 {
     return(new InMemoryStoredEvent
     {
         SequenceNumber = e.SequenceNumber,
         AggregateId = e.AggregateId,
         Timestamp = e.Timestamp,
         Type = e.Type,
         Body = e.Body,
         ETag = e.ETag
     });
 }
        public RaisePropertyChangedEventPropertyStepTests()
        {
            _mockProperties = new MockPropertiesWithChangeNotification();
            _properties     = _mockProperties;

            _npc = _mockProperties.PropertyChanged.Stored();

            ((INotifyPropertyChanged)_mockProperties).PropertyChanged += (sender, args) =>
            {
                _changedPropertyNames.Add(args.PropertyName);
            };
        }
Пример #8
0
        private void ThrowConcurrencyException(IStoredEvent storedEvent)
        {
            var existing = events.Single(
                e => e.AggregateId == storedEvent.AggregateId &&
                     e.SequenceNumber == storedEvent.SequenceNumber)
                                 .ToDomainEvent(Name)
                                 .ToDiagnosticJson();

            var attempted = storedEvent
                .ToDomainEvent(Name)
                .ToDiagnosticJson();

            throw new ConcurrencyException(
                string.Format(
                    @"There was a concurrency violation.
Existing:
{0}
Attempted:
{1}",
                    existing,
                    attempted));
        }
Пример #9
0
        private void ThrowConcurrencyException(IStoredEvent storedEvent)
        {
            var existing = events.Single(
                e => e.AggregateId == storedEvent.AggregateId &&
                e.SequenceNumber == storedEvent.SequenceNumber)
                           .ToDomainEvent(Name)
                           .ToDiagnosticJson();

            var attempted = storedEvent
                            .ToDomainEvent(Name)
                            .ToDiagnosticJson();

            throw new ConcurrencyException(
                      string.Format(
                          @"There was a concurrency violation.
Existing:
{0}
Attempted:
{1}",
                          existing,
                          attempted));
        }
Пример #10
0
        public async Task Append(IStoredEvent[] @events)
        {
            await Task.Run(() =>
            {
                var handler = BeforeSave;
                if (handler != null)
                {
                    @events.ForEach(e => handler(this, e));
                }

                lock (this.events)
                {
                    foreach (var storedEvent in events)
                    {
                        if (this.events.Contains(storedEvent))
                        {
                            ThrowConcurrencyException(storedEvent);
                        }
                        this.events.Add(storedEvent);
                    }
                }
            });
        }
Пример #11
0
        public IEvent UnwrapEvent(IStoredEvent storedEvent)
        {
            var theEvent = (StoredEvent)storedEvent;

            return(_serializer.Deserialize(theEvent.Body));
        }
Пример #12
0
 /// <summary>
 ///     Raises an <see cref="EventHandler{TEventArgs}" /> generic event using a sender and
 ///     <typeparamref name="TEventArg" /> pair.
 /// </summary>
 /// <typeparam name="TEventArg">The type argument used for this event handler type.</typeparam>
 /// <param name="storedEvent">The stored event step that holds the event handler.</param>
 /// <param name="sender">The event sender.</param>
 /// <param name="e">The <typeparamref name="TEventArg" /> instance containing the event data.</param>
 public static void Raise <TEventArg>(this IStoredEvent <EventHandler <TEventArg> > storedEvent, object sender,
                                      TEventArg e) where TEventArg : EventArgs
 {
     storedEvent.EventHandler?.Invoke(sender, e);
 }
Пример #13
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="RaisePropertyChangedEventPropertyStep{TValue}" /> class.
 /// </summary>
 /// <param name="propertyChangedEvent">The event store that is used to raise the event.</param>
 public RaisePropertyChangedEventPropertyStep(IStoredEvent <PropertyChangedEventHandler> propertyChangedEvent)
 {
     _propertyChangedEvent = propertyChangedEvent ?? throw new ArgumentNullException(nameof(propertyChangedEvent));
 }
Пример #14
0
 /// <summary>
 ///     Introduces a step that will invoke the given <see cref="PropertyChangedEventHandler" /> with the name of the
 ///     property when set. It will also forward on all calls to the next step.
 /// </summary>
 /// <typeparam name="TValue">The type of the property.</typeparam>
 /// <param name="caller">The mock or step to which this 'miscellaneous' step is added.</param>
 /// <param name="propertyChangedEvent">The property changed event.</param>
 /// <returns>An <see cref="ICanHaveNextPropertyStep{TValue}" /> that can be used to add further steps.</returns>
 public static ICanHaveNextPropertyStep <TValue> RaisePropertyChangedEvent <TValue>(
     this ICanHaveNextPropertyStep <TValue> caller,
     IStoredEvent <PropertyChangedEventHandler> propertyChangedEvent)
 {
     return(caller.SetNextStep(new RaisePropertyChangedEventPropertyStep <TValue>(propertyChangedEvent)));
 }
Пример #15
0
 public IEvent UnwrapEvent(IStoredEvent storedEvent)
 {
     return(_serializer.Deserialize(((StoredEvent)storedEvent).Body));
 }
Пример #16
0
        public void DeleteEvent(IStoredEvent theEvent)
        {
            var id = ((StoredEvent)theEvent).Id;

            _session.Delete(id);
        }
Пример #17
0
 /// <summary>
 ///     Raises an <see cref="PropertyChangedEventHandler" /> event using a sender and
 ///     <see cref="PropertyChangedEventArgs" /> pair.
 /// </summary>
 /// <param name="storedEvent">The stored event step that holds the event handler.</param>
 /// <param name="sender">The event sender.</param>
 /// <param name="e">The <see cref="PropertyChangedEventArgs" /> instance containing the event data.</param>
 public static void Raise(this IStoredEvent <PropertyChangedEventHandler> storedEvent, object sender,
                          PropertyChangedEventArgs e)
 {
     storedEvent.EventHandler?.Invoke(sender, e);
 }