Class SnapshotRepository<TAuthenticationToken>
Provides basic repository methods for operations with instances of IAggregateRoot<TAuthenticationToken> utilising Snapshot for optimised rehydration.
Inheritance
Inherited Members
Namespace: Cqrs.Snapshots
Assembly: Cqrs.dll
Syntax
public class SnapshotRepository<TAuthenticationToken> : IAggregateRepository<TAuthenticationToken>
Type Parameters
Name | Description |
---|---|
TAuthenticationToken | The System.Type of authentication token. |
Constructors
SnapshotRepository(ISnapshotStore, ISnapshotStrategy<TAuthenticationToken>, IAggregateRepository<TAuthenticationToken>, IEventStore<TAuthenticationToken>, IAggregateFactory)
Instantiates a new instance of SnapshotRepository<TAuthenticationToken>.
Declaration
public SnapshotRepository(ISnapshotStore snapshotStore, ISnapshotStrategy<TAuthenticationToken> snapshotStrategy, IAggregateRepository<TAuthenticationToken> repository, IEventStore<TAuthenticationToken> eventStore, IAggregateFactory aggregateFactory)
Parameters
Type | Name | Description |
---|---|---|
ISnapshotStore | snapshotStore | |
ISnapshotStrategy<TAuthenticationToken> | snapshotStrategy | |
IAggregateRepository<TAuthenticationToken> | repository | |
IEventStore<TAuthenticationToken> | eventStore | |
IAggregateFactory | aggregateFactory |
Properties
AggregateFactory
Gets or sets the IAggregateFactory.
Declaration
protected IAggregateFactory AggregateFactory { get; }
Property Value
Type | Description |
---|---|
IAggregateFactory |
EventStore
Gets or sets the IEventStore<TAuthenticationToken>.
Declaration
protected IEventStore<TAuthenticationToken> EventStore { get; }
Property Value
Type | Description |
---|---|
IEventStore<TAuthenticationToken> |
Repository
Gets or sets the IAggregateRepository<TAuthenticationToken>.
Declaration
protected IAggregateRepository<TAuthenticationToken> Repository { get; }
Property Value
Type | Description |
---|---|
IAggregateRepository<TAuthenticationToken> |
SnapshotStore
Gets or sets the ISnapshotStore.
Declaration
protected ISnapshotStore SnapshotStore { get; }
Property Value
Type | Description |
---|---|
ISnapshotStore |
SnapshotStrategy
Gets or sets the ISnapshotStrategy<TAuthenticationToken>.
Declaration
protected ISnapshotStrategy<TAuthenticationToken> SnapshotStrategy { get; }
Property Value
Type | Description |
---|---|
ISnapshotStrategy<TAuthenticationToken> |
Methods
Get<TAggregateRoot>(Guid, IList<IEvent<TAuthenticationToken>>)
Retrieves an IAggregateRoot<TAuthenticationToken> of type TAggregateRoot
,
First using TryRestoreAggregateFromSnapshot<TAggregateRoot>(Guid, TAggregateRoot), otherwise via Get<TAggregateRoot>(Guid, IList<IEvent<TAuthenticationToken>>) on Repository
Then does rehydration.
Declaration
public TAggregateRoot Get<TAggregateRoot>(Guid aggregateId, IList<IEvent<TAuthenticationToken>> events = null)where TAggregateRoot : IAggregateRoot<TAuthenticationToken>
Parameters
Type | Name | Description |
---|---|---|
System.Guid | aggregateId | The identifier of the IAggregateRoot<TAuthenticationToken> to retrieve. |
System.Collections.Generic.IList<IEvent<TAuthenticationToken>> | events | A collection of IEvent<TAuthenticationToken> to replay on the retrieved IAggregateRoot<TAuthenticationToken>. If null, the IEventStore<TAuthenticationToken> will be used to retrieve a list of IEvent<TAuthenticationToken> for you. |
Returns
Type | Description |
---|---|
TAggregateRoot |
Type Parameters
Name | Description |
---|---|
TAggregateRoot | The System.Type of the IAggregateRoot<TAuthenticationToken>. |
Implements
Save<TAggregateRoot>(TAggregateRoot, Nullable<Int32>)
Calls TryMakeSnapshot(IAggregateRoot<TAuthenticationToken>) then Save<TAggregateRoot>(TAggregateRoot, Nullable<Int32>) on Repository.
Declaration
public void Save<TAggregateRoot>(TAggregateRoot aggregate, int ? expectedVersion = null)where TAggregateRoot : IAggregateRoot<TAuthenticationToken>
Parameters
Type | Name | Description |
---|---|---|
TAggregateRoot | aggregate | The IAggregateRoot<TAuthenticationToken> to save and persist. |
System.Nullable<System.Int32> | expectedVersion | The version number the IAggregateRoot<TAuthenticationToken> is expected to be at. |
Type Parameters
Name | Description |
---|---|
TAggregateRoot | The System.Type of the IAggregateRoot<TAuthenticationToken>. |
Implements
TryMakeSnapshot(IAggregateRoot<TAuthenticationToken>)
Calls ShouldMakeSnapShot(IAggregateRoot<TAuthenticationToken>) on SnapshotStrategy If the IAggregateRoot<TAuthenticationToken> is snapshot-able GetSnapshot() is called The Version is calculated, finally Save(Snapshot) is called on SnapshotStore.
Declaration
protected virtual void TryMakeSnapshot(IAggregateRoot<TAuthenticationToken> aggregate)
Parameters
Type | Name | Description |
---|---|---|
IAggregateRoot<TAuthenticationToken> | aggregate | The IAggregateRoot<TAuthenticationToken> to try and snapshot. |
TryRestoreAggregateFromSnapshot<TAggregateRoot>(Guid, TAggregateRoot)
Calls IsSnapshotable(Type) on SnapshotStrategy
If the TAggregateRoot
is snapshot-able Get<TAggregateRoot>(Guid) is called on SnapshotStore.
The Restore method is then called on
Declaration
protected virtual int TryRestoreAggregateFromSnapshot<TAggregateRoot>(Guid id, TAggregateRoot aggregate)
Parameters
Type | Name | Description |
---|---|---|
System.Guid | id | The identifier of the IAggregateRoot<TAuthenticationToken> to restore, since the |
TAggregateRoot | aggregate | The |
Returns
Type | Description |
---|---|
System.Int32 | -1 if no restoration was made, otherwise version number the |
Type Parameters
Name | Description |
---|---|
TAggregateRoot | The System.Type of the IAggregateRoot<TAuthenticationToken>. |
Remarks
There may be more events after the snapshot that still need to rehydrated into the TAggregateRoot
after restoration.