protected ViewstateContext GetFromDatabase(string query, object args)
        {
            SqlConnection conn = new SqlConnection(connectionstringProvider.Connectionstring);

            ViewstateDatabaseRow r;
            try
            {
                conn.Open();
                r = conn.ExecuteMapperQuery<ViewstateDatabaseRow>(query, args).FirstOrDefault();
            }
                catch(Exception ex)
                {
                    AnnotatedException e = new AnnotatedException("Error loading viewstate", ex);
                    e.AddAnnotation("Query", GetByGuidQuery);
                    e.AddAnnotations(args);

                    throw e;

                }
            finally
            {
                conn.Close();
            }

            if (r == null)
            {
                AnnotatedException e = new AnnotatedException("Unloading to load viewstate", new Exception("Empty (no data) in viewstate from database"));
                e.AddAnnotations(args);

                throw e;
            }

            return new ViewstateContext(r.ViewstateID, r.ViewData, r.Reference);
        }
        protected void DeleteViewstate(string query, object args)
        {
            SqlConnection conn = new SqlConnection( connectionstringProvider.Connectionstring );

            try
            {
                conn.Open();
                conn.ExecuteMapperCommand(query, args);
            }
            catch(Exception ex)
            {
                AnnotatedException e = new AnnotatedException("Could not delete viewstate from database", ex);
                e.AddAnnotation("Query", query);
                e.AddAnnotations(args);

                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        public void SetState(Guid viewStateID, int state)
        {
            SqlConnection conn = new SqlConnection( connectionstringProvider.Connectionstring );
            var args = new
                {
                    ViewStateID = viewStateID,
                    State=state
                };

            try
            {
                conn.Open();
                conn.ExecuteMapperCommand(SetStateQuery, args);
            }
            catch(Exception ex)
            {
                AnnotatedException e = new AnnotatedException("Error saving state for viewstate", ex);
                e.AddAnnotation("Query", SetStateQuery);
                e.AddAnnotations(args);

                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        protected void CreateState(Guid viewstateID)
        {
            SqlConnection conn = new SqlConnection(connectionstringProvider.Connectionstring);

            try
            {
                conn.Open();
                conn.ExecuteMapperCommand(CreateStateQuery, new {SessionID = viewstateID});
            }
            catch(Exception ex)
            {
                AnnotatedException e = new AnnotatedException("Could not create state", ex);
                e.AddAnnotation("ViewstateID", viewstateID);

                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
        public void SaveState(ViewstateContext ctx, bool useGuid = true)
        {
            if ( !useGuid && string.IsNullOrWhiteSpace( ctx.Reference ) )
                useGuid = true;

            string query = useGuid ? SaveStateByGuidQuery : SaveStateByReferenceQuery;
            var args = ctx.GetDatebaseRow();

            SqlConnection conn = new SqlConnection(connectionstringProvider.Connectionstring);
            try
            {
                conn.Open();
                conn.ExecuteMapperCommand(query, args);
            }
            catch(Exception ex)
            {
                AnnotatedException e = new AnnotatedException("Error saving viewstate", ex);
                e.AddAnnotation("UseGuid", useGuid);
                e.AddAnnotation("Query", query);
                e.AddAnnotations(args);

                throw e;
            }
            finally
            {
                conn.Close();
            }
        }