設定項目をAccessに保存・取得するシングルトン
かなウェルや波佐見弁利で、設定項目を永続化したいと思って作ったクラス。
Accessに保存する中間クラスとした。
キャッシュ機能つき。
Accessのテーブルは以下のよう。
settingsSingleton:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ABCS.DAOs; using ABCS.App_Data; namespace 波佐見弁利v2.DAOs { class settingsSingleton { #region //設定のキー public static string KeyUsePredictSocialIME = "KeyUsePredictSocialIME"; public static string KeyUseNormalSocialIME = "KeyUseNormalSocialIME"; public static string KeyUsePredictVJE = "KeyUsePredictVJE"; public static string KeyUseNormalVJE = "KeyUseNormalVJE"; #endregion #region //定数 public static string TRUE = "true"; public static string FALSE = "false"; #endregion public class values { public int intValue = 0; public string strgValue = ""; public values(int intvalue, string strgvalue) { this.intValue = intvalue; this.strgValue = strgvalue; } } private static Dictionarycache_ = new Dictionary (); private static settingsSingleton sigleton_ = new settingsSingleton(); settingsSingleton() { } public settingsSingleton getInstance() { return settingsSingleton.sigleton_; } public static bool isExistsIntValue(string strgKey) { Dictionary c = settingsSingleton.cache_; if (c.ContainsKey(strgKey)) { values v = c[strgKey]; if (v != null) { if (v.intValue != Int32.MinValue) { return true; } else { return false; } } else { return false; } } else { return false; } } public static bool isExistsStringValue(string strgKey) { Dictionary c = settingsSingleton.cache_; if (c.ContainsKey(strgKey)) { values v = c[strgKey]; if (v != null) { if (v.strgValue != null) { return true; } else { return false; } } else { return false; } } else { return false; } } public static void getValue(string strgKey, out int intValue) { intValue = 0; if (!settingsSingleton.isExistsIntValue(strgKey)) { //キャッシュになし //DBから読み込む using (DAOContext con = new DAOContext(AccessConString.mainConString)) { con.OpenConnection(); settingsDAO dao = new settingsDAO(con); dao.selectSetting(strgKey, out intValue); con.CloseConnection(); settingsSingleton.cache_[strgKey] = new values(intValue, null); } } else { //キャッシュにあり //キャッシュの値を返す values v = settingsSingleton.cache_[strgKey]; intValue = v.intValue; } } public static void getValue(string strgKey, out string strgValue) { strgValue = ""; if (!settingsSingleton.isExistsStringValue(strgKey)) { //キャッシュになし //DBから読み込む using (DAOContext con = new DAOContext(AccessConString.mainConString)) { con.OpenConnection(); settingsDAO dao = new settingsDAO(con); dao.selectSetting(strgKey, out strgValue); con.CloseConnection(); settingsSingleton.cache_[strgKey] = new values(Int32.MinValue, strgValue); } } else { //キャッシュにあり //キャッシュの値を返す values v = settingsSingleton.cache_[strgKey]; strgValue = v.strgValue; } } public static bool isEqualsIntValue(values v, int intValue) { if (v != null) { if (v.intValue == intValue) { return true; } else { return false; } } else { return false; } } public static bool isEqualsStringValue(values v, string strgValue) { if (v != null) { if (v.strgValue.Equals(strgValue)) { return true; } else { return false; } } else { return false; } } public static void setValue(string strgKey, int intValue) { using (DAOContext con = new DAOContext(AccessConString.mainConString)) { con.OpenConnection(); settingsDAO dao = new settingsDAO(con); dao.mergeSettings(strgKey, intValue); con.CloseConnection(); } settingsSingleton.cache_[strgKey] = new values(intValue, null); } public static void setValue(string strgKey, string strgValue) { using (DAOContext con = new DAOContext(AccessConString.mainConString)) { con.OpenConnection(); settingsDAO dao = new settingsDAO(con); dao.mergeSettings(strgKey, strgValue); con.CloseConnection(); } settingsSingleton.cache_[strgKey] = new values(Int32.MinValue, strgValue); } } }
シングルトンで使っているDAO。
settingsDAO:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using ABCS.DAOs; namespace ABCS.DAOs { class settingsDAO : MyDAOBase { public settingsDAO(DAOContext con) : base(con) { } public void selectSetting(string strgKey, out int intValue) { string sql = @"select * from settings where SETTINGKEY like @pkey"; this.ClearParameters(); this.AddParameter("pkey", DbType.String, strgKey); DataTable tbl = base.GetTable(sql); if (tbl.Rows.Count > 0) { int ret = 0; int.TryParse(tbl.Rows[0]["INTVALUE"].ToString(), out ret); intValue = ret; } else { intValue = 0; } } public void selectSetting(string strgKey, out string strgValue) { string sql = @"select * from settings where SETTINGKEY like @pkey"; this.ClearParameters(); this.AddParameter("pkey", DbType.String, strgKey); DataTable tbl = base.GetTable(sql); if (tbl.Rows.Count > 0) { strgValue = tbl.Rows[0]["TEXTVALUE"].ToString(); } else { strgValue = ""; } } public bool isExistSetting(string strgKey) { string sql = @"select * from settings where SETTINGKEY like @pkey"; this.ClearParameters(); this.AddParameter("pkey", DbType.String, strgKey); DataTable tbl = base.GetTable(sql); if (tbl.Rows.Count > 0) { return true; } else { return false; } } #region //数値設定 public int updateDate(string strgKey, int intValue) { string sql = @"update settings set INTVALUE = @pintvalue where SETTINGKEY like @pkey"; this.ClearParameters(); this.AddParameter("pintvalue", DbType.Int32, intValue); this.AddParameter("pkey", DbType.String, strgKey); return base.ExecuteNonQuery(sql); } public int insertNew(string strgKey, int intValue) { string sql = @"insert into settings( SETTINGKEY ,INTVALUE ) values( @pkey ,@pintvalue )"; this.ClearParameters(); this.AddParameter("pkey", DbType.String, strgKey); this.AddParameter("pintvalue", DbType.Int32, intValue); return base.ExecuteNonQuery(sql); } public int mergeSettings(string strgKey, int intValue) { if (this.isExistSetting(strgKey)) { return this.updateDate(strgKey, intValue); } else { return this.insertNew(strgKey, intValue); } } #endregion #region //文字設定 public int updateDate(string strgKey, string strgValue) { string sql = @"update settings set TEXTVALUE = @ptextvalue where SETTINGKEY like @pkey"; this.ClearParameters(); this.AddParameter("ptextvalue", DbType.String, strgValue); this.AddParameter("pkey", DbType.String, strgKey); return base.ExecuteNonQuery(sql); } public int insertNew(string strgKey, string strgValue) { string sql = @"insert into settings( SETTINGKEY ,TEXTVALUE ) values( @ppkey ,@pptextvalue )"; this.ClearParameters(); this.AddParameter("ppkey", DbType.String, strgKey); this.AddParameter("pptextvalue", DbType.String, strgValue); return base.ExecuteNonQuery(sql); } public int mergeSettings(string strgKey, string strgValue) { if (this.isExistSetting(strgKey)) { return this.updateDate(strgKey, strgValue); } else { return this.insertNew(strgKey, strgValue); } } #endregion } }
上のDAOのベースクラス。
MyDAOBase:
using System; using System.Collections.Generic; using System.Data; using System.Data.Common; namespace ABCS.DAOs { public class DAOContext : IDisposable { private DbConnection con_ = null; private DbProviderFactory fac_ = null; private DbTransaction tran_ = null; public DbConnection Connection { get { return this.con_; } private set { this.con_ = value; } } public DbProviderFactory Factory { get { return this.fac_; } private set { this.fac_ = value; } } public DbTransaction Transaction { get { return this.tran_; } private set { this.tran_ = value; } } public DAOContext() : this("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|abc.mdb") { } public DAOContext(string connectionString) : this("System.Data.OleDb", connectionString) { } public DAOContext(string providerName, string connectionString) { if (providerName == null) throw new ArgumentNullException("providerName"); if (connectionString == null) throw new ArgumentNullException("connectionString"); this.Factory = DbProviderFactories.GetFactory(providerName); this.Connection = this.Factory.CreateConnection(); this.Connection.ConnectionString = connectionString; } public void OpenConnection() { if (this.Connection == null) throw new InvalidOperationException("Object is diposed."); if (this.Connection.State == ConnectionState.Closed) this.Connection.Open(); } public void CloseConnection() { if (this.Connection == null) throw new InvalidOperationException("Object is diposed."); if (this.Connection.State == ConnectionState.Open) this.Connection.Close(); } public void BeginTransaction(IsolationLevel isolationLevel) { if (this.Transaction != null) throw new InvalidOperationException("There is already a transaction."); this.OpenConnection(); this.Transaction = this.Connection.BeginTransaction(isolationLevel); } public void CommitTransaction() { if (this.Transaction == null) throw new InvalidOperationException("There is no transaction."); this.Transaction.Commit(); this.Transaction = null; } public void RollbackTransaction() { if (this.Transaction == null) throw new InvalidOperationException("There is no transaction."); this.Transaction.Rollback(); this.Transaction = null; } public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (disposing) { try { if (this.Transaction != null) { this.RollbackTransaction(); this.Transaction = null; } } finally { if (this.Connection != null) { this.Connection.Dispose(); this.Connection = null; } } } } } public class MyDAOBase { private readonly Listparameters = new List (); private DAOContext context_ = null; public DAOContext Context { get { return this.context_; } private set { this.context_ = value; } } public IList Parameters { get { return this.parameters; } } public MyDAOBase(DAOContext context) { if (context == null) throw new ArgumentNullException("context"); this.Context = context; } public void ClearParameters() { this.parameters.Clear(); } public void AddParameter(string parameterName, DbType dbType, object value) { DbParameter p = this.Context.Factory.CreateParameter(); p.ParameterName = parameterName; p.DbType = dbType; p.Value = value; this.parameters.Add(p); } protected void ExecuteCommand(string commandText, IEnumerable parameters, Action action) { DbCommand command = this.Context.Factory.CreateCommand(); command.CommandText = commandText; command.Connection = this.Context.Connection; command.Transaction = this.Context.Transaction; foreach (DbParameter parameter in parameters) command.Parameters.Add(parameter); action(command); } protected int ExecuteNonQuery(string commandText, IEnumerable parameters) { int count = 0; this.ExecuteCommand(commandText, parameters, delegate(DbCommand command){ count = command.ExecuteNonQuery(); }); return count; } protected int ExecuteNonQuery(string commandText) { return this.ExecuteNonQuery(commandText, this.parameters); } protected DataTable GetTable(string commandText, IEnumerable parameters) { DataTable table = new DataTable(); this.ExecuteCommand(commandText, parameters, delegate(DbCommand command) { using (DbDataReader reader = command.ExecuteReader()) { table.Load(reader); } }); return table; } protected DataTable GetTable(string commandText) { return this.GetTable(commandText, this.Parameters); } } }
Accessの接続文字列を保持するクラス。
AccessConString:
using System; using System.Collections.Generic; using System.Text; namespace ABCS.App_Data { public class AccessConString { public static readonly string ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|App_Data\\abc.mdb"; public static readonly string mainConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|App_Data\\main.mdb"; } }