Skip to content

Commit

Permalink
Merge pull request #741 from eventflow/small-fixes
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
rasmus authored Mar 4, 2020
2 parents 5c687ba + 8621068 commit 56cd52e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
10 changes: 7 additions & 3 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
### New in 0.78 (not released yet)

* Updated LibLog provider to support structured logging with NLog 4.5.
Reduced memory allocations for log4net-provider.
* Added quoting to the SQL query generator for the column names
* New: Updated LibLog provider to support structured logging with NLog 4.5.
Reduced memory allocations for log4net-provider
* New: Made several methods in `AggregateRoot<,>` `virtual` to allow
easier customization
* Fixed: Added quoting to the SQL query generator for the column names
```sql
-- query before the fix
UPDATE [ReadModel-TestAttributes]
Expand All @@ -14,6 +16,8 @@
SET [UpdatedTime] = @UpdatedTime
WHERE [Id] = @Id
```
* Fixed: Do not log about event upgraders if none is found for an event
* Fixed: Add default `null` predicate to `AddCommands` and `AddJobs`

### New in 0.77.4077 (released 2019-12-10)

Expand Down
30 changes: 21 additions & 9 deletions Source/EventFlow/Aggregates/AggregateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public abstract class AggregateRoot<TAggregate, TIdentity> : IAggregateRoot<TIde
private readonly List<IUncommittedEvent> _uncommittedEvents = new List<IUncommittedEvent>();
private CircularBuffer<ISourceId> _previousSourceIds = new CircularBuffer<ISourceId>(10);

public IAggregateName Name => AggregateName;
public virtual IAggregateName Name => AggregateName;
public TIdentity Id { get; }
public int Version { get; protected set; }
public bool IsNew => Version <= 0;
public virtual bool IsNew => Version <= 0;
public IEnumerable<IUncommittedEvent> UncommittedEvents => _uncommittedEvents;

static AggregateRoot()
Expand All @@ -55,8 +55,11 @@ static AggregateRoot()

protected AggregateRoot(TIdentity id)
{
if (id == null) throw new ArgumentNullException(nameof(id));
if ((this as TAggregate) == null)
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}
if (!(this is TAggregate))
{
throw new InvalidOperationException(
$"Aggregate '{GetType().PrettyPrint()}' specifies '{typeof(TAggregate).PrettyPrint()}' as generic argument, it should be its own type");
Expand All @@ -70,15 +73,18 @@ protected void SetSourceIdHistory(int count)
_previousSourceIds = new CircularBuffer<ISourceId>(count);
}

public bool HasSourceId(ISourceId sourceId)
public virtual bool HasSourceId(ISourceId sourceId)
{
return !sourceId.IsNone() && _previousSourceIds.Any(s => s.Value == sourceId.Value);
}

protected virtual void Emit<TEvent>(TEvent aggregateEvent, IMetadata metadata = null)
where TEvent : IAggregateEvent<TAggregate, TIdentity>
{
if (aggregateEvent == null) throw new ArgumentNullException(nameof(aggregateEvent));
if (aggregateEvent == null)
{
throw new ArgumentNullException(nameof(aggregateEvent));
}

var aggregateSequenceNumber = Version + 1;
var eventId = EventId.NewDeterministic(
Expand Down Expand Up @@ -135,9 +141,12 @@ public virtual async Task<IReadOnlyCollection<IDomainEvent>> CommitAsync(
return domainEvents;
}

public void ApplyEvents(IReadOnlyCollection<IDomainEvent> domainEvents)
public virtual void ApplyEvents(IReadOnlyCollection<IDomainEvent> domainEvents)
{
if (domainEvents == null) throw new ArgumentNullException(nameof(domainEvents));
if (domainEvents == null)
{
throw new ArgumentNullException(nameof(domainEvents));
}

foreach (var domainEvent in domainEvents)
{
Expand Down Expand Up @@ -169,7 +178,10 @@ public IIdentity GetIdentity()

protected virtual void ApplyEvent(IAggregateEvent<TAggregate, TIdentity> aggregateEvent)
{
if (aggregateEvent == null) throw new ArgumentNullException(nameof(aggregateEvent));
if (aggregateEvent == null)
{
throw new ArgumentNullException(nameof(aggregateEvent));
}

var eventType = aggregateEvent.GetType();
if (_eventHandlers.ContainsKey(eventType))
Expand Down
9 changes: 6 additions & 3 deletions Source/EventFlow/EventStores/EventUpgradeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,10 @@ public IReadOnlyCollection<IDomainEvent> Upgrade(IReadOnlyCollection<IDomainEven

private IEnumerable<IDomainEvent> Upgrade(IEnumerable<IDomainEvent> domainEvents)
{
// TODO: Clean this up!

var domainEventList = domainEvents.ToList();
if (!domainEventList.Any())
{
return new IDomainEvent[] { };
return Enumerable.Empty<IDomainEvent>();
}

var eventUpgraders = domainEventList
Expand All @@ -92,6 +90,11 @@ private IEnumerable<IDomainEvent> Upgrade(IEnumerable<IDomainEvent> domainEvents
};
});

if (!eventUpgraders.Any())
{
return Enumerable.Empty<IDomainEvent>();
}

_log.Verbose(() => string.Format(
"Upgrading {0} events and found these event upgraders to use: {1}",
domainEventList.Count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static IEventFlowOptions AddCommands(
public static IEventFlowOptions AddCommands(
this IEventFlowOptions eventFlowOptions,
Assembly fromAssembly,
Predicate<Type> predicate)
Predicate<Type> predicate = null)
{
predicate = predicate ?? (t => true);
var commandTypes = fromAssembly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static IEventFlowOptions AddJobs(
public static IEventFlowOptions AddJobs(
this IEventFlowOptions eventFlowOptions,
Assembly fromAssembly,
Predicate<Type> predicate)
Predicate<Type> predicate = null)
{
predicate = predicate ?? (t => true);
var jobTypes = fromAssembly
Expand Down

0 comments on commit 56cd52e

Please sign in to comment.