/// <summary> /// Calls <see cref="ISnapshotStrategy{TAuthenticationToken}.IsSnapshotable"/> on <see cref="SnapshotStrategy"/> /// If the <typeparamref name="TAggregateRoot"/> is snapshot-able <see cref="ISnapshotStore.Get{TAggregateRoot}"/> is called on <see cref="SnapshotStore"/>. /// The Restore method is then called on /// </summary> /// <typeparam name="TAggregateRoot">The <see cref="Type"/> of the <see cref="IAggregateRoot{TAuthenticationToken}"/>.</typeparam> /// <param name="id">The identifier of the <see cref="IAggregateRoot{TAuthenticationToken}"/> to restore, since the <paramref name="aggregate"/> may be completely uninitialised.</param> /// <param name="aggregate">The <typeparamref name="TAggregateRoot"/></param> /// <returns>-1 if no restoration was made, otherwise version number the <typeparamref name="TAggregateRoot"/> was rehydrated to.</returns> /// <remarks>There may be more events after the snapshot that still need to rehydrated into the <typeparamref name="TAggregateRoot"/> after restoration.</remarks> protected virtual int TryRestoreAggregateFromSnapshot <TAggregateRoot>(Guid id, TAggregateRoot aggregate) { int version = -1; if (SnapshotStrategy.IsSnapshotable(aggregate.GetType())) { Snapshot snapshot = SnapshotStore.Get(aggregate.GetType(), id); if (snapshot != null) { aggregate.AsDynamic().Restore(snapshot); version = snapshot.Version; } } return(version); }