forked from NEventStore/CommonDomain-ReadOnly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exposed the EventRouter from AggregateBase, so that we don't have to …
…call the default constructor when instantiating aggregates. Made throwing exceptions when apply methods arenot found optional, so that historical events don't have to have empty apply methods.
- Loading branch information
Showing
2 changed files
with
100 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,93 @@ | ||
namespace CommonDomain.Core | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
public abstract class AggregateBase : IAggregate, IEquatable<IAggregate> | ||
{ | ||
private readonly ICollection<object> uncommittedEvents = new LinkedList<object>(); | ||
private readonly IRouteEvents registeredRoutes = new ConventionEventRouter(); | ||
public abstract class AggregateBase : IAggregate, IEquatable<IAggregate> | ||
{ | ||
private readonly ICollection<object> uncommittedEvents = new LinkedList<object>(); | ||
|
||
protected AggregateBase() | ||
: this(null) | ||
{ | ||
} | ||
protected AggregateBase(IRouteEvents handler) | ||
{ | ||
this.registeredRoutes = handler ?? this.registeredRoutes; | ||
this.registeredRoutes.Register(this); | ||
} | ||
private IRouteEvents registeredRoutes; | ||
|
||
public Guid Id { get; protected set; } | ||
public int Version { get; protected set; } | ||
protected AggregateBase() | ||
: this(null) | ||
{ | ||
} | ||
|
||
protected void Register<T>(Action<T> route) | ||
{ | ||
this.registeredRoutes.Register(route); | ||
} | ||
protected AggregateBase(IRouteEvents handler) | ||
{ | ||
if (handler == null) return; | ||
|
||
protected void RaiseEvent(object @event) | ||
{ | ||
((IAggregate)this).ApplyEvent(@event); | ||
this.uncommittedEvents.Add(@event); | ||
} | ||
void IAggregate.ApplyEvent(object @event) | ||
{ | ||
this.registeredRoutes.Dispatch(@event); | ||
this.Version++; | ||
} | ||
ICollection IAggregate.GetUncommittedEvents() | ||
{ | ||
return (ICollection)this.uncommittedEvents; | ||
} | ||
void IAggregate.ClearUncommittedEvents() | ||
{ | ||
this.uncommittedEvents.Clear(); | ||
} | ||
this.RegisteredRoutes = handler; | ||
this.RegisteredRoutes.Register(this); | ||
} | ||
|
||
IMemento IAggregate.GetSnapshot() | ||
{ | ||
var snapshot = this.GetSnapshot(); | ||
snapshot.Id = this.Id; | ||
snapshot.Version = this.Version; | ||
return snapshot; | ||
} | ||
protected virtual IMemento GetSnapshot() | ||
{ | ||
return null; | ||
} | ||
public Guid Id { get; protected set; } | ||
public int Version { get; protected set; } | ||
|
||
public override int GetHashCode() | ||
{ | ||
return this.Id.GetHashCode(); | ||
} | ||
public override bool Equals(object obj) | ||
{ | ||
return this.Equals(obj as IAggregate); | ||
} | ||
public virtual bool Equals(IAggregate other) | ||
{ | ||
return null != other && other.Id == this.Id; | ||
} | ||
} | ||
protected IRouteEvents RegisteredRoutes | ||
{ | ||
get | ||
{ | ||
return registeredRoutes ?? (registeredRoutes = new ConventionEventRouter(true, this)); | ||
} | ||
set | ||
{ | ||
if (value == null) | ||
throw new InvalidOperationException("AggregateBase must have an event router to function"); | ||
|
||
registeredRoutes = value; | ||
} | ||
} | ||
|
||
protected void Register<T>(Action<T> route) | ||
{ | ||
this.registeredRoutes.Register(route); | ||
} | ||
|
||
protected void RaiseEvent(object @event) | ||
{ | ||
((IAggregate)this).ApplyEvent(@event); | ||
this.uncommittedEvents.Add(@event); | ||
} | ||
void IAggregate.ApplyEvent(object @event) | ||
{ | ||
this.registeredRoutes.Dispatch(@event); | ||
this.Version++; | ||
} | ||
ICollection IAggregate.GetUncommittedEvents() | ||
{ | ||
return (ICollection)this.uncommittedEvents; | ||
} | ||
void IAggregate.ClearUncommittedEvents() | ||
{ | ||
this.uncommittedEvents.Clear(); | ||
} | ||
|
||
IMemento IAggregate.GetSnapshot() | ||
{ | ||
var snapshot = this.GetSnapshot(); | ||
snapshot.Id = this.Id; | ||
snapshot.Version = this.Version; | ||
return snapshot; | ||
} | ||
protected virtual IMemento GetSnapshot() | ||
{ | ||
return null; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return this.Id.GetHashCode(); | ||
} | ||
public override bool Equals(object obj) | ||
{ | ||
return this.Equals(obj as IAggregate); | ||
} | ||
public virtual bool Equals(IAggregate other) | ||
{ | ||
return null != other && other.Id == this.Id; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters