Click here to Skip to main content
12,450,981 members (24,971 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » Data » Downloads

Stats

52.6K views
1.1K downloads
127 bookmarked
Posted

Web Application State

, 15 Apr 2008 CPOL
This article explains how to maintain the web application state.
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.IO;

namespace AndrewGolik.Training.Components.Storage
{
    public class FileStorage<T> : IStorage<T> where T : class
    {
        private static object sync = new object();

        private const string EXTENSION = ".instance";

        private static string TypeStoragePath
        {
            get
            {
                string typeKey = typeof (T).FullName;
                return Config.DirectoryStoragePath + @"/" + typeKey;
            }
        }

        private static string FilePath(object key)
        {
            return TypeStoragePath + @"/" + key + EXTENSION;
        }


        public void Save(object key, T obj)
        {
            lock (sync)
            {
                string xml = Serializer<T>.Serialize(obj);
                if (!Directory.Exists(TypeStoragePath))
                    Directory.CreateDirectory(TypeStoragePath);
                using (StreamWriter writer = new StreamWriter(FilePath(key), false))
                {
                    writer.Write(xml);
                }
            }
        }

        private T Deserialize(string filePath)
        {
            if (!File.Exists(filePath))
                return null;
            using (StreamReader reader = new StreamReader(filePath))
            {
                return Serializer<T>.Deserialize(reader.ReadToEnd());
            }
        }

        public T Get(object key)
        {
            return Deserialize(FilePath(key));
        }

        public void Delete(object key)
        {
            File.Delete(FilePath(key));
        }


        public List<T> GetAll(Type type)
        {
            List<T> list = new List<T>();

            if (!Directory.Exists(TypeStoragePath))
                return list;

            string[] files = Directory.GetFiles(TypeStoragePath, "*" + EXTENSION);

            foreach (string file in files)
            {
                list.Add(Deserialize(file));
            }
            return list;
        }


        public void Clear()
        {
            Directory.Delete(TypeStoragePath);
        }
    }
}

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...

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