protected override ISaga <T> DoCreateInstance(string sagaIdentifier, Func <T> sagaFactory)
 {
     try
     {
         var unitOfWOrk  = CurrentUnitOfWork.Get();
         var processRoot = unitOfWOrk.Root();
         var sagaRoot    = sagaFactory.Invoke();
         var saga        = new AnnotatedSaga <T>(sagaIdentifier, new HashSet <AssociationValue>(), sagaRoot, null, _sagaModel);
         UnsavedSagasResource(processRoot).Add(sagaIdentifier);
         unitOfWOrk.OnPrepareCommit(u =>
         {
             if (saga.IsActive)
             {
                 StoreSaga(saga);
                 saga.AssociationValues.Commit();
                 UnsavedSagasResource(processRoot).Remove(sagaIdentifier);
             }
         });
         _managedSagas[sagaIdentifier] = saga;
         processRoot.OnCleanup(u => _managedSagas.Remove(sagaIdentifier));
         return(saga);
     }
     catch (Exception e)
     {
         throw new SagaCreationException("An error occurred while attempting to create a new managed instance", e);
     }
 }
 private void Commit(AnnotatedSaga <T> saga)
 {
     if (!saga.IsActive)
     {
         DeleteSaga(saga);
     }
     else
     {
         UpdateSaga(saga);
         saga.AssociationValues.Commit();
     }
 }
 private void UpdateSaga(AnnotatedSaga <T> saga)
 {
     _sagaStore.UpdateSaga(typeof(T), saga.SagaIdentifier, saga.Root, saga.TrackingToken, saga.AssociationValues);
 }
 private void StoreSaga(AnnotatedSaga <T> saga)
 {
     _sagaStore.InsertSaga(typeof(T), saga.SagaIdentifier, saga.Root, saga.TrackingToken, saga.AssociationValues.AsSet());
 }
        private void DeleteSaga(AnnotatedSaga <T> saga)
        {
            var associationValues = saga.AssociationValues.ToHashSet().Union(saga.AssociationValues.RemovedAssociations).ToHashSet();

            _sagaStore.DeleteSaga(typeof(T), saga.SagaIdentifier, associationValues);
        }