您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

513 行
14 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Core.Utilities;
  4. namespace CIG
  5. {
  6. public class StorageDictionary
  7. {
  8. public StorageDictionary()
  9. {
  10. this._storage = new Dictionary<string, object>();
  11. }
  12. public StorageDictionary(Dictionary<string, object> storage)
  13. {
  14. this._storage = storage;
  15. }
  16. public bool Contains(string key)
  17. {
  18. return this._storage.ContainsKey(key);
  19. }
  20. public void Remove(string key)
  21. {
  22. this._storage.Remove(key);
  23. }
  24. public void Set(string key, string value)
  25. {
  26. this._storage[key] = value;
  27. }
  28. public void Set(string key, bool value)
  29. {
  30. this._storage[key] = value;
  31. }
  32. public void Set(string key, char value)
  33. {
  34. this._storage[key] = value;
  35. }
  36. public void Set(string key, float value)
  37. {
  38. this._storage[key] = value;
  39. }
  40. public void Set(string key, double value)
  41. {
  42. this._storage[key] = value;
  43. }
  44. public void Set(string key, sbyte value)
  45. {
  46. this._storage[key] = value;
  47. }
  48. public void Set(string key, byte value)
  49. {
  50. this._storage[key] = value;
  51. }
  52. public void Set(string key, short value)
  53. {
  54. this._storage[key] = value;
  55. }
  56. public void Set(string key, ushort value)
  57. {
  58. this._storage[key] = value;
  59. }
  60. public void Set(string key, int value)
  61. {
  62. this._storage[key] = value;
  63. }
  64. public void Set(string key, uint value)
  65. {
  66. this._storage[key] = value;
  67. }
  68. public void Set(string key, long value)
  69. {
  70. this._storage[key] = value;
  71. }
  72. public void Set(string key, ulong value)
  73. {
  74. this._storage[key] = value;
  75. }
  76. public void Set(string key, decimal value)
  77. {
  78. this._storage[key] = value;
  79. }
  80. public void Set(string key, DateTime value)
  81. {
  82. this._storage[key] = value.ToBinary();
  83. }
  84. public void Set<T>(string key, T value) where T : ICanSerialize
  85. {
  86. this._storage[key] = value.Serialize()._storage;
  87. }
  88. public void SetOrRemove(string key, double? value)
  89. {
  90. if (value == null)
  91. {
  92. this._storage.Remove(key);
  93. }
  94. else
  95. {
  96. this.Set(key, value.Value);
  97. }
  98. }
  99. public void Set(string key, List<string> values)
  100. {
  101. this.SetList<string>(key, values);
  102. }
  103. public void Set(string key, List<bool> values)
  104. {
  105. this.SetList<bool>(key, values);
  106. }
  107. public void Set(string key, List<char> values)
  108. {
  109. this.SetList<char>(key, values);
  110. }
  111. public void Set(string key, List<float> values)
  112. {
  113. this.SetList<float>(key, values);
  114. }
  115. public void Set(string key, List<double> values)
  116. {
  117. this.SetList<double>(key, values);
  118. }
  119. public void Set(string key, List<sbyte> values)
  120. {
  121. this.SetList<sbyte>(key, values);
  122. }
  123. public void Set(string key, List<byte> values)
  124. {
  125. this.SetList<byte>(key, values);
  126. }
  127. public void Set(string key, List<short> values)
  128. {
  129. this.SetList<short>(key, values);
  130. }
  131. public void Set(string key, List<ushort> values)
  132. {
  133. this.SetList<ushort>(key, values);
  134. }
  135. public void Set(string key, List<int> values)
  136. {
  137. this.SetList<int>(key, values);
  138. }
  139. public void Set(string key, List<uint> values)
  140. {
  141. this.SetList<uint>(key, values);
  142. }
  143. public void Set(string key, List<long> values)
  144. {
  145. this.SetList<long>(key, values);
  146. }
  147. public void Set(string key, List<ulong> values)
  148. {
  149. this.SetList<ulong>(key, values);
  150. }
  151. public void Set(string key, List<decimal> values)
  152. {
  153. this.SetList<decimal>(key, values);
  154. }
  155. public void Set(string key, List<DateTime> values)
  156. {
  157. List<object> list = new List<object>();
  158. int count = values.Count;
  159. for (int i = 0; i < count; i++)
  160. {
  161. list.Add(values[i].ToBinary());
  162. }
  163. this._storage[key] = list;
  164. }
  165. public void Set(string key, Dictionary<string, string> values)
  166. {
  167. this.SetDictionary<string>(key, values);
  168. }
  169. public void Set(string key, Dictionary<string, long> values)
  170. {
  171. this.SetDictionary<long>(key, values);
  172. }
  173. public void Set(string key, Dictionary<string, int> values)
  174. {
  175. this.SetDictionary<int>(key, values);
  176. }
  177. public void Set<T>(string key, List<T> values, Func<T, StorageDictionary> serialize)
  178. {
  179. List<object> list = new List<object>();
  180. int count = values.Count;
  181. for (int i = 0; i < count; i++)
  182. {
  183. list.Add(serialize(values[i])._storage);
  184. }
  185. this._storage[key] = list;
  186. }
  187. public void Set<T>(string key, Dictionary<T, long> values) where T : struct, IConvertible
  188. {
  189. if (!typeof(T).IsEnum())
  190. {
  191. throw new ArgumentException("T must be an Enum.");
  192. }
  193. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  194. foreach (KeyValuePair<T, long> keyValuePair in values)
  195. {
  196. dictionary.Add(Convert.ToInt32(keyValuePair.Key).ToString(), keyValuePair.Value);
  197. }
  198. this._storage[key] = dictionary;
  199. }
  200. private void SetList<T>(string key, List<T> values)
  201. {
  202. List<object> list = new List<object>();
  203. int count = values.Count;
  204. for (int i = 0; i < count; i++)
  205. {
  206. list.Add(values[i]);
  207. }
  208. this._storage[key] = list;
  209. }
  210. private void SetDictionary<T>(string key, Dictionary<string, T> values)
  211. {
  212. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  213. foreach (KeyValuePair<string, T> keyValuePair in values)
  214. {
  215. dictionary.Add(keyValuePair.Key, keyValuePair.Value);
  216. }
  217. this._storage[key] = dictionary;
  218. }
  219. public TField Get<TField>(string key, TField defaultValue)
  220. {
  221. if (!this._storage.ContainsKey(key))
  222. {
  223. return defaultValue;
  224. }
  225. if (!(this._storage[key] is TField))
  226. {
  227. 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));
  228. }
  229. return (TField)((object)this._storage[key]);
  230. }
  231. public DateTime GetDateTime(string key, DateTime defaultValue)
  232. {
  233. if (!this._storage.ContainsKey(key))
  234. {
  235. return defaultValue;
  236. }
  237. if (!(this._storage[key] is long))
  238. {
  239. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(long).Name));
  240. }
  241. return DateTime.FromBinary((long)this._storage[key]);
  242. }
  243. public List<DateTime> GetDateTimeList(string key)
  244. {
  245. if (!this._storage.ContainsKey(key))
  246. {
  247. return new List<DateTime>();
  248. }
  249. if (!(this._storage[key] is List<object>))
  250. {
  251. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
  252. }
  253. List<DateTime> list = new List<DateTime>();
  254. List<object> list2 = (List<object>)this._storage[key];
  255. int count = list2.Count;
  256. for (int i = 0; i < count; i++)
  257. {
  258. if (!(list2[i] is long))
  259. {
  260. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[]
  261. {
  262. (list2[i] != null) ? list2[i].GetType().Name : "null",
  263. key,
  264. i,
  265. typeof(long).Name
  266. }));
  267. }
  268. list.Add(DateTime.FromBinary((long)list2[i]));
  269. }
  270. return list;
  271. }
  272. public StorageDictionary GetStorageDict(string key)
  273. {
  274. if (!this._storage.ContainsKey(key))
  275. {
  276. return new StorageDictionary();
  277. }
  278. if (!(this._storage[key] is Dictionary<string, object>))
  279. {
  280. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
  281. }
  282. return new StorageDictionary((Dictionary<string, object>)this._storage[key]);
  283. }
  284. public List<TField> GetList<TField>(string key)
  285. {
  286. if (!this._storage.ContainsKey(key))
  287. {
  288. return new List<TField>();
  289. }
  290. if (!(this._storage[key] is List<object>))
  291. {
  292. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
  293. }
  294. List<TField> list = new List<TField>();
  295. List<object> list2 = (List<object>)this._storage[key];
  296. int count = list2.Count;
  297. for (int i = 0; i < count; i++)
  298. {
  299. if (!(list2[i] is TField))
  300. {
  301. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[]
  302. {
  303. (list2[i] != null) ? list2[i].GetType().Name : "null",
  304. key,
  305. i,
  306. typeof(TField).Name
  307. }));
  308. }
  309. list.Add((TField)((object)list2[i]));
  310. }
  311. return list;
  312. }
  313. public Dictionary<string, TField> GetDictionary<TField>(string key)
  314. {
  315. if (!this._storage.ContainsKey(key))
  316. {
  317. return new Dictionary<string, TField>();
  318. }
  319. if (!(this._storage[key] is Dictionary<string, object>))
  320. {
  321. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary<string, object>).Name));
  322. }
  323. Dictionary<string, TField> dictionary = new Dictionary<string, TField>();
  324. Dictionary<string, object> dictionary2 = (Dictionary<string, object>)this._storage[key];
  325. foreach (KeyValuePair<string, object> keyValuePair in dictionary2)
  326. {
  327. if (!(dictionary2[keyValuePair.Key] is TField))
  328. {
  329. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(TField).Name));
  330. }
  331. dictionary.Add(keyValuePair.Key, (TField)((object)dictionary2[keyValuePair.Key]));
  332. }
  333. return dictionary;
  334. }
  335. public Dictionary<TKey, TValue> GetDictionary<TKey, TValue>(string key) where TKey : struct, IConvertible
  336. {
  337. if (!typeof(TKey).IsEnum())
  338. {
  339. throw new ArgumentException("T must be an Enum.");
  340. }
  341. if (!this._storage.ContainsKey(key))
  342. {
  343. return new Dictionary<TKey, TValue>();
  344. }
  345. if (!(this._storage[key] is Dictionary<string, object>))
  346. {
  347. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary<string, object>).Name));
  348. }
  349. Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
  350. Dictionary<string, object> dictionary2 = (Dictionary<string, object>)this._storage[key];
  351. foreach (KeyValuePair<string, object> keyValuePair in dictionary2)
  352. {
  353. if (!Enum.IsDefined(typeof(TKey), Convert.ToInt32(keyValuePair.Key)))
  354. {
  355. throw new InvalidOperationException(string.Format("{0} is not an underlying value of the {1} enum", keyValuePair.Key, typeof(TKey).Name));
  356. }
  357. if (!(dictionary2[keyValuePair.Key] is TValue))
  358. {
  359. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(TValue).Name));
  360. }
  361. dictionary.Add((TKey)((object)Enum.Parse(typeof(TKey), keyValuePair.Key)), (TValue)((object)dictionary2[keyValuePair.Key]));
  362. }
  363. return dictionary;
  364. }
  365. public Dictionary<string, TModel> GetDictionaryModels<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
  366. {
  367. Dictionary<string, Dictionary<string, object>> dictionary = this.GetDictionary<Dictionary<string, object>>(key);
  368. Dictionary<string, TModel> dictionary2 = new Dictionary<string, TModel>();
  369. foreach (KeyValuePair<string, Dictionary<string, object>> keyValuePair in dictionary)
  370. {
  371. dictionary2.Add(keyValuePair.Key, factoryMethod(new StorageDictionary(keyValuePair.Value)));
  372. }
  373. return dictionary2;
  374. }
  375. public Dictionary<string, TModel> GetDictionaryModelsWithNulls<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
  376. {
  377. if (!this._storage.ContainsKey(key))
  378. {
  379. return new Dictionary<string, TModel>();
  380. }
  381. if (!(this._storage[key] is Dictionary<string, object>))
  382. {
  383. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
  384. }
  385. Dictionary<string, object> dictionary = (Dictionary<string, object>)this._storage[key];
  386. Dictionary<string, TModel> dictionary2 = new Dictionary<string, TModel>();
  387. foreach (KeyValuePair<string, object> keyValuePair in dictionary)
  388. {
  389. if (keyValuePair.Value == null)
  390. {
  391. dictionary2.Add(keyValuePair.Key, default(TModel));
  392. }
  393. else
  394. {
  395. if (!(dictionary[keyValuePair.Key] is Dictionary<string, object>))
  396. {
  397. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(Dictionary<string, object>).Name));
  398. }
  399. dictionary2.Add(keyValuePair.Key, factoryMethod(new StorageDictionary((Dictionary<string, object>)keyValuePair.Value)));
  400. }
  401. }
  402. return dictionary2;
  403. }
  404. public List<TModel> GetModels<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
  405. {
  406. List<Dictionary<string, object>> list = this.GetList<Dictionary<string, object>>(key);
  407. List<TModel> list2 = new List<TModel>();
  408. int count = list.Count;
  409. for (int i = 0; i < count; i++)
  410. {
  411. list2.Add(factoryMethod(new StorageDictionary(list[i])));
  412. }
  413. return list2;
  414. }
  415. public List<TModel> GetModelsWithNulls<TModel>(string key, Func<StorageDictionary, TModel> factoryMethod)
  416. {
  417. if (!(this._storage[key] is List<object>))
  418. {
  419. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}]. Expecting {2}", this._storage[key].GetType().Name, key, typeof(List<object>).Name));
  420. }
  421. List<TModel> list = new List<TModel>();
  422. List<object> list2 = (List<object>)this._storage[key];
  423. int count = list2.Count;
  424. for (int i = 0; i < count; i++)
  425. {
  426. if (list2[i] == null)
  427. {
  428. list.Add(default(TModel));
  429. }
  430. else
  431. {
  432. if (!(list2[i] is Dictionary<string, object>))
  433. {
  434. throw new InvalidOperationException(string.Format("Found {0} at _storage[{1}][{2}]. Expecting {3}", new object[]
  435. {
  436. list2[i].GetType().Name,
  437. key,
  438. i,
  439. typeof(Dictionary<string, object>).Name
  440. }));
  441. }
  442. list.Add(factoryMethod(new StorageDictionary((Dictionary<string, object>)list2[i])));
  443. }
  444. }
  445. return list;
  446. }
  447. public Dictionary<string, object> InternalDictionary
  448. {
  449. get
  450. {
  451. return this._storage;
  452. }
  453. }
  454. private Dictionary<string, object> _storage;
  455. }
  456. }