public void ApplyChange(IDomainEvent <TId> domainEvent, bool isFromHistory = false) { lock (_events) { InnerApply(domainEvent); if (!isFromHistory) { _events.Enqueue(domainEvent); domainEvent.SetVersion(Version + 1); } if (Version + 1 != domainEvent.Version) { throw new AggregateVersioningException( $"DomainEvent({domainEvent.GetType().Name}) version {domainEvent.Version} is not valid, next version should be {Version + 1}"); } Version++; } }
private void ApplyChange(IDomainEvent e, bool isNew) { var applyMethodInfo = GetType().GetMethod("Apply", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { e.GetType() }, null); if (applyMethodInfo == null) { throw new InvalidOperationException($"Aggregate '{GetType().Name}' can't apply '{e.GetType().Name}' event type."); } applyMethodInfo.Invoke(this, new object[] { e }); IncrementedVersion(); if (isNew) { e.SetVersion(Version); e.SetTimestamp(UtcNow); _domainEvents.Add(e); } }