/// <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> /// Finds the and modify. /// </summary> /// <param name="id">The id.</param> /// <param name="events">The events.</param> /// <returns></returns> public EntityEventSource <TEntity, long> AddEvent(long id, IEnumerable <IDomainEvent <TEntity> > events) { if (NHibernateHelper.CurrentSession.Get <EntityEventSourceWrapper>(id) == null) { NHibernateHelper.CurrentSession.Save(new EntityEventSourceWrapper { Id = id, Status = "Ready" }); NHibernateHelper.CurrentSession.Flush(); } EntityEventSource <TEntity, long> entity = null; SpinWait.SpinUntil(() => { var criterion = Restrictions.Or(Restrictions.Or(Restrictions.Eq("Status", "Ready"), Restrictions.Le("ProcessingAt", DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(_timeOut)))), Restrictions.Eq("ProcessingBy", Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture))); var query = Restrictions.And(Restrictions.Eq("Id", id), criterion); var entitySourceWrapper = (EntityEventSourceWrapper)NHibernateHelper.CurrentSession .CreateCriteria <EntityEventSourceWrapper>() .Add(query).UniqueResult(); if (entitySourceWrapper != null) { entitySourceWrapper.Status = "Processing"; entitySourceWrapper.ProcessingAt = DateTime.UtcNow; entitySourceWrapper.ProcessingBy = Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture); foreach (var domainEvent in events) { BsonClassMap.IsClassMapRegistered(domainEvent.GetType()); var eventWrapper = new EventWrapper { Type = domainEvent.GetType().Name, Data = domainEvent.ToBson(), Version = domainEvent.Version }; entitySourceWrapper.AddEvent(eventWrapper); } NHibernateHelper.CurrentSession.Update(entitySourceWrapper); NHibernateHelper.CurrentSession.Flush(); entity = new EntityEventSource <TEntity, long>(); Translator.Instance.TranslateToDomain(entity, entitySourceWrapper); } return(entity != null); }); return(entity); }
/// <summary> /// Adds the event. /// </summary> /// <param name="eventWrapper">The event wrapper.</param> public virtual void AddEvent(EventWrapper eventWrapper) { Events.Add(eventWrapper); eventWrapper.Entity = this; }