|
- using System;
- using System.Collections.Generic;
- using SUISS.Core.Utilities;
-
- namespace CIG
- {
- public class StorageDictionary
- {
- public StorageDictionary()
- {
- this._storage = new Dictionary<string, object>();
- }
-
- public StorageDictionary(Dictionary<string, object> storage)
- {
- this._storage = storage;
- }
-
- public bool Contains(string key)
- {
- return this._storage.ContainsKey(key);
- }
-
- public void Remove(string key)
- {
- this._storage.Remove(key);
- }
-
- public void Set(string key, string value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, bool value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, char value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, float value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, double value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, sbyte value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, byte value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, short value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, ushort value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, int value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, uint value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, long value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, ulong value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, decimal value)
- {
- this._storage[key] = value;
- }
-
- public void Set(string key, DateTime value)
- {
- this._storage[key] = value.ToBinary();
- }
-
- public void Set<T>(string key, T value) where T : ICanSerialize
- {
- this._storage[key] = value.Serialize()._storage;
- }
-
- public void SetOrRemove(string key, double? value)
- {
- if (value == null)
- {
- this._storage.Remove(key);
- }
- else
- {
- this.Set(key, value.Value);
- }
- }
-
- public void Set(string key, List<string> values)
- {
- this.SetList<string>(key, values);
- }
-
- public void Set(string key, List<bool> values)
- {
- this.SetList<bool>(key, values);
- }
-
- public void Set(string key, List<char> values)
- {
- this.SetList<char>(key, values);
- }
-
- public void Set(string key, List<float> values)
- {
- this.SetList<float>(key, values);
- }
-
- public void Set(string key, List<double> values)
- {
- this.SetList<double>(key, values);
- }
-
- public void Set(string key, List<sbyte> values)
- {
- this.SetList<sbyte>(key, values);
- }
-
- public void Set(string key, List<byte> values)
- {
- this.SetList<byte>(key, values);
- }
-
- public void Set(string key, List<short> values)
- {
- this.SetList<short>(key, values);
- }
-
- public void Set(string key, List<ushort> values)
- {
- this.SetList<ushort>(key, values);
- }
-
- public void Set(string key, List<int> values)
- {
- this.SetList<int>(key, values);
- }
-
- public void Set(string key, List<uint> values)
- {
- this.SetList<uint>(key, values);
- }
-
- public void Set(string key, List<long> values)
- {
- this.SetList<long>(key, values);
- }
-
- public void Set(string key, List<ulong> values)
- {
- this.SetList<ulong>(key, values);
- }
-
- public void Set(string key, List<decimal> values)
- {
- this.SetList<decimal>(key, values);
- }
-
- public void Set(string key, List<DateTime> values)
- {
- List<object> list = new List<object>();
- int count = values.Count;
- for (int i = 0; i < count; i++)
- {
- list.Add(values[i].ToBinary());
- }
- this._storage[key] = list;
- }
-
- public void Set(string key, Dictionary<string, string> values)
- {
- this.SetDictionary<string>(key, values);
- }
-
- public void Set(string key, Dictionary<string, long> values)
- {
- this.SetDictionary<long>(key, values);
- }
-
- public void Set(string key, Dictionary<string, int> values)
- {
- this.SetDictionary<int>(key, values);
- }
-
- public void Set<T>(string key, List<T> values, Func<T, StorageDictionary> serialize)
- {
- List<object> list = new List<object>();
- int count = values.Count;
- for (int i = 0; i < count; i++)
- {
- list.Add(serialize(values[i])._storage);
- }
- this._storage[key] = list;
- }
-
- public void Set<T>(string key, Dictionary<T, long> values) where T : struct, IConvertible
- {
- if (!typeof(T).IsEnum())
- {
- throw new ArgumentException("T must be an Enum.");
- }
- Dictionary<string, object> dictionary = new Dictionary<string, object>();
- foreach (KeyValuePair<T, long> keyValuePair in values)
- {
- dictionary.Add(Convert.ToInt32(keyValuePair.Key).ToString(), keyValuePair.Value);
- }
- this._storage[key] = dictionary;
- }
-
- private void SetList<T>(string key, List<T> values)
- {
- List<object> list = new List<object>();
- int count = values.Count;
- for (int i = 0; i < count; i++)
- {
- list.Add(values[i]);
- }
- this._storage[key] = list;
- }
-
- private void SetDictionary<T>(string key, Dictionary<string, T> values)
- {
- Dictionary<string, object> dictionary = new Dictionary<string, object>();
- foreach (KeyValuePair<string, T> keyValuePair in values)
- {
- dictionary.Add(keyValuePair.Key, keyValuePair.Value);
- }
- this._storage[key] = dictionary;
- }
-
- public TField Get<TField>(string key, TField defaultValue)
- {
- if (!this._storage.ContainsKey(key))
- {
- return defaultValue;
- }
- if (!(this._storage[key] is TField))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", (this._storage[key] != null) ? this._storage[key].GetType().Name : "null", key, typeof(TField).Name));
- }
- return (TField)((object)this._storage[key]);
- }
-
- public DateTime GetDateTime(string key, DateTime defaultValue)
- {
- if (!this._storage.ContainsKey(key))
- {
- return defaultValue;
- }
- if (!(this._storage[key] is long))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(long).Name));
- }
- return DateTime.FromBinary((long)this._storage[key]);
- }
-
- public List<DateTime> GetDateTimeList(string key)
- {
- if (!this._storage.ContainsKey(key))
- {
- return new List<DateTime>();
- }
- if (!(this._storage[key] is List<object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
- }
- List<DateTime> list = new List<DateTime>();
- List<object> list2 = (List<object>)this._storage[key];
- int count = list2.Count;
- for (int i = 0; i < count; i++)
- {
- if (!(list2[i] is long))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[]
- {
- (list2[i] != null) ? list2[i].GetType().Name : "null",
- key,
- i,
- typeof(long).Name
- }));
- }
- list.Add(DateTime.FromBinary((long)list2[i]));
- }
- return list;
- }
-
- public StorageDictionary GetStorageDict(string key)
- {
- if (!this._storage.ContainsKey(key))
- {
- return new StorageDictionary();
- }
- if (!(this._storage[key] is Dictionary<string, object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
- }
- return new StorageDictionary((Dictionary<string, object>)this._storage[key]);
- }
-
- public List<TField> GetList<TField>(string key)
- {
- if (!this._storage.ContainsKey(key))
- {
- return new List<TField>();
- }
- if (!(this._storage[key] is List<object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
- }
- List<TField> list = new List<TField>();
- List<object> list2 = (List<object>)this._storage[key];
- int count = list2.Count;
- for (int i = 0; i < count; i++)
- {
- if (!(list2[i] is TField))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[]
- {
- (list2[i] != null) ? list2[i].GetType().Name : "null",
- key,
- i,
- typeof(TField).Name
- }));
- }
- list.Add((TField)((object)list2[i]));
- }
- return list;
- }
-
- public Dictionary<string, TField> GetDictionary<TField>(string key)
- {
- if (!this._storage.ContainsKey(key))
- {
- return new Dictionary<string, TField>();
- }
- if (!(this._storage[key] is Dictionary<string, object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary<string, object>).Name));
- }
- Dictionary<string, TField> dictionary = new Dictionary<string, TField>();
- Dictionary<string, object> dictionary2 = (Dictionary<string, object>)this._storage[key];
- foreach (KeyValuePair<string, object> keyValuePair in dictionary2)
- {
- if (!(dictionary2[keyValuePair.Key] is TField))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(TField).Name));
- }
- dictionary.Add(keyValuePair.Key, (TField)((object)dictionary2[keyValuePair.Key]));
- }
- return dictionary;
- }
-
- public Dictionary<TKey, TValue> GetDictionary<TKey, TValue>(string key) where TKey : struct, IConvertible
- {
- if (!typeof(TKey).IsEnum())
- {
- throw new ArgumentException("T must be an Enum.");
- }
- if (!this._storage.ContainsKey(key))
- {
- return new Dictionary<TKey, TValue>();
- }
- if (!(this._storage[key] is Dictionary<string, object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary<string, object>).Name));
- }
- Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
- Dictionary<string, object> dictionary2 = (Dictionary<string, object>)this._storage[key];
- foreach (KeyValuePair<string, object> keyValuePair in dictionary2)
- {
- if (!Enum.IsDefined(typeof(TKey), Convert.ToInt32(keyValuePair.Key)))
- {
- throw new InvalidOperationException(string.Format("{0} is not an underlying value of the {1} enum", keyValuePair.Key, typeof(TKey).Name));
- }
- if (!(dictionary2[keyValuePair.Key] is TValue))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(TValue).Name));
- }
- dictionary.Add((TKey)((object)Enum.Parse(typeof(TKey), keyValuePair.Key)), (TValue)((object)dictionary2[keyValuePair.Key]));
- }
- return dictionary;
- }
-
- public Dictionary<string, TModel> GetDictionaryModels<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
- {
- Dictionary<string, Dictionary<string, object>> dictionary = this.GetDictionary<Dictionary<string, object>>(key);
- Dictionary<string, TModel> dictionary2 = new Dictionary<string, TModel>();
- foreach (KeyValuePair<string, Dictionary<string, object>> keyValuePair in dictionary)
- {
- dictionary2.Add(keyValuePair.Key, factoryMethod(new StorageDictionary(keyValuePair.Value)));
- }
- return dictionary2;
- }
-
- public Dictionary<string, TModel> GetDictionaryModelsWithNulls<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
- {
- if (!this._storage.ContainsKey(key))
- {
- return new Dictionary<string, TModel>();
- }
- if (!(this._storage[key] is Dictionary<string, object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
- }
- Dictionary<string, object> dictionary = (Dictionary<string, object>)this._storage[key];
- Dictionary<string, TModel> dictionary2 = new Dictionary<string, TModel>();
- foreach (KeyValuePair<string, object> keyValuePair in dictionary)
- {
- if (keyValuePair.Value == null)
- {
- dictionary2.Add(keyValuePair.Key, default(TModel));
- }
- else
- {
- if (!(dictionary[keyValuePair.Key] is Dictionary<string, object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary<string, object>).Name));
- }
- dictionary2.Add(keyValuePair.Key, factoryMethod(new StorageDictionary((Dictionary<string, object>)keyValuePair.Value)));
- }
- }
- return dictionary2;
- }
-
- public List<TModel> GetModels<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
- {
- List<Dictionary<string, object>> list = this.GetList<Dictionary<string, object>>(key);
- List<TModel> list2 = new List<TModel>();
- int count = list.Count;
- for (int i = 0; i < count; i++)
- {
- list2.Add(factoryMethod(new StorageDictionary(list[i])));
- }
- return list2;
- }
-
- public List<TModel> GetModelsWithNulls<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
- {
- if (!(this._storage[key] is List<object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
- }
- List<TModel> list = new List<TModel>();
- List<object> list2 = (List<object>)this._storage[key];
- int count = list2.Count;
- for (int i = 0; i < count; i++)
- {
- if (list2[i] == null)
- {
- list.Add(default(TModel));
- }
- else
- {
- if (!(list2[i] is Dictionary<string, object>))
- {
- throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[]
- {
- list2[i].GetType().Name,
- key,
- i,
- typeof(Dictionary<string, object>).Name
- }));
- }
- list.Add(factoryMethod(new StorageDictionary((Dictionary<string, object>)list2[i])));
- }
- }
- return list;
- }
-
- public Dictionary<string, object> InternalDictionary
- {
- get
- {
- return this._storage;
- }
- }
-
- private Dictionary<string, object> _storage;
- }
- }
|