/Rhino.Etl.Core/Files/FluentFile.cs

http://github.com/ayende/rhino-etl · C# · 118 lines · 58 code · 11 blank · 49 comment · 2 complexity · a3e876acb5c2dabe9f6eb232bea5832a MD5 · raw file

  1. namespace Rhino.Etl.Core.Files
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Text;
  7. using FileHelpers;
  8. /// <summary>
  9. /// Gives a fluent interface syntax on top of the <see cref="FileHelperEngine"/>
  10. /// </summary>
  11. public class FluentFile
  12. {
  13. private readonly FileHelperAsyncEngine engine;
  14. /// <summary>
  15. /// Get a new fluent file instance for <typeparam name="T"></typeparam>
  16. /// </summary>
  17. public static FluentFile For<T>()
  18. {
  19. return new FluentFile(typeof(T));
  20. }
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="FluentFile"/> class.
  23. /// </summary>
  24. /// <param name="type">The type.</param>
  25. public FluentFile(Type type)
  26. {
  27. engine = new FileHelperAsyncEngine(type);
  28. }
  29. /// <summary>
  30. /// Specify which file to start reading from
  31. /// </summary>
  32. /// <param name="filename">The filename.</param>
  33. public FileEngine From(string filename)
  34. {
  35. filename = NormalizeFilename(filename);
  36. engine.BeginReadFile(filename);
  37. return new FileEngine(engine);
  38. }
  39. /// <summary>
  40. /// Specify which file to start writing to
  41. /// </summary>
  42. /// <param name="filename">The filename.</param>
  43. /// <remarks>
  44. /// This will overwrite the file, use <see cref="AppendTo"/> if you want
  45. /// to append.
  46. /// </remarks>
  47. public FileEngine To(string filename)
  48. {
  49. filename = NormalizeFilename(filename);
  50. engine.BeginWriteFile(filename);
  51. return new FileEngine(engine);
  52. }
  53. /// <summary>
  54. /// Specify which file to start appending to
  55. /// </summary>
  56. /// <param name="filename">The filename.</param>
  57. /// <returns></returns>
  58. public FileEngine AppendTo(string filename)
  59. {
  60. engine.BeginAppendToFile(filename);
  61. return new FileEngine(engine);
  62. }
  63. private static string NormalizeFilename(string filename)
  64. {
  65. if (filename.StartsWith("~") == false)
  66. return filename;
  67. //note that this ignores rooted paths
  68. return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
  69. }
  70. ///// <summary>
  71. ///// Gets or sets the options.
  72. ///// </summary>
  73. ///// <value>The options.</value>
  74. //public RecordOptions Options
  75. //{
  76. // get { return engine.RecordType.; }
  77. // set { engine.Options = value; }
  78. //}
  79. /// <summary>
  80. /// Gets or sets the footer text.
  81. /// </summary>
  82. /// <value>The footer text.</value>
  83. public string FooterText
  84. {
  85. get { return engine.FooterText; }
  86. set { engine.FooterText = value; }
  87. }
  88. /// <summary>
  89. /// Gets or sets the header text.
  90. /// </summary>
  91. /// <value>The header text.</value>
  92. public string HeaderText
  93. {
  94. get { return engine.HeaderText; }
  95. set { engine.HeaderText = value; }
  96. }
  97. /// <summary>
  98. /// Gets or sets the encoding.
  99. /// </summary>
  100. /// <value>The encoding.</value>
  101. public Encoding Encoding
  102. {
  103. get { return engine.Encoding; }
  104. set { engine.Encoding = value; }
  105. }
  106. }
  107. }