/// <summary> /// Gets the instance of the aggregate root with the specified identifier. /// </summary> /// <param name="id">The identifier of the aggregate root.</param> /// <returns>The instance of the aggregate root with the specified identifier.</returns> public override TAggregateRoot Get <TAggregateRoot>(TAggregateRootId id) { var aggregateRootType = typeof(TAggregateRoot).AssemblyQualifiedName; var spec = Specification <SnapshotDataObject <TAggregateRootId> > .Eval(p => p.AggregateRootId.Equals(id) && p.AggregateRootType == aggregateRootType); var snapshotDataObject = this._storage.SelectFirstOnly(spec); if (snapshotDataObject == null) { throw new RepositoryException("The aggregate (id={0}) cannot be found in the domain repository.", id); } var snapshot = _snapshotSerializer.ExtractSnapshot(snapshotDataObject); var aggregateRoot = this.CreateAggregateRootInstance <TAggregateRoot>(); aggregateRoot.BuildFromSnapshot(snapshot); return(aggregateRoot); }
/// <summary> /// Gets the snapshot for the aggregate root with the given type and identifier. /// </summary> /// <param name="aggregateRootType">The type of the aggregate root.</param> /// <param name="id">The identifier of the aggregate root.</param> /// <returns>The snapshot instance.</returns> public override ISnapshot <TAggregateRootId> GetSnapshot(Type aggregateRootType, TAggregateRootId id) { var key = new EventNumberSnapshotMappingKey(aggregateRootType.AssemblyQualifiedName, id); if (_snapshotMapping.ContainsKey(key)) { return(_snapshotMapping[key]); } var aggregateRootTypeName = aggregateRootType.AssemblyQualifiedName; ISpecification <SnapshotDataObject <TAggregateRootId> > spec = Specification <SnapshotDataObject <TAggregateRootId> > .Eval( p => p.AggregateRootType == aggregateRootTypeName && p.AggregateRootId.Equals(id)); var dataObj = this.SnapshotStorage.SelectFirstOnly <SnapshotDataObject <TAggregateRootId> >(spec); if (dataObj == null) { return(null); } var snapshot = _snapshotSerializer.ExtractSnapshot(dataObj); this._snapshotMapping.Add(key, snapshot); return(snapshot); }