Beispiel #1
0
        protected void DispatchToSagaInternal(string sagaId, bool createNew, SagaBase sagaHandler, Action <SagaBase> callback)
        {
            string version       = null;
            bool   found         = false;
            object sagaState     = null;
            var    st            = Stopwatch.StartNew();
            var    sagaStateType = sagaHandler.GetType().BaseType.GetGenericArguments()[0]; //todo: what if inheritance is deeper???

            if (!string.IsNullOrEmpty(sagaId))
            {
                found = SagaStateRepo.Get(sagaId, sagaStateType, true, out sagaState, out version);
            }
            else //create new id
            {
                sagaId = Guid.NewGuid().ToString("N");
            }

            if (!found)
            {
                if (!createNew)
                {
                    throw new Exception("Saga instance not found: " + sagaId);
                }
                sagaState = Activator.CreateInstance(sagaStateType);

                version = "1";
            }
            sagaHandler.IsNew = !found;
            sagaHandler.InitializeSagaState(sagaId, version, sagaState);
            callback(sagaHandler);
            if (sagaHandler.Completed)
            {
                if (!createNew)
                {
                    SagaStateRepo.Delete(sagaId);
                    log.Info("Deleted saga {0}/{1}", sagaHandler.GetType().Name, sagaId);
                }
            }
            else
            {
                sagaHandler.BeforeSave();
                if (!found)
                {
                    SagaStateRepo.InsertNew(sagaHandler.Id, sagaState);
                    log.Info("Saved new saga {0}/{1}", sagaHandler.GetType().Name, sagaId);
                }
                else
                {
                    SagaStateRepo.Update(sagaHandler.Id, sagaState, version);
                    log.Debug("Updated saga {0}/{1}", sagaHandler.GetType().Name, sagaId);
                }
            }
            st.Stop();
            statLog.Info("Dispatch:{0}", st.ElapsedMilliseconds);
        }
        protected void DispatchToSagaInternal(string sagaId, bool createNew, SagaBase sagaHandler, Action<SagaBase> callback)
        {
            string version = null;
            bool found = false;
            object sagaState = null;
            var st = Stopwatch.StartNew();
            var sagaStateType = sagaHandler.GetType().BaseType.GetGenericArguments()[0]; //todo: what if inheritance is deeper???

            if (!string.IsNullOrEmpty(sagaId))
            {
                found = SagaStateRepo.Get(sagaId, sagaStateType, true, out sagaState, out version);
            }
            else //create new id
            {
                sagaId = Guid.NewGuid().ToString("N");
            }

            if (!found)
            {
                if (!createNew) throw new Exception("Saga instance not found: " + sagaId);
                sagaState = Activator.CreateInstance(sagaStateType);

                version = "1";
            }
            sagaHandler.IsNew = !found;
            sagaHandler.InitializeSagaState(sagaId, version, sagaState);
            callback(sagaHandler);
            if (sagaHandler.Completed)
            {
                if (!createNew)
                {
                    SagaStateRepo.Delete(sagaId);
                    log.Info("Deleted saga {0}/{1}", sagaHandler.GetType().Name, sagaId);
                }
            }
            else
            {
                sagaHandler.BeforeSave();
                if (!found)
                {
                    SagaStateRepo.InsertNew(sagaHandler.Id, sagaState);
                    log.Info("Saved new saga {0}/{1}", sagaHandler.GetType().Name, sagaId);
                }
                else
                {
                    SagaStateRepo.Update(sagaHandler.Id, sagaState, version);
                    log.Debug("Updated saga {0}/{1}", sagaHandler.GetType().Name, sagaId);
                }
            }
            st.Stop();
            statLog.Info("Dispatch:{0}", st.ElapsedMilliseconds);
        }