00001 using System;
00002 using System.Collections.Generic;
00003 using Foodolini.Database;
00004
00008 namespace Foodolini.BusinessLogic
00009 {
00013 public class Settings : IDisposable
00014 {
00015 private Repository repository = null;
00016
00017 private Settings(){
00018 }
00019
00023 internal Repository Repository{
00024 get{
00025 if(this.repository == null)
00026 throw new ObjectDisposedException("Object has been disposed or you are not connected to a database.");
00027 return this.repository;
00028 }
00029 }
00030
00034 public string this[string key]{
00035 get{
00036 var setting = this.repository.SingleWhere<Setting>("Key = @0", key);
00037 if(setting == null)
00038 return null;
00039 return setting.Value;
00040 }
00041 set{
00042 var setting = this.repository.SingleWhere<Setting>("Key = @0", key);
00043 if(setting == null){
00044 setting = new Setting();
00045 setting.Key = key;
00046 setting.Value = value;
00047 this.repository.Add(setting);
00048 }else{
00049 setting.Value = value;
00050 this.repository.Update(setting);
00051 }
00052 }
00053 }
00054
00058 public string GetValue(string key){
00059 return this[key];
00060 }
00061
00065 public string GetValue(string key, string @default){
00066 string retval = this[key];
00067 if(retval == null)
00068 return @default;
00069 return retval;
00070 }
00071
00076 public void Save(){
00077 this.repository.Commit();
00078 }
00079
00086 public void OpenSqliteDatabase(string database){
00087 if(this.repository != null)
00088 this.repository.Dispose();
00089 this.repository = Repository.ConnectSqlite(database);
00090 }
00091
00101 public void OpenSqliteDatabase(string database, bool disableCommit){
00102 if(this.repository != null)
00103 this.repository.Dispose();
00104 this.repository = Repository.ConnectSqlite(database, disableCommit);
00105 }
00106
00107 private static Settings instance = null;
00108 public static Settings Instance{
00109 get{
00110 if(instance == null || instance.repository == null)
00111 instance = new Settings();
00112 return instance;
00113 }
00114 }
00115
00119 internal static Repository Repo{
00120 get{
00121 return Settings.Instance.Repository;
00122 }
00123 }
00124
00125 #region IDisposable Implementation
00126
00127
00128
00140 protected virtual void Dispose(bool disposing){
00141 if(disposing){
00142
00143 if(this.repository != null)
00144 this.repository.Dispose();
00145 this.repository = null;
00146 }
00147 }
00148
00152 public void Dispose(){
00153
00154 this.Dispose(true);
00155 GC.SuppressFinalize(this);
00156 }
00157
00161 ~Settings(){
00162
00163
00164 this.Dispose(false);
00165 }
00166 #endregion
00167
00174 public void Close(){
00175 this.Dispose();
00176 }
00177 }
00178 }