|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
| 1 | +using System.Collections.Generic; |
3 | 2 | using System.IO; |
| 3 | +using System.Linq; |
4 | 4 | using System.Reflection; |
5 | 5 |
|
6 | 6 | namespace NTestDataBuilder.DataSources.Dictionaries |
7 | 7 | { |
8 | 8 | /// <summary> |
9 | | - /// Retrieves words from dictionaries stored in files |
| 9 | + /// Retrieves words from dictionaries stored in files. First looks for external file that user might have created. If this does not exist then data is retrieved from embedded resource files. |
10 | 10 | /// </summary> |
11 | | - public class FileDictionaryRepository : IDictionaryRepository |
| 11 | + public class CachedFileDictionaryRepository : IDictionaryRepository |
12 | 12 | { |
13 | 13 | public IList<string> GetWordsFrom(string dictionary) |
14 | 14 | { |
15 | | - // let users override values with file in bin directory |
| 15 | + if (Cache.Contains(dictionary)) |
| 16 | + { |
| 17 | + return Cache.Get(dictionary); |
| 18 | + } |
| 19 | + |
| 20 | + var words = new List<string>(); |
| 21 | + |
16 | 22 | var name = string.Format("{0}.txt", dictionary); |
17 | 23 | if (File.Exists(name)) |
18 | 24 | { |
19 | | - return File.ReadAllLines(name); |
| 25 | + words = File.ReadAllLines(name).ToList(); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + var resourceName = string.Format("NTestDataBuilder.DataSources.Dictionaries.Resources.{0}", name); |
| 30 | + words = GetWordsFromEmbeddedResource(GetType().Assembly, resourceName).ToList(); |
20 | 31 | } |
21 | 32 |
|
22 | | - // otherwise get data from embedded resource files |
23 | | - var resourceName = string.Format("NTestDataBuilder.DataSources.Dictionaries.Resources.{0}", name); |
24 | | - return GetWordsFromEmbeddedResource(GetType().Assembly, resourceName); |
| 33 | + Cache.Set(dictionary, words); |
| 34 | + return words; |
25 | 35 | } |
26 | 36 |
|
27 | 37 | internal IList<string> GetWordsFromEmbeddedResource(Assembly assembly, string resourceName) |
|
0 commit comments