Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. class Document
  2. {
  3. public string FileData { get; set; }
  4. public string FileRelativePath { get; set; }
  5. }
  6.  
  7. interface IDocumentRepository
  8. {
  9. Document Get(int id);
  10. }
  11.  
  12.  
  13. abstract class DocumentFactory
  14. {
  15. public abstract Document Create(int docId);
  16. }
  17.  
  18. interface IFileStore
  19. {
  20. string Read(string fileName);
  21. }
  22.  
  23. class ConcreteDocumentFactory : DocumentFactory
  24. {
  25. private IDocumentRepository _db;
  26. private IFileStore _fileStore;
  27.  
  28. public ConcreteDocumentFactory(IDocumentRepository db, IFileStore fileStore)
  29. {
  30. _db = db;
  31. _fileStore = fileStore;
  32. }
  33.  
  34. public override Document Create(int docId)
  35. {
  36.  
  37. Document newDoc = _db.Get(docId);
  38. newDoc.FileData = _fileStore.Read(newDoc.FileRelativePath);
  39. return newDoc;
  40. }
  41. }
  42.  
  43.  
  44. /////// Test Code Below
  45.  
  46. [TestFixture]
  47. class TestClass
  48. {
  49.  
  50. class TestFriendlyFileStore : IFileStore
  51. {
  52. public string Read(string fileName)
  53. {
  54.  
  55. if (fileName == "sample.txt")
  56. return "Some File Content";
  57. throw new Exception("Not good file name.");
  58. }
  59. }
  60.  
  61.  
  62. class TestFriendlyDocRepo : IDocumentRepository
  63. {
  64. public Document Get(int id)
  65. {
  66. if (id != 999)
  67. return new Document() {FileRelativePath = "sample.txt"};
  68. throw new Exception("Not good id.");
  69.  
  70. }
  71. }
  72.  
  73. [Test]
  74. public void Test()
  75. {
  76. var concreteDocFactory = new ConcreteDocumentFactory(new TestFriendlyDocRepo(), new TestFriendlyFileStore());
  77. var doc = concreteDocFactory.Create(999);
  78. Assert.AreEqual(doc.FileData == "Some File Content")
  79.  
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement