MaterializedOnDemandBsonDocument.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. [BsonSerializer(typeof(MaterializedOnDemandBsonDocumentSerializer))]
  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 an element of this document.
  459. /// </summary>
  460. /// <param name="index">The zero based index of the element.</param>
  461. /// <returns>
  462. /// The element.
  463. /// </returns>
  464. public override BsonElement GetElement(int index)
  465. {
  466. EnsureIsMaterialized();
  467. return base.GetElement(index);
  468. }
  469. /// <summary>
  470. /// Gets an element of this document.
  471. /// </summary>
  472. /// <param name="name">The name of the element.</param>
  473. /// <returns>
  474. /// A BsonElement.
  475. /// </returns>
  476. public override BsonElement GetElement(string name)
  477. {
  478. EnsureIsMaterialized();
  479. return base.GetElement(name);
  480. }
  481. /// <summary>
  482. /// Gets an enumerator that can be used to enumerate the elements of this document.
  483. /// </summary>
  484. /// <returns>
  485. /// An enumerator.
  486. /// </returns>
  487. public override IEnumerator<BsonElement> GetEnumerator()
  488. {
  489. EnsureIsMaterialized();
  490. return base.GetEnumerator();
  491. }
  492. /// <summary>
  493. /// Returns a hash code for this instance.
  494. /// </summary>
  495. /// <returns>
  496. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
  497. /// </returns>
  498. public override int GetHashCode()
  499. {
  500. EnsureIsMaterialized();
  501. return base.GetHashCode();
  502. }
  503. /// <summary>
  504. /// Gets the value of an element.
  505. /// </summary>
  506. /// <param name="index">The zero based index of the element.</param>
  507. /// <returns>
  508. /// The value of the element.
  509. /// </returns>
  510. public override BsonValue GetValue(int index)
  511. {
  512. EnsureIsMaterialized();
  513. return base.GetValue(index);
  514. }
  515. /// <summary>
  516. /// Gets the value of an element.
  517. /// </summary>
  518. /// <param name="name">The name of the element.</param>
  519. /// <returns>
  520. /// The value of the element.
  521. /// </returns>
  522. public override BsonValue GetValue(string name)
  523. {
  524. EnsureIsMaterialized();
  525. return base.GetValue(name);
  526. }
  527. /// <summary>
  528. /// Gets the value of an element or a default value if the element is not found.
  529. /// </summary>
  530. /// <param name="name">The name of the element.</param>
  531. /// <param name="defaultValue">The default value returned if the element is not found.</param>
  532. /// <returns>
  533. /// The value of the element or the default value if the element is not found.
  534. /// </returns>
  535. public override BsonValue GetValue(string name, BsonValue defaultValue)
  536. {
  537. EnsureIsMaterialized();
  538. return base.GetValue(name, defaultValue);
  539. }
  540. /// <summary>
  541. /// Inserts a new element at a specified position.
  542. /// </summary>
  543. /// <param name="index">The position of the new element.</param>
  544. /// <param name="element">The element.</param>
  545. public override void InsertAt(int index, BsonElement element)
  546. {
  547. EnsureIsMaterialized();
  548. base.InsertAt(index, element);
  549. }
  550. /// <summary>
  551. /// Merges another document into this one. Existing elements are not overwritten.
  552. /// </summary>
  553. /// <param name="document">The other document.</param>
  554. /// <returns>
  555. /// The document (so method calls can be chained).
  556. /// </returns>
  557. public override BsonDocument Merge(BsonDocument document)
  558. {
  559. EnsureIsMaterialized();
  560. return base.Merge(document);
  561. }
  562. /// <summary>
  563. /// Merges another document into this one, specifying whether existing elements are overwritten.
  564. /// </summary>
  565. /// <param name="document">The other document.</param>
  566. /// <param name="overwriteExistingElements">Whether to overwrite existing elements.</param>
  567. /// <returns>
  568. /// The document (so method calls can be chained).
  569. /// </returns>
  570. public override BsonDocument Merge(BsonDocument document, bool overwriteExistingElements)
  571. {
  572. EnsureIsMaterialized();
  573. return base.Merge(document, overwriteExistingElements);
  574. }
  575. /// <summary>
  576. /// Removes an element from this document (if duplicate element names are allowed
  577. /// then all elements with this name will be removed).
  578. /// </summary>
  579. /// <param name="name">The name of the element to remove.</param>
  580. public override void Remove(string name)
  581. {
  582. EnsureIsMaterialized();
  583. base.Remove(name);
  584. }
  585. /// <summary>
  586. /// Removes an element from this document.
  587. /// </summary>
  588. /// <param name="index">The zero based index of the element to remove.</param>
  589. public override void RemoveAt(int index)
  590. {
  591. EnsureIsMaterialized();
  592. base.RemoveAt(index);
  593. }
  594. /// <summary>
  595. /// Removes an element from this document.
  596. /// </summary>
  597. /// <param name="element">The element to remove.</param>
  598. public override void RemoveElement(BsonElement element)
  599. {
  600. EnsureIsMaterialized();
  601. base.RemoveElement(element);
  602. }
  603. /// <summary>
  604. /// Sets the value of an element.
  605. /// </summary>
  606. /// <param name="index">The zero based index of the element whose value is to be set.</param>
  607. /// <param name="value">The new value.</param>
  608. /// <returns>
  609. /// The document (so method calls can be chained).
  610. /// </returns>
  611. public override BsonDocument Set(int index, BsonValue value)
  612. {
  613. EnsureIsMaterialized();
  614. return base.Set(index, value);
  615. }
  616. /// <summary>
  617. /// Sets the value of an element (an element will be added if no element with this name is found).
  618. /// </summary>
  619. /// <param name="name">The name 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(string name, BsonValue value)
  625. {
  626. EnsureIsMaterialized();
  627. return base.Set(name, value);
  628. }
  629. /// <summary>
  630. /// 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).
  631. /// </summary>
  632. /// <param name="element">The new element.</param>
  633. /// <returns>
  634. /// The document.
  635. /// </returns>
  636. public override BsonDocument SetElement(BsonElement element)
  637. {
  638. EnsureIsMaterialized();
  639. return base.SetElement(element);
  640. }
  641. /// <summary>
  642. /// Sets an element of the document (replacing the existing element at that position).
  643. /// </summary>
  644. /// <param name="index">The zero based index of the element to replace.</param>
  645. /// <param name="element">The new element.</param>
  646. /// <returns>
  647. /// The document.
  648. /// </returns>
  649. public override BsonDocument SetElement(int index, BsonElement element)
  650. {
  651. EnsureIsMaterialized();
  652. return base.SetElement(index, element);
  653. }
  654. /// <summary>
  655. /// Tries to get an element of this document.
  656. /// </summary>
  657. /// <param name="name">The name of the element.</param>
  658. /// <param name="value">The element.</param>
  659. /// <returns>
  660. /// True if an element with that name was found.
  661. /// </returns>
  662. public override bool TryGetElement(string name, out BsonElement value)
  663. {
  664. EnsureIsMaterialized();
  665. return base.TryGetElement(name, out value);
  666. }
  667. /// <summary>
  668. /// Tries to get the value of an element of this document.
  669. /// </summary>
  670. /// <param name="name">The name of the element.</param>
  671. /// <param name="value">The value of the element.</param>
  672. /// <returns>
  673. /// True if an element with that name was found.
  674. /// </returns>
  675. public override bool TryGetValue(string name, out BsonValue value)
  676. {
  677. EnsureIsMaterialized();
  678. return base.TryGetValue(name, out value);
  679. }
  680. // protected methods
  681. /// <summary>
  682. /// Releases unmanaged and - optionally - managed resources.
  683. /// </summary>
  684. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  685. protected virtual void Dispose(bool disposing)
  686. {
  687. _disposed = true;
  688. }
  689. /// <summary>
  690. /// Materializes the BsonDocument.
  691. /// </summary>
  692. /// <returns>The materialized elements.</returns>
  693. protected abstract IEnumerable<BsonElement> Materialize();
  694. /// <summary>
  695. /// Informs subclasses that the Materialize process completed so they can free any resources related to the unmaterialized state.
  696. /// </summary>
  697. protected abstract void MaterializeCompleted();
  698. /// <summary>
  699. /// Throws if disposed.
  700. /// </summary>
  701. /// <exception cref="System.ObjectDisposedException"></exception>
  702. protected void ThrowIfDisposed()
  703. {
  704. if (_disposed)
  705. {
  706. throw new ObjectDisposedException(GetType().Name);
  707. }
  708. }
  709. // private methods
  710. private void EnsureIsMaterialized()
  711. {
  712. ThrowIfDisposed();
  713. if (!_isMaterialized)
  714. {
  715. var elements = Materialize();
  716. try
  717. {
  718. _isMaterialized = true;
  719. base.AddRange(elements);
  720. MaterializeCompleted();
  721. }
  722. catch
  723. {
  724. base.Clear();
  725. _isMaterialized = false;
  726. throw;
  727. }
  728. }
  729. }
  730. }
  731. internal class MaterializedOnDemandBsonDocumentSerializer : AbstractClassSerializer<MaterializedOnDemandBsonDocument>
  732. {
  733. }
  734. }