⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datastore.cs

📁 微软的行业应用解决方案示例
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace Microsoft.Mobile.Data
{
    /// <summary>
    /// This class unifies data access so that a single interface can be used 
    /// to access data from different databases, flat files, XML and other 
    /// sources of row column data.
    /// </summary>
    public abstract class DataStore : IDisposable
    {
        #region Constants
        /// <summary>
        /// Name of the Sql Server CE Provider
        /// </summary>
        [SuppressMessage("Microsoft.Naming",
                         "CA1709:IdentifiersShouldBeCasedCorrectly",
                         MessageId = "SSCE",
                         Justification = "SSCE is the abbreviated product name.")]
        public const string SSCEProvider = "Microsoft.Mobile.Data.SqlServerCE.SSCEDataStore";
        #endregion


        #region Fields
        private static Dictionary<string, DataStore> _dataStores = new Dictionary<string, DataStore>();
        private static Parameters _defaultParameters = new Parameters();
        private static string _defaultProvider = SSCEProvider;
        private Parameters _implementationParameters;
        #endregion


        #region Constructor(s) & Dispose
        /// <summary>
        /// DataStore construction signature required by all data stores.
        /// </summary>
        /// <param name="connectionString">
        /// string containing connection information
        /// </param>
        protected DataStore(string connectionString) : 
            this(connectionString, _defaultParameters)
        {
        }


        /// <summary>
        /// Optional DataStore construction signature which allows for 
        /// implementation specific parameters to be passed to a specific
        /// implementation.
        /// </summary>
        /// <param name="connectionString">
        /// string containing connection information
        /// </param>
        /// <param name="implementationParameters">
        /// implementation specific additional parameters
        /// </param>
        protected DataStore(string connectionString,
                            Parameters implementationParameters) 
        {
            if (string.IsNullOrEmpty(connectionString) ||
                string.IsNullOrEmpty(connectionString.Trim()))
            {
                throw new ArgumentException("Connection string cannot be null or empty.",
                                            "connectionString");
            }
            _implementationParameters = implementationParameters;
        }


        /// <summary>
        /// Destructor for DataStore to only be used by Finalizer
        /// </summary>
        ~DataStore()
        {
            Dispose(false);
        }


        /// <summary>
        /// Dispose of any resources associated with the DataStore 
        /// implementation.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);

            // This object will be cleaned up by the Dispose method. Therefore, 
            // you should call GC.SupressFinalize to take this object off the 
            // finalization queue and prevent finalization code for this object
            // from executing a second time.
            GC.SuppressFinalize(this);
        }


        /// <summary>
        /// Actual dispose implementation. Used to prevent dispose executing 
        /// twice.
        /// </summary>
        /// <remarks>
        /// Dispose(bool disposing) executes in two distinct scenarios. If 
        /// disposing equals true, the method has been called directly or 
        /// indirectly by a user's code. Managed and unmanaged resources can be 
        /// disposed.
        /// 
        /// If disposing equals false, the method has been called by the 
        /// runtime from inside the finalizer and you should not reference 
        /// other objects. Only unmanaged resources can be disposed.
        /// </remarks>
        /// <param name="disposing">
        /// set to true when being called by dispose otherwise false.
        /// </param>
        protected abstract void Dispose(bool disposing);
        #endregion


        #region Properties
        /// <summary>
        /// Default implementation parameters to use if none are provided.
        /// </summary>
        [SuppressMessage("Microsoft.Usage", 
                         "CA2227:CollectionPropertiesShouldBeReadOnly",
                         Justification="Parameter collection can be changed.")]
        public static Parameters DefaultParameters
        {
            get
            {
                return _defaultParameters;
            }
            set
            {
                _defaultParameters = value;
            }
        }


        /// <summary>
        /// Default data store provider to use
        /// </summary>
        public static string DefaultProvider
        {
            get
            {
                return _defaultProvider;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ArgumentException("DefaultProvider cannot be set to an empty or null string.");
                }

                _defaultProvider = value;
            }
        }


        /// <summary>
        /// Used to determine is there are any TriggerError event subscribers. 
        /// This is required since only DataStore can manipulate the event 
        /// delegate.
        /// </summary>
        protected bool HasTriggerErrorSubscribers
        {
            get
            {
                return (TriggerError != null) &&
                       (TriggerError.GetInvocationList().Length > 0);
            }
        }


        /// <summary>
        /// Used to determine is there are any Trigger event subscribers. This 
        /// is required since only DataStore can manipulate the event delegate.
        /// </summary>
        protected bool HasTriggerSubscribers
        {
            get
            {
                return (Trigger != null) && 
                       (Trigger.GetInvocationList().Length > 0);
            }
        }


        /// <summary>
        /// Implementation specific arguments that are passed into the 
        /// <see cref="Microsoft.Mobile.Data.DataStore"/> when it is created.
        /// </summary>
        protected Parameters ImplementationParameters
        {
            get
            {
                return _implementationParameters;
            }
        }
        #endregion


        #region Methods
        /// <summary>
        /// Start a transaction.
        /// </summary>
        public abstract void BeginTransaction();


        /// <summary>
        /// Close connection. When used the connection will reopen.
        /// </summary>
        public abstract void Close();


        /// <summary>
        /// Method for committing changes changes 
        /// </summary>
        public abstract void Commit();


        /// <summary>
        /// This method creates a data store of the type passed into the 
        /// method. In creating the datastore it must construct it with a 
        /// connecion string.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -