MaterializedOnDemandBsonDocument.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. /* Copyright 2010-2014 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.IO;
  19. using System.Linq;
  20. using MongoDB.Bson.IO;
  21. using MongoDB.Bson.Serialization;
  22. using MongoDB.Bson.Serialization.Attributes;
  23. using MongoDB.Bson.Serialization.Serializers;
  24. namespace MongoDB.Bson
  25. {
  26. /// <summary>
  27. /// Represents a BSON document that is not materialized until you start using it.
  28. /// </summary>
  29. [Serializable]
  30. public abstract class MaterializedOnDemandBsonDocument : BsonDocument, IDisposable
  31. {
  32. // private fields
  33. private bool _disposed;
  34. private bool _isMaterialized;
  35. // constructors
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="MaterializedOnDemandBsonDocument"/> class.
  38. /// </summary>
  39. protected MaterializedOnDemandBsonDocument()
  40. {
  41. }
  42. // public properties
  43. /// <summary>
  44. /// Gets the number of elements.
  45. /// </summary>
  46. public override int ElementCount
  47. {
  48. get
  49. {
  50. EnsureIsMaterialized();
  51. return base.ElementCount;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets the elements.
  56. /// </summary>
  57. public override IEnumerable<BsonElement> Elements
  58. {
  59. get
  60. {
  61. EnsureIsMaterialized();
  62. return base.Elements;
  63. }
  64. }
  65. /// <summary>
  66. /// Gets a value indicating whether this instance is disposed.
  67. /// </summary>
  68. /// <value>
  69. /// <c>true</c> if this instance is disposed; otherwise, <c>false</c>.
  70. /// </value>
  71. public bool IsDisposed
  72. {
  73. get { return _disposed; }
  74. }
  75. /// <summary>
  76. /// Gets a value indicating whether this instance is materialized.
  77. /// </summary>
  78. /// <value>
  79. /// <c>true</c> if this instance is materialized; otherwise, <c>false</c>.
  80. /// </value>
  81. public bool IsMaterialized
  82. {
  83. get { return _isMaterialized; }
  84. }
  85. /// <summary>
  86. /// Gets the element names.
  87. /// </summary>
  88. public override IEnumerable<string> Names
  89. {
  90. get
  91. {
  92. EnsureIsMaterialized();
  93. return base.Names;
  94. }
  95. }
  96. /// <summary>
  97. /// Gets the raw values (see BsonValue.RawValue).
  98. /// </summary>
  99. [Obsolete("Use Values instead.")]
  100. public override IEnumerable<object> RawValues
  101. {
  102. get
  103. {
  104. EnsureIsMaterialized();
  105. return base.RawValues;
  106. }
  107. }
  108. /// <summary>
  109. /// Gets the values.
  110. /// </summary>
  111. public override IEnumerable<BsonValue> Values
  112. {
  113. get
  114. {
  115. EnsureIsMaterialized();
  116. return base.Values;
  117. }
  118. }
  119. // public indexers
  120. /// <summary>
  121. /// Gets or sets a value by position.
  122. /// </summary>
  123. /// <param name="index">The position.</param>
  124. /// <returns>The value.</returns>
  125. public override BsonValue this[int index]
  126. {
  127. get
  128. {
  129. EnsureIsMaterialized();
  130. return base[index];
  131. }
  132. set
  133. {
  134. EnsureIsMaterialized();
  135. base[index] = value;
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the value of an element or a default value if the element is not found.
  140. /// </summary>
  141. /// <param name="name">The name of the element.</param>
  142. /// <param name="defaultValue">The default value to return if the element is not found.</param>
  143. /// <returns>Teh value of the element or a default value if the element is not found.</returns>
  144. [Obsolete("Use GetValue(string name, BsonValue defaultValue) instead.")]
  145. public override BsonValue this[string name, BsonValue defaultValue]
  146. {
  147. get
  148. {
  149. EnsureIsMaterialized();
  150. return base[name, defaultValue];
  151. }
  152. }
  153. /// <summary>
  154. /// Gets or sets a value by name.
  155. /// </summary>
  156. /// <param name="name">The name.</param>
  157. /// <returns>The value.</returns>
  158. public override BsonValue this[string name]
  159. {
  160. get
  161. {
  162. EnsureIsMaterialized();
  163. return base[name];
  164. }
  165. set
  166. {
  167. EnsureIsMaterialized();
  168. base[name] = value;
  169. }
  170. }
  171. // public methods
  172. /// <summary>
  173. /// Adds an element to the document.
  174. /// </summary>
  175. /// <param name="element">The element to add.</param>
  176. /// <returns>
  177. /// The document (so method calls can be chained).
  178. /// </returns>
  179. public override BsonDocument Add(BsonElement element)
  180. {
  181. EnsureIsMaterialized();
  182. return base.Add(element);
  183. }
  184. /// <summary>
  185. /// Adds elements to the document from a dictionary of key/value pairs.
  186. /// </summary>
  187. /// <param name="dictionary">The dictionary.</param>
  188. /// <returns>The document (so method calls can be chained).</returns>
  189. [Obsolete("Use AddRange instead.")]
  190. public override BsonDocument Add(Dictionary<string, object> dictionary)
  191. {
  192. EnsureIsMaterialized();
  193. return base.Add(dictionary);
  194. }
  195. /// <summary>
  196. /// Adds elements to the document from a dictionary of key/value pairs.
  197. /// </summary>
  198. /// <param name="dictionary">The dictionary.</param>
  199. /// <param name="keys">Which keys of the hash table to add.</param>
  200. /// <returns>The document (so method calls can be chained).</returns>
  201. [Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
  202. public override BsonDocument Add(Dictionary<string, object> dictionary, IEnumerable<string> keys)
  203. {
  204. EnsureIsMaterialized();
  205. return base.Add(dictionary, keys);
  206. }
  207. /// <summary>
  208. /// Adds elements to the document from a dictionary of key/value pairs.
  209. /// </summary>
  210. /// <param name="dictionary">The dictionary.</param>
  211. /// <returns>The document (so method calls can be chained).</returns>
  212. [Obsolete("Use AddRange instead.")]
  213. public override BsonDocument Add(IDictionary<string, object> dictionary)
  214. {
  215. EnsureIsMaterialized();
  216. return base.Add(dictionary);
  217. }
  218. /// <summary>
  219. /// Adds elements to the document from a dictionary of key/value pairs.
  220. /// </summary>
  221. /// <param name="dictionary">The dictionary.</param>
  222. /// <param name="keys">Which keys of the hash table to add.</param>
  223. /// <returns>The document (so method calls can be chained).</returns>
  224. [Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
  225. public override BsonDocument Add(IDictionary<string, object> dictionary, IEnumerable<string> keys)
  226. {
  227. EnsureIsMaterialized();
  228. return base.Add(dictionary, keys);
  229. }
  230. /// <summary>
  231. /// Adds elements to the document from a dictionary of key/value pairs.
  232. /// </summary>
  233. /// <param name="dictionary">The dictionary.</param>
  234. /// <returns>The document (so method calls can be chained).</returns>
  235. [Obsolete("Use AddRange instead.")]
  236. public override BsonDocument Add(IDictionary dictionary)
  237. {
  238. EnsureIsMaterialized();
  239. return base.Add(dictionary);
  240. }
  241. /// <summary>
  242. /// Adds elements to the document from a dictionary of key/value pairs.
  243. /// </summary>
  244. /// <param name="dictionary">The dictionary.</param>
  245. /// <param name="keys">Which keys of the hash table to add.</param>
  246. /// <returns>The document (so method calls can be chained).</returns>
  247. [Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
  248. public override BsonDocument Add(IDictionary dictionary, IEnumerable keys)
  249. {
  250. EnsureIsMaterialized();
  251. return base.Add(dictionary, keys);
  252. }
  253. /// <summary>
  254. /// Adds a list of elements to the document.
  255. /// </summary>
  256. /// <param name="elements">The list of elements.</param>
  257. /// <returns>The document (so method calls can be chained).</returns>
  258. [Obsolete("Use AddRange instead.")]
  259. public override BsonDocument Add(IEnumerable<BsonElement> elements)
  260. {
  261. EnsureIsMaterialized();
  262. return base.Add(elements);
  263. }
  264. /// <summary>
  265. /// Adds a list of elements to the document.
  266. /// </summary>
  267. /// <param name="elements">The list of elements.</param>
  268. /// <returns>The document (so method calls can be chained).</returns>
  269. [Obsolete("Use AddRange(IEnumerable<BsonElement> elements) instead.")]
  270. public override BsonDocument Add(params BsonElement[] elements)
  271. {
  272. EnsureIsMaterialized();
  273. return base.Add(elements);
  274. }
  275. /// <summary>
  276. /// Creates and adds an element to the document.
  277. /// </summary>
  278. /// <param name="name">The name of the element.</param>
  279. /// <param name="value">The value of the element.</param>
  280. /// <returns>
  281. /// The document (so method calls can be chained).
  282. /// </returns>
  283. public override BsonDocument Add(string name, BsonValue value)
  284. {
  285. EnsureIsMaterialized();
  286. return base.Add(name, value);
  287. }
  288. /// <summary>
  289. /// Creates and adds an element to the document, but only if the condition is true.
  290. /// </summary>
  291. /// <param name="name">The name of the element.</param>
  292. /// <param name="value">The value of the element.</param>
  293. /// <param name="condition">Whether to add the element to the document.</param>
  294. /// <returns>The document (so method calls can be chained).</returns>
  295. public override BsonDocument Add(string name, BsonValue value, bool condition)
  296. {
  297. EnsureIsMaterialized();
  298. return base.Add(name, value, condition);
  299. }
  300. /// <summary>
  301. /// Creates and adds an element to the document, but only if the condition is true.
  302. /// If the condition is false the value factory is not called at all.
  303. /// </summary>
  304. /// <param name="name">The name of the element.</param>
  305. /// <param name="valueFactory">A delegate called to compute the value of the element if condition is true.</param>
  306. /// <param name="condition">Whether to add the element to the document.</param>
  307. /// <returns>The document (so method calls can be chained).</returns>
  308. public override BsonDocument Add(string name, Func<BsonValue> valueFactory, bool condition)
  309. {
  310. EnsureIsMaterialized();
  311. return base.Add(name, valueFactory, condition);
  312. }
  313. /// <summary>
  314. /// Adds elements to the document from a dictionary of key/value pairs.
  315. /// </summary>
  316. /// <param name="dictionary">The dictionary.</param>
  317. /// <returns>
  318. /// The document (so method calls can be chained).
  319. /// </returns>
  320. public override BsonDocument AddRange(Dictionary<string, object> dictionary)
  321. {
  322. EnsureIsMaterialized();
  323. return base.AddRange(dictionary);
  324. }
  325. /// <summary>
  326. /// Adds elements to the document from a dictionary of key/value pairs.
  327. /// </summary>
  328. /// <param name="dictionary">The dictionary.</param>
  329. /// <returns>
  330. /// The document (so method calls can be chained).
  331. /// </returns>
  332. public override BsonDocument AddRange(IDictionary dictionary)
  333. {
  334. EnsureIsMaterialized();
  335. return base.AddRange(dictionary);
  336. }
  337. /// <summary>
  338. /// Adds a list of elements to the document.
  339. /// </summary>
  340. /// <param name="elements">The list of elements.</param>
  341. /// <returns>
  342. /// The document (so method calls can be chained).
  343. /// </returns>
  344. public override BsonDocument AddRange(IEnumerable<BsonElement> elements)
  345. {
  346. EnsureIsMaterialized();
  347. return base.AddRange(elements);
  348. }
  349. /// <summary>
  350. /// Adds elements to the document from a dictionary of key/value pairs.
  351. /// </summary>
  352. /// <param name="dictionary">The dictionary.</param>
  353. /// <returns>
  354. /// The document (so method calls can be chained).
  355. /// </returns>
  356. public override BsonDocument AddRange(IEnumerable<KeyValuePair<string, object>> dictionary)
  357. {
  358. EnsureIsMaterialized();
  359. return base.AddRange(dictionary);
  360. }
  361. /// <summary>
  362. /// Clears the document (removes all elements).
  363. /// </summary>
  364. public override void Clear()
  365. {
  366. EnsureIsMaterialized();
  367. base.Clear();
  368. }
  369. /// <summary>
  370. /// Creates a shallow clone of the document (see also DeepClone).
  371. /// </summary>
  372. /// <returns>
  373. /// A shallow clone of the document.
  374. /// </returns>
  375. public override BsonValue Clone()
  376. {
  377. EnsureIsMaterialized();
  378. return base.Clone();
  379. }
  380. /// <summary>
  381. /// Compares this document to another document.
  382. /// </summary>
  383. /// <param name="other">The other document.</param>
  384. /// <returns>
  385. /// A 32-bit signed integer that indicates whether this document is less than, equal to, or greather than the other.
  386. /// </returns>
  387. public override int CompareTo(BsonDocument other)
  388. {
  389. EnsureIsMaterialized();
  390. return base.CompareTo(other);
  391. }
  392. /// <summary>
  393. /// Compares the BsonDocument to another BsonValue.
  394. /// </summary>
  395. /// <param name="other">The other BsonValue.</param>
  396. /// <returns>A 32-bit signed integer that indicates whether this BsonDocument is less than, equal to, or greather than the other BsonValue.</returns>
  397. public override int CompareTo(BsonValue other)
  398. {
  399. EnsureIsMaterialized();
  400. return base.CompareTo(other);
  401. }
  402. /// <summary>
  403. /// Tests whether the document contains an element with the specified name.
  404. /// </summary>
  405. /// <param name="name">The name of the element to look for.</param>
  406. /// <returns>
  407. /// True if the document contains an element with the specified name.
  408. /// </returns>
  409. public override bool Contains(string name)
  410. {
  411. EnsureIsMaterialized();
  412. return base.Contains(name);
  413. }
  414. /// <summary>
  415. /// Tests whether the document contains an element with the specified value.
  416. /// </summary>
  417. /// <param name="value">The value of the element to look for.</param>
  418. /// <returns>
  419. /// True if the document contains an element with the specified value.
  420. /// </returns>
  421. public override bool ContainsValue(BsonValue value)
  422. {
  423. EnsureIsMaterialized();
  424. return base.ContainsValue(value);
  425. }
  426. /// <summary>
  427. /// Creates a deep clone of the document (see also Clone).
  428. /// </summary>
  429. /// <returns>
  430. /// A deep clone of the document.
  431. /// </returns>
  432. public override BsonValue DeepClone()
  433. {
  434. EnsureIsMaterialized();
  435. return base.DeepClone();
  436. }
  437. /// <summary>
  438. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  439. /// </summary>
  440. public void Dispose()
  441. {
  442. Dispose(true);
  443. GC.SuppressFinalize(this);
  444. }
  445. /// <summary>
  446. /// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
  447. /// </summary>
  448. /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
  449. /// <returns>
  450. /// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
  451. /// </returns>
  452. public override bool Equals(object obj)
  453. {
  454. EnsureIsMaterialized();
  455. return base.Equals(obj);
  456. }
  457. /// <summary>
  458. /// Gets the Id of the document.
  459. /// </summary>
  460. /// <param name="id">The Id of the document (the RawValue if it has one, otherwise the element Value).</param>
  461. /// <param name="idNominalType">The nominal type of the Id.</param>
  462. /// <param name="idGenerator">The IdGenerator for the Id (or null).</param>
  463. /// <returns>True (a BsonDocument either has an Id member or one can be added).</returns>
  464. [Obsolete("GetDocumentId was intended to be private and will become private in a future release. Use document[\"_id\"] or document.GetValue(\"_id\") instead.")]
  465. public override bool GetDocumentId(out object id, out Type idNominalType, out IIdGenerator idGenerator)
  466. {
  467. EnsureIsMaterialized();
  468. return base.GetDocumentId(out id, out idNominalType, out idGenerator);
  469. }
  470. /// <summary>
  471. /// Gets an element of this document.
  472. /// </summary>
  473. /// <param name="index">The zero based index of the element.</param>
  474. /// <returns>
  475. /// The element.
  476. /// </returns>
  477. public override BsonElement GetElement(int index)
  478. {
  479. EnsureIsMaterialized();
  480. return base.GetElement(index);
  481. }
  482. /// <summary>
  483. /// Gets an element of this document.
  484. /// </summary>
  485. /// <param name="name">The name of the element.</param>
  486. /// <returns>
  487. /// A BsonElement.
  488. /// </returns>
  489. public override BsonElement GetElement(string name)
  490. {
  491. EnsureIsMaterialized();
  492. return base.GetElement(name);
  493. }
  494. /// <summary>
  495. /// Gets an enumerator that can be used to enumerate the elements of this document.
  496. /// </summary>
  497. /// <returns>
  498. /// An enumerator.
  499. /// </returns>
  500. public override IEnumerator<BsonElement> GetEnumerator()
  501. {
  502. EnsureIsMaterialized();
  503. return base.GetEnumerator();
  504. }
  505. /// <summary>
  506. /// Returns a hash code for this instance.
  507. /// </summary>
  508. /// <returns>
  509. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  510. /// </returns>
  511. public override int GetHashCode()
  512. {
  513. EnsureIsMaterialized();
  514. return base.GetHashCode();
  515. }
  516. /// <summary>
  517. /// Gets the value of an element.
  518. /// </summary>
  519. /// <param name="index">The zero based index of the element.</param>
  520. /// <returns>
  521. /// The value of the element.
  522. /// </returns>
  523. public override BsonValue GetValue(int index)
  524. {
  525. EnsureIsMaterialized();
  526. return base.GetValue(index);
  527. }
  528. /// <summary>
  529. /// Gets the value of an element.
  530. /// </summary>
  531. /// <param name="name">The name of the element.</param>
  532. /// <returns>
  533. /// The value of the element.
  534. /// </returns>
  535. public override BsonValue GetValue(string name)
  536. {
  537. EnsureIsMaterialized();
  538. return base.GetValue(name);
  539. }
  540. /// <summary>
  541. /// Gets the value of an element or a default value if the element is not found.
  542. /// </summary>
  543. /// <param name="name">The name of the element.</param>
  544. /// <param name="defaultValue">The default value returned if the element is not found.</param>
  545. /// <returns>
  546. /// The value of the element or the default value if the element is not found.
  547. /// </returns>
  548. public override BsonValue GetValue(string name, BsonValue defaultValue)
  549. {
  550. EnsureIsMaterialized();
  551. return base.GetValue(name, defaultValue);
  552. }
  553. /// <summary>
  554. /// Inserts a new element at a specified position.
  555. /// </summary>
  556. /// <param name="index">The position of the new element.</param>
  557. /// <param name="element">The element.</param>
  558. public override void InsertAt(int index, BsonElement element)
  559. {
  560. EnsureIsMaterialized();
  561. base.InsertAt(index, element);
  562. }
  563. /// <summary>
  564. /// Merges another document into this one. Existing elements are not overwritten.
  565. /// </summary>
  566. /// <param name="document">The other document.</param>
  567. /// <returns>
  568. /// The document (so method calls can be chained).
  569. /// </returns>
  570. public override BsonDocument Merge(BsonDocument document)
  571. {
  572. EnsureIsMaterialized();
  573. return base.Merge(document);
  574. }
  575. /// <summary>
  576. /// Merges another document into this one, specifying whether existing elements are overwritten.
  577. /// </summary>
  578. /// <param name="document">The other document.</param>
  579. /// <param name="overwriteExistingElements">Whether to overwrite existing elements.</param>
  580. /// <returns>
  581. /// The document (so method calls can be chained).
  582. /// </returns>
  583. public override BsonDocument Merge(BsonDocument document, bool overwriteExistingElements)
  584. {
  585. EnsureIsMaterialized();
  586. return base.Merge(document, overwriteExistingElements);
  587. }
  588. /// <summary>
  589. /// Removes an element from this document (if duplicate element names are allowed
  590. /// then all elements with this name will be removed).
  591. /// </summary>
  592. /// <param name="name">The name of the element to remove.</param>
  593. public override void Remove(string name)
  594. {
  595. EnsureIsMaterialized();
  596. base.Remove(name);
  597. }
  598. /// <summary>
  599. /// Removes an element from this document.
  600. /// </summary>
  601. /// <param name="index">The zero based index of the element to remove.</param>
  602. public override void RemoveAt(int index)
  603. {
  604. EnsureIsMaterialized();
  605. base.RemoveAt(index);
  606. }
  607. /// <summary>
  608. /// Removes an element from this document.
  609. /// </summary>
  610. /// <param name="element">The element to remove.</param>
  611. public override void RemoveElement(BsonElement element)
  612. {
  613. EnsureIsMaterialized();
  614. base.RemoveElement(element);
  615. }
  616. /// <summary>
  617. /// Sets the value of an element.
  618. /// </summary>
  619. /// <param name="index">The zero based index of the element whose value is to be set.</param>
  620. /// <param name="value">The new value.</param>
  621. /// <returns>
  622. /// The document (so method calls can be chained).
  623. /// </returns>
  624. public override BsonDocument Set(int index, BsonValue value)
  625. {
  626. EnsureIsMaterialized();
  627. return base.Set(index, value);
  628. }
  629. /// <summary>
  630. /// Sets the value of an element (an element will be added if no element with this name is found).
  631. /// </summary>
  632. /// <param name="name">The name of the element whose value is to be set.</param>
  633. /// <param name="value">The new value.</param>
  634. /// <returns>
  635. /// The document (so method calls can be chained).
  636. /// </returns>
  637. public override BsonDocument Set(string name, BsonValue value)
  638. {
  639. EnsureIsMaterialized();
  640. return base.Set(name, value);
  641. }
  642. /// <summary>
  643. /// Sets the document Id.
  644. /// </summary>
  645. /// <param name="id">The value of the Id.</param>
  646. [Obsolete("SetDocumentId was intended to be private and will become private in a future release. Use document[\"_id\"] = value or document.Set(\"_id\", value) instead.")]
  647. public override void SetDocumentId(object id)
  648. {
  649. EnsureIsMaterialized();
  650. base.SetDocumentId(id);
  651. }
  652. /// <summary>
  653. /// Sets an element of the document (replaces any existing element with the same name or adds a new element if an element with the same name is not found).
  654. /// </summary>
  655. /// <param name="element">The new element.</param>
  656. /// <returns>
  657. /// The document.
  658. /// </returns>
  659. public override BsonDocument SetElement(BsonElement element)
  660. {
  661. EnsureIsMaterialized();
  662. return base.SetElement(element);
  663. }
  664. /// <summary>
  665. /// Sets an element of the document (replacing the existing element at that position).
  666. /// </summary>
  667. /// <param name="index">The zero based index of the element to replace.</param>
  668. /// <param name="element">The new element.</param>
  669. /// <returns>
  670. /// The document.
  671. /// </returns>
  672. public override BsonDocument SetElement(int index, BsonElement element)
  673. {
  674. EnsureIsMaterialized();
  675. return base.SetElement(index, element);
  676. }
  677. /// <summary>
  678. /// Tries to get an element of this document.
  679. /// </summary>
  680. /// <param name="name">The name of the element.</param>
  681. /// <param name="value">The element.</param>
  682. /// <returns>
  683. /// True if an element with that name was found.
  684. /// </returns>
  685. public override bool TryGetElement(string name, out BsonElement value)
  686. {
  687. EnsureIsMaterialized();
  688. return base.TryGetElement(name, out value);
  689. }
  690. /// <summary>
  691. /// Tries to get the value of an element of this document.
  692. /// </summary>
  693. /// <param name="name">The name of the element.</param>
  694. /// <param name="value">The value of the element.</param>
  695. /// <returns>
  696. /// True if an element with that name was found.
  697. /// </returns>
  698. public override bool TryGetValue(string name, out BsonValue value)
  699. {
  700. EnsureIsMaterialized();
  701. return base.TryGetValue(name, out value);
  702. }
  703. // protected methods
  704. /// <summary>
  705. /// Releases unmanaged and - optionally - managed resources.
  706. /// </summary>
  707. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  708. protected virtual void Dispose(bool disposing)
  709. {
  710. _disposed = true;
  711. }
  712. /// <summary>
  713. /// Materializes the BsonDocument.
  714. /// </summary>
  715. /// <returns>The materialized elements.</returns>
  716. protected abstract IEnumerable<BsonElement> Materialize();
  717. /// <summary>
  718. /// Informs subclasses that the Materialize process completed so they can free any resources related to the unmaterialized state.
  719. /// </summary>
  720. protected abstract void MaterializeCompleted();
  721. /// <summary>
  722. /// Throws if disposed.
  723. /// </summary>
  724. /// <exception cref="System.ObjectDisposedException"></exception>
  725. protected void ThrowIfDisposed()
  726. {
  727. if (_disposed)
  728. {
  729. throw new ObjectDisposedException(GetType().Name);
  730. }
  731. }
  732. // private methods
  733. private void EnsureIsMaterialized()
  734. {
  735. ThrowIfDisposed();
  736. if (!_isMaterialized)
  737. {
  738. var elements = Materialize();
  739. try
  740. {
  741. _isMaterialized = true;
  742. base.AddRange(elements);
  743. MaterializeCompleted();
  744. }
  745. catch
  746. {
  747. base.Clear();
  748. _isMaterialized = false;
  749. throw;
  750. }
  751. }
  752. }
  753. }
  754. }