/// <summary> /// Saves the snap shot. /// </summary> /// <param name="entity">The entity.</param> /// <param name="snapShotType">Type of the snap shot.</param> /// <param name="events">The events.</param> /// <param name="state">The state.</param> /// <exception cref="System.NotImplementedException"></exception> public void SaveSnapShot(TEntity entity, SnapShotType snapShotType, IEnumerable <IDomainEvent <TEntity> > events, string state) { var query = _querySnapshotBuilder.GetQuery(snapShotType, entity); var entitySourceWrapper = (EntityEventSourceWrapper)NHibernateHelper.CurrentSession .CreateCriteria <EntityEventSourceWrapper>() .Add(query).UniqueResult(); entitySourceWrapper.SnapShot = entity.ToBson(); entitySourceWrapper.Version = entity.Version; entitySourceWrapper.State = state; entitySourceWrapper.ClearEvents(); foreach (var domainEvent in events) { var eventWrapper = new EventWrapper { Type = domainEvent.GetType().Name, Data = domainEvent.ToBson() }; entitySourceWrapper.AddEvent(eventWrapper); } NHibernateHelper.CurrentSession.Update(entitySourceWrapper); NHibernateHelper.CurrentSession.Flush(); }
/// <summary> /// Gets the query. /// </summary> /// <param name="snapShotType">Type of the snap shot.</param> /// <param name="entity">The entity.</param> /// <returns></returns> public IMongoQuery GetQuery(SnapShotType snapShotType, TEntity entity) { return(snapShotType == SnapShotType.ById ? Query.EQ("_id", BsonDocumentWrapper.Create(entity.Id)) : Query.And(Query.EQ("_id", BsonDocumentWrapper.Create(entity.Id)), Query.Or(Query.NotExists("Version"), Query.LTE("Version", BsonDocumentWrapper.Create(entity.Version))) )); }
/// <summary> /// Saves the snap shot. /// </summary> /// <param name="entity">The entity.</param> /// <param name="snapShotType">Type of the snap shot.</param> /// <param name="events">The events.</param> /// <param name="state">The state.</param> public void SaveSnapShot(TEntity entity, SnapShotType snapShotType, IEnumerable <IDomainEvent <TEntity> > events, string state) { _repository.Update ( _mongoBuilder.GetQuery(snapShotType, entity), x => ((UpdateBuilder)x.GetUpdateBuilder()) .Set("SnapShot", BsonDocumentWrapper.Create(entity)) .Set("Version", BsonDocumentWrapper.Create(entity.Version)) .Set("State", BsonDocumentWrapper.Create(state)) .PullAll("Events", events.Select(BsonDocumentWrapper.Create)) .Set("AppliedEvents", BsonDocumentWrapper.Create(events)) ); }
internal SnapShotData(SnapShotType type, string ownerId, byte[] buffer) { Type = type; Buffer = buffer; OwnerId = ownerId; }
/// <summary> /// Gets the query. /// </summary> /// <param name="snapShotType">Type of the snap shot.</param> /// <param name="entity">The entity.</param> /// <returns></returns> public ICriterion GetQuery(SnapShotType snapShotType, TEntity entity) { return(snapShotType == SnapShotType.ById ? Restrictions.Eq("Id", entity.Id) : Restrictions.And(Restrictions.Eq("Id", entity.Id), Restrictions.Le("Version", entity.Version))); }