IsolatedStorageStateManager.cs :  » Windows-Presentation-Foundation » Caliburn » Caliburn » PresentationFramework » ApplicationModel » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Windows Presentation Foundation » Caliburn 
Caliburn » Caliburn » PresentationFramework » ApplicationModel » IsolatedStorageStateManager.cs
namespace Caliburn.PresentationFramework.ApplicationModel{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Xml;
    using Core;
    using Core.Logging;

    /// <summary>
    /// An implementation of <see cref="IStateManager"/> that uses isolated storage as its backing store.
    /// </summary>
    public class IsolatedStorageStateManager : PropertyChangedBase, IStateManager
    {
        private static readonly ILog Log = LogManager.GetLog(typeof(IsolatedStorageStateManager));
        private readonly Dictionary<string, string> _state = new Dictionary<string, string>();
        private string _stateName;

        /// <summary>
        /// Occurs after the state was loaded from the backing store.
        /// </summary>
        public event EventHandler AfterStateLoad = delegate { };

        /// <summary>
        /// Occurs before the state is committed to the backing store.
        /// </summary>
        public event EventHandler BeforeStateCommit = delegate { };

        /// <summary>
        /// Initializes the backing store.
        /// </summary>
        /// <param name="stateName">Name of the state.</param>
        /// <returns></returns>
        public virtual bool Initialize(string stateName)
        {
            if(string.IsNullOrEmpty(stateName))
            {
                var ex = new CaliburnException("State name is null or empty.");
                Log.Error(ex);
                throw ex;
            }

            _stateName = stateName;

            return LoadState();
        }

        private bool LoadState()
        {
            try
            {
                Log.Info("Loading state.");

                using(var store = GetStorageFile())
                using(var stream = new IsolatedStorageFileStream(_stateName, FileMode.Open, store))
                using(var reader = XmlReader.Create(stream))
                {
                    if(reader.ReadToFollowing("State"))
                    {
                        int stateDepth = reader.Depth;
                        reader.Read();

                        while(reader.Depth > stateDepth)
                        {
                            var key = reader.GetAttribute("Key");
                            var value = reader.ReadElementContentAsString();

                            _state[key] = value;
                        }
                    }
                }

                AfterStateLoad(this, EventArgs.Empty);
                return true;
            }
            catch(Exception ex)
            {
                Log.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// Commits the changes.
        /// </summary>
        /// <param name="stateName">Name of the state.</param>
        /// <returns></returns>
        public virtual bool CommitChanges(string stateName)
        {
            BeforeStateCommit(this, EventArgs.Empty);

            _stateName = stateName ?? _stateName;

            try
            {
                Log.Info("Committing state.");

                using(var store = GetStorageFile())
                using(var stream = new IsolatedStorageFileStream(_stateName, FileMode.OpenOrCreate, store))
                using(var writer = XmlWriter.Create(stream))
                {
                    writer.WriteStartDocument();
                    writer.WriteStartElement("State");

                    foreach(var pair in _state)
                    {
                        writer.WriteStartElement("Item");
                        writer.WriteAttributeString("Key", pair.Key);
                        writer.WriteString(pair.Value);
                        writer.WriteEndElement();
                    }

                    writer.WriteEndDocument();
                }

                return true;
            }
            catch(Exception ex)
            {
                Log.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// Gets the storage file.
        /// </summary>
        /// <returns></returns>
        protected IsolatedStorageFile GetStorageFile()
        {
#if SILVERLIGHT
            return IsolatedStorageFile.GetUserStoreForApplication();
#else
            return IsolatedStorageFile.GetUserStoreForDomain();
#endif
        }

        /// <summary>
        /// Inserts or updates a value in the state.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public virtual void InsertOrUpdate(string key, string value)
        {
            _state[key] = value;
        }

        /// <summary>
        /// Gets the value with the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public virtual string Get(string key)
        {
            string value;
            _state.TryGetValue(key, out value);
            return value;
        }

        /// <summary>
        /// Removes the value with the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public virtual bool Remove(string key)
        {
            return _state.Remove(key);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.