/Source/ResourceManagement.ObjectModel/RmResource.cs

# · C# · 451 lines · 388 code · 53 blank · 10 comment · 34 complexity · eead8067cd69c898c2869601d1ec1f6f MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Microsoft.ResourceManagement.ObjectModel
  6. {
  7. /// <summary>
  8. /// Represents the base Resource object type.
  9. ///
  10. /// This class is a weakly-typed property bag implemented as a dictionary.
  11. /// Some attributes like ObjectId, DisplayName, and ObjectType have been propomoted to .NET properties.
  12. ///
  13. /// Derived object types like RmUser can promote other properties which are specific to those object types.
  14. ///
  15. /// Use a RmResourceTransaction to monitor changes made to resource objects.
  16. /// </summary>
  17. public class RmResource : IDictionary<RmAttributeName, RmAttributeValue>, IDisposable
  18. {
  19. internal Dictionary<RmAttributeName, RmAttributeValue> attributes;
  20. public RmResource()
  21. {
  22. this.attributes = new Dictionary<RmAttributeName, RmAttributeValue>();
  23. this.EnsureAllAttributesExist();
  24. this.ObjectType = this.GetResourceType();
  25. }
  26. protected virtual void EnsureAllAttributesExist()
  27. {
  28. this.EnsureAttributeExists(AttributeNames.CreatedTime);
  29. this.EnsureAttributeExists(AttributeNames.Creator);
  30. this.EnsureAttributeExists(AttributeNames.DeletedTime);
  31. this.EnsureAttributeExists(AttributeNames.Description);
  32. this.EnsureAttributeExists(AttributeNames.DetectedRulesList);
  33. this.EnsureAttributeExists(AttributeNames.DisplayName);
  34. this.EnsureAttributeExists(AttributeNames.ExpectedRulesList);
  35. this.EnsureAttributeExists(AttributeNames.ExpirationTime);
  36. this.EnsureAttributeExists(AttributeNames.Locale);
  37. this.EnsureAttributeExists(AttributeNames.MVObjectID);
  38. this.EnsureAttributeExists(AttributeNames.ObjectID);
  39. this.EnsureAttributeExists(AttributeNames.ObjectType);
  40. this.EnsureAttributeExists(AttributeNames.ResourceTime);
  41. }
  42. public virtual String GetResourceType()
  43. {
  44. return @"Resource";
  45. }
  46. public Dictionary<RmAttributeName, RmAttributeValue> Attributes
  47. {
  48. get
  49. {
  50. return this.attributes;
  51. }
  52. }
  53. protected String GetString(RmAttributeName attributeName)
  54. {
  55. Object o = null;
  56. RmAttributeValue rma = null;
  57. this.TryGetValue(attributeName, out rma);
  58. if (rma != null)
  59. o = rma.Value;
  60. if (o == null)
  61. {
  62. return String.Empty;
  63. }
  64. else
  65. {
  66. return (String)o;
  67. }
  68. }
  69. protected RmReference GetReference(RmAttributeName attributeName)
  70. {
  71. IComparable o = null;
  72. RmAttributeValue rma = null;
  73. this.attributes.TryGetValue(attributeName, out rma);
  74. if (rma != null && rma.Value != null)
  75. o = rma.Value;
  76. return o as RmReference;
  77. }
  78. protected bool GetBoolean(RmAttributeName attributeName)
  79. {
  80. Object o = null;
  81. RmAttributeValue rma = null;
  82. this.attributes.TryGetValue(attributeName, out rma);
  83. if (rma != null)
  84. o = rma.Value;
  85. if (o == null)
  86. {
  87. return false;
  88. }
  89. else
  90. {
  91. return (bool)o;
  92. }
  93. }
  94. protected int GetInteger(RmAttributeName attributeName)
  95. {
  96. Object o = null;
  97. RmAttributeValue rma = null;
  98. this.attributes.TryGetValue(attributeName, out rma);
  99. if (rma != null)
  100. o = rma.Value;
  101. if (o == null)
  102. {
  103. return 0;
  104. }
  105. else
  106. {
  107. return (Int32)o;
  108. }
  109. }
  110. protected RmList<RmReference> GetMultiValuedReference(RmAttributeName attributeName)
  111. {
  112. IList<IComparable> o = null;
  113. RmAttributeValue rma = null;
  114. this.attributes.TryGetValue(attributeName, out rma);
  115. if (rma == null)
  116. {
  117. rma = new RmAttributeValue();
  118. this.attributes[attributeName] = rma;
  119. }
  120. o = rma.Values;
  121. if (o == null)
  122. {
  123. return null;
  124. }
  125. else
  126. {
  127. return new RmList<RmReference>(o);
  128. }
  129. }
  130. #region promoted properties
  131. public RmReference ObjectID
  132. {
  133. get
  134. {
  135. return GetReference(AttributeNames.ObjectID);
  136. }
  137. set
  138. {
  139. this.attributes[AttributeNames.ObjectID].Value = value;
  140. }
  141. }
  142. public String ObjectType
  143. {
  144. get
  145. {
  146. return GetString(AttributeNames.ObjectType);
  147. }
  148. set
  149. {
  150. this[AttributeNames.ObjectType].Value = value;
  151. }
  152. }
  153. public String DisplayName
  154. {
  155. get
  156. {
  157. return GetString(AttributeNames.DisplayName);
  158. }
  159. set
  160. {
  161. this[AttributeNames.DisplayName].Value = value;
  162. }
  163. }
  164. public String Locale
  165. {
  166. get
  167. {
  168. return GetString(AttributeNames.Locale);
  169. }
  170. set
  171. {
  172. this[AttributeNames.Locale].Value = value;
  173. }
  174. }
  175. public String Description
  176. {
  177. get
  178. {
  179. return GetString(AttributeNames.Description);
  180. }
  181. set
  182. {
  183. this[AttributeNames.Description].Value = value;
  184. }
  185. }
  186. #endregion
  187. protected void EnsureAttributeExists(RmAttributeName attributeName)
  188. {
  189. EnsureNotDisposed();
  190. lock (this.attributes)
  191. {
  192. if (attributeName == null)
  193. {
  194. throw new ArgumentNullException("attributeName");
  195. }
  196. if (this.attributes.ContainsKey(attributeName))
  197. {
  198. return;
  199. }
  200. else
  201. {
  202. this.attributes.Add(attributeName, new RmAttributeValue());
  203. }
  204. }
  205. }
  206. #region Object
  207. public override bool Equals(object obj)
  208. {
  209. RmResource other = obj as RmResource;
  210. if (other == null)
  211. {
  212. return false;
  213. }
  214. else
  215. {
  216. if (this.attributes.Count != other.attributes.Count)
  217. {
  218. return false;
  219. }
  220. foreach (KeyValuePair<RmAttributeName, RmAttributeValue> item in this.attributes)
  221. {
  222. RmAttributeValue otherValue = null;
  223. other.TryGetValue(item.Key, out otherValue);
  224. if (otherValue == null)
  225. return false;
  226. if (item.Value.Equals(otherValue) == false)
  227. return false;
  228. }
  229. return true;
  230. }
  231. }
  232. public override int GetHashCode()
  233. {
  234. return this.ObjectID.GetHashCode();
  235. }
  236. public override string ToString()
  237. {
  238. return String.Format("{0}:{1}",
  239. this.ObjectType,
  240. this.ObjectID.ToString()
  241. );
  242. }
  243. #endregion
  244. #region IDictionary<RmAttributeName,RmAttributeValue> Members
  245. public void Add(RmAttributeName key, RmAttributeValue value)
  246. {
  247. EnsureNotDisposed();
  248. this.attributes.Add(key, value);
  249. }
  250. public bool ContainsKey(RmAttributeName key)
  251. {
  252. EnsureNotDisposed();
  253. return this.attributes.ContainsKey(key);
  254. }
  255. public ICollection<RmAttributeName> Keys
  256. {
  257. get
  258. {
  259. EnsureNotDisposed();
  260. return this.attributes.Keys;
  261. }
  262. }
  263. public bool Remove(RmAttributeName key)
  264. {
  265. EnsureNotDisposed();
  266. return this.attributes.Remove(key);
  267. }
  268. public bool TryGetValue(RmAttributeName key, out RmAttributeValue value)
  269. {
  270. EnsureNotDisposed();
  271. return this.attributes.TryGetValue(key, out value);
  272. }
  273. public ICollection<RmAttributeValue> Values
  274. {
  275. get
  276. {
  277. EnsureNotDisposed();
  278. return this.attributes.Values;
  279. }
  280. }
  281. public RmAttributeValue this[String key]
  282. {
  283. get
  284. {
  285. EnsureNotDisposed();
  286. return this.attributes[new RmAttributeName(key)];
  287. }
  288. set
  289. {
  290. EnsureNotDisposed();
  291. RmAttributeName myKey = new RmAttributeName(key);
  292. this.attributes[myKey] = value;
  293. }
  294. }
  295. public RmAttributeValue this[RmAttributeName key]
  296. {
  297. get
  298. {
  299. EnsureNotDisposed();
  300. return this.attributes[key];
  301. }
  302. set
  303. {
  304. EnsureNotDisposed();
  305. this.attributes[key] = value;
  306. }
  307. }
  308. #endregion
  309. #region ICollection<KeyValuePair<RmAttributeName,RmAttributeValue>> Members
  310. public void Add(KeyValuePair<RmAttributeName, RmAttributeValue> item)
  311. {
  312. EnsureNotDisposed();
  313. this.Add(item.Key, item.Value);
  314. }
  315. public void Clear()
  316. {
  317. EnsureNotDisposed();
  318. this.attributes.Clear();
  319. }
  320. public bool Contains(KeyValuePair<RmAttributeName, RmAttributeValue> item)
  321. {
  322. EnsureNotDisposed();
  323. return this.attributes.ContainsKey(item.Key) && this.attributes[item.Key].Value.Equals(item.Value);
  324. }
  325. public void CopyTo(KeyValuePair<RmAttributeName, RmAttributeValue>[] array, int arrayIndex)
  326. {
  327. EnsureNotDisposed();
  328. throw new NotImplementedException();
  329. }
  330. public int Count
  331. {
  332. get
  333. {
  334. EnsureNotDisposed();
  335. return this.attributes.Count;
  336. }
  337. }
  338. public bool IsReadOnly
  339. {
  340. get { return false; }
  341. }
  342. public bool Remove(KeyValuePair<RmAttributeName, RmAttributeValue> item)
  343. {
  344. EnsureNotDisposed();
  345. return this.attributes.Remove(item.Key);
  346. }
  347. #endregion
  348. #region IEnumerable<KeyValuePair<RmAttributeName,RmAttributeValue>> Members
  349. public IEnumerator<KeyValuePair<RmAttributeName, RmAttributeValue>> GetEnumerator()
  350. {
  351. EnsureNotDisposed();
  352. return this.attributes.GetEnumerator();
  353. }
  354. #endregion
  355. #region IEnumerable Members
  356. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  357. {
  358. EnsureNotDisposed();
  359. return this.attributes.GetEnumerator();
  360. }
  361. #endregion
  362. #region IDisposable Members
  363. public void Dispose()
  364. {
  365. EnsureNotDisposed();
  366. lock (this.attributes)
  367. {
  368. this.attributes.Clear();
  369. this.attributes = null;
  370. GC.SuppressFinalize(this);
  371. }
  372. }
  373. private void EnsureNotDisposed()
  374. {
  375. if (this.attributes == null)
  376. {
  377. throw new ObjectDisposedException("RmObject", "The RmObject object has already been disposed");
  378. }
  379. }
  380. #endregion
  381. public sealed class AttributeNames
  382. {
  383. public static RmAttributeName CreatedTime = new RmAttributeName(@"CreatedTime");
  384. public static RmAttributeName Creator = new RmAttributeName(@"Creator");
  385. public static RmAttributeName DeletedTime = new RmAttributeName(@"DeletedTime");
  386. public static RmAttributeName Description = new RmAttributeName(@"Description");
  387. public static RmAttributeName DetectedRulesList = new RmAttributeName(@"DetectedRulesList");
  388. public static RmAttributeName DisplayName = new RmAttributeName(@"DisplayName");
  389. public static RmAttributeName ExpectedRulesList = new RmAttributeName(@"ExpectedRulesList");
  390. public static RmAttributeName ExpirationTime = new RmAttributeName(@"ExpirationTime");
  391. public static RmAttributeName Locale = new RmAttributeName(@"Locale");
  392. public static RmAttributeName MVObjectID = new RmAttributeName(@"MVObjectID");
  393. public static RmAttributeName ObjectID = new RmAttributeName(@"ObjectID");
  394. public static RmAttributeName ObjectType = new RmAttributeName(@"ObjectType");
  395. public static RmAttributeName ResourceTime = new RmAttributeName(@"ResourceTime");
  396. }
  397. }
  398. }