Click here to Skip to main content
12,076,727 members (45,849 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Data » Downloads

Stats

51K views
1.1K downloads
127 bookmarked
Posted

Web Application State

, 15 Apr 2008 CPOL
This article explains how to maintain the web application state.
sample.zip
State
AndrewGolik.Training
AndrewGolik.Training.csproj.user
bin
Debug
AndrewGolik.Training.dll
AndrewGolik.Training.ExceptionHandling.dll
Microsoft.SqlServer.BatchParser.dll
Microsoft.SqlServer.Replication.dll
Bll
Components
Storage
Dal
obj
Debug
AndrewGolik.Training.dll
AndrewGolik.Training.ExceptionHandling.dll
AndrewGolik.Training.ExceptionHandling.pdb
AndrewGolik.Training.pdb
ResolveAssemblyReference.cache
Properties
Lib
nunit.framework.dll
Web
App_Data
State.mdb
App_Themes
Main
Bin
AndrewGolik.Training.dll
AndrewGolik.Training.ExceptionHandling.dll
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using AndrewGolik.Training.Components;
using AndrewGolik.Training.Components.Storage;

namespace AndrewGolik.Training.Dal
{
    public class DaoStorage<T> : IStorage<T> where T : class
    {
        private static string Key(object key)
        {
            return TypeKey + "_" + key;
        }

        private static string TypeKey
        {
            get { return typeof (T).FullName; }
        }


        private void Update(OleDbConnection connection, object key, string value)
        {
            string insertSql = "update Heap set PropertyKey = ? , PropertyValue = ?";
            OleDbCommand command = new OleDbCommand(insertSql, connection);
            command.Parameters.Add(new OleDbParameter("PropertyKey", Key(key)));
            command.Parameters.Add(new OleDbParameter("PropertyValue", value));
            command.ExecuteNonQuery();
        }

        private static void Insert(OleDbConnection connection, object key, string value)
        {
            string insertSql = "INSERT INTO Heap(PropertyKey, PropertyValue) Values( ? , ?)";
            OleDbCommand command = new OleDbCommand(insertSql, connection);
            command.Parameters.Add(new OleDbParameter("PropertyKey", Key(key)));
            command.Parameters.Add(new OleDbParameter("PropertyValue", value));
            command.ExecuteNonQuery();
        }


        private static bool IsExist(OleDbConnection connection, object key)
        {
            string insertSql = "SELECT ID FROM Heap WHERE PropertyKey = ?";
            OleDbCommand command = new OleDbCommand(insertSql, connection);
            command.Parameters.Add(new OleDbParameter("PropertyKey", Key(key)));
            return command.ExecuteScalar() == null ? false : true;
        }

        private static void Delete(OleDbConnection connection, object key)
        {
            string deleteSql = "DELETE from Heap where PropertyKey= ? ";
            OleDbCommand command = new OleDbCommand(deleteSql, connection);
            command.Parameters.Add(new OleDbParameter("PropertyKey", Key(key)));
            command.ExecuteNonQuery();
        }


        private static string Get(OleDbConnection connection, object key)
        {
            string selectSql = "Select PropertyValue from Heap where PropertyKey = ? ";
            OleDbCommand command = new OleDbCommand(selectSql, connection);
            command.Parameters.Add(new OleDbParameter("PropertyKey", Key(key)));
            return (string) command.ExecuteScalar();
        }


        private static void Clear(OleDbConnection connection)
        {
            string deleteSql = "DELETE FROM HEAP WHERE PropertyKey LIKE '?*'";
            OleDbCommand command = new OleDbCommand(deleteSql, connection);
            command.Parameters.Add(new OleDbParameter("PropertyKey", TypeKey));
            command.ExecuteNonQuery();
        }


        private static List<T> GetAll(OleDbConnection connection)
        {
            List<T> list = new List<T>();
            string selectSql = " SELECT * FROM HEAP WHERE  PropertyKey LIKE ?+'%'";
            OleDbCommand command = new OleDbCommand(selectSql, connection);
            command.Parameters.Add(new OleDbParameter("PropertyKey", TypeKey));
            using (OleDbDataReader dataReader = command.ExecuteReader())
            {
                while (dataReader.Read())
                {
                    string obj = (string) dataReader["PropertyValue"];
                    list.Add(Serializer<T>.Deserialize(obj));
                }
            }
            return list;
        }


        public void Save(object key, T obj)
        {
            string value = Serializer<T>.Serialize(obj);

            using (OleDbConnection connection = new OleDbConnection(Config.ConnectionString))
            {
                connection.Open();
                if (IsExist(connection, key))
                    Update(connection, key, value);
                else
                    Insert(connection, key, value);
            }
        }


        public T Get(object key)
        {
            using (OleDbConnection connection = new OleDbConnection(Config.ConnectionString))
            {
                connection.Open();
                string obj = Get(connection, key);
                return Serializer<T>.Deserialize(obj);
            }
        }

        public void Delete(object key)
        {
            using (OleDbConnection connection = new OleDbConnection(Config.ConnectionString))
            {
                connection.Open();
                Delete(connection, key);
            }
        }


        public List<T> GetAll(Type type)
        {
            using (OleDbConnection connection = new OleDbConnection(Config.ConnectionString))
            {
                connection.Open();
                return GetAll(connection);
            }
        }


        public void Clear()
        {
            using (OleDbConnection connection = new OleDbConnection(Config.ConnectionString))
            {
                connection.Open();
                Clear(connection);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andrew Golik
Web Developer
Belarus Belarus
Andrew Golik is a software professional working in Minsk, Belarus.
He enjoys design infrastructures based on object oriented paradigm. His programming experience includes ASP, ASP.NET, .NET, COM, JAVA, PHP, DHTML, AJAX, blah blah blah....

Andrew Golik's Blog

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160212.1 | Last Updated 15 Apr 2008
Article Copyright 2008 by Andrew Golik
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid