PowerToolsBlock.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) Microsoft. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using System;
  4. using DocumentFormat.OpenXml.Packaging;
  5. namespace OpenXmlPowerTools
  6. {
  7. /// <summary>
  8. /// Provides an elegant way of wrapping a set of invocations of the PowerTools in a using
  9. /// statement that demarcates those invokations as one "block" before and after which the
  10. /// strongly typed classes provided by the Open XML SDK can be used safely.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// This class lends itself to scenarios where the PowerTools and Linq-to-XML are used as
  15. /// a secondary API for working with Open XML elements, next to the strongly typed classes
  16. /// provided by the Open XML SDK. In these scenarios, the class would be
  17. /// used as follows:
  18. /// </para>
  19. /// <code>
  20. /// [Your code using the strongly typed classes]
  21. ///
  22. /// using (new PowerToolsBlock(wordprocessingDocument))
  23. /// {
  24. /// [Your code using the PowerTools]
  25. /// }
  26. ///
  27. /// [Your code using the strongly typed classes]
  28. /// </code>
  29. /// <para>
  30. /// Upon creation, instances of this class will invoke the
  31. /// <see cref="PowerToolsBlockExtensions.BeginPowerToolsBlock"/> method on the package
  32. /// to begin the transaction. Upon disposal, instances of this class will call the
  33. /// <see cref="PowerToolsBlockExtensions.EndPowerToolsBlock"/> method on the package
  34. /// to end the transaction.
  35. /// </para>
  36. /// </remarks>
  37. /// <seealso cref="StronglyTypedBlock" />
  38. /// <seealso cref="PowerToolsBlockExtensions.BeginPowerToolsBlock"/>
  39. /// <seealso cref="PowerToolsBlockExtensions.EndPowerToolsBlock"/>
  40. public class PowerToolsBlock : IDisposable
  41. {
  42. private OpenXmlPackage _package;
  43. public PowerToolsBlock(OpenXmlPackage package)
  44. {
  45. if (package == null) throw new ArgumentNullException("package");
  46. _package = package;
  47. _package.BeginPowerToolsBlock();
  48. }
  49. public void Dispose()
  50. {
  51. if (_package == null) return;
  52. _package.EndPowerToolsBlock();
  53. _package = null;
  54. }
  55. }
  56. }