Ejemplo n.º 1
0
        public Guid CreateInstance(int recordId, int requestedById)
        {
            //set properties
            InstanceId = Guid.NewGuid();
            RecordId = recordId;
            IsCompleted = false;
            setFirstState();
            CurrentState = FirstState;

            //wrap and save
            using (WorkflowEntities db = new WorkflowEntities())
            {
                Instance ins = new Instance()
                {
                    InstanceId = InstanceId,
                    WorkflowData = Wrap()
                };

                History hst = new History()
                {
                    FromState = "",
                    InstanceId = ins.InstanceId,
                    ToState = FirstState.Name,
                    TransitionBy = requestedById,
                    TransitionDate = DateTime.Now
                };

                db.Entry(ins).State = EntityState.Added;
                db.Entry(hst).State = EntityState.Added;
                db.SaveChanges();
                return ins.InstanceId;
            }
        }
Ejemplo n.º 2
0
        public bool TryAdvance(int requestedBy)
        {
            if (CurrentState.CanProgress())
            {
                string fromState = CurrentState.Name;
                CurrentState = CurrentState.GetNextState();
                IsCompleted = CurrentState.IsFinalState ? true : false;

                History hst = new History()
                {
                    FromState = fromState,
                    ToState = CurrentState.Name,
                    InstanceId = InstanceId,
                    TransitionBy = requestedBy,
                    TransitionDate = DateTime.Now
                };

                using (WorkflowEntities db = new WorkflowEntities())
                {
                    Instance ins = db.Instances.Single(x => x.InstanceId == this.InstanceId);
                    ins.WorkflowData = Wrap();
                    db.Entry(ins).State = EntityState.Modified;
                    db.Entry(hst).State = EntityState.Added;
                    db.SaveChanges();
                }

                return true;
            }
            return false;
        }