diff --git a/Source/Bogus/DataSets/System.cs b/Source/Bogus/DataSets/System.cs index 46c8781c..8fdcb594 100644 --- a/Source/Bogus/DataSets/System.cs +++ b/Source/Bogus/DataSets/System.cs @@ -17,41 +17,47 @@ public class System : DataSet /// The locale that will be used to generate values. public System(string locale = "en") : base(locale) { - mimes = this.GetArray("mimeTypes"); - - lookup = mimes.OfType() - .ToDictionary(o => o["mime"].StringValue); - - mimeKeys = mimes - .OfType() - .Select(o => o["mime"].StringValue) - .Distinct() - .ToArray(); - - exts = mimes - .OfType() - .SelectMany(bObject => + mimes = new Lazy( + () => this.GetArray("mimeTypes")); + + lookup = new Lazy>( + () => mimes.Value.OfType() + .ToDictionary(o => o["mime"].StringValue)); + + mimeKeys = new Lazy( + () => mimes.Value + .OfType() + .Select(o => o["mime"].StringValue) + .Distinct() + .ToArray()); + + exts = new Lazy( + () => mimes.Value + .OfType() + .SelectMany(bObject => { - if( bObject.ContainsKey("extensions") ) + if (bObject.ContainsKey("extensions")) { var extensions = bObject["extensions"] as BArray; return extensions.OfType().Select(s => s.StringValue); } + return Enumerable.Empty(); }) - .ToArray(); - - types = mimeKeys.Select(k => k.Substring(0, k.IndexOf('/'))) - .Distinct() - .ToArray(); + .ToArray()); + + types = new Lazy( + () => mimeKeys.Value.Select(k => k.Substring(0, k.IndexOf('/'))) + .Distinct() + .ToArray()); } protected Lorem Lorem = null; - private readonly Dictionary lookup; - private readonly BArray mimes; - private readonly string[] exts; - private readonly string[] types; - private readonly string[] mimeKeys; + private readonly Lazy> lookup; + private readonly Lazy mimes; + private readonly Lazy exts; + private readonly Lazy types; + private readonly Lazy mimeKeys; private static readonly string[] commonFileTypes = { "video", "audio", "image", "text", "application" }; @@ -145,7 +151,7 @@ public string CommonFileName(string ext = null) /// public string MimeType() { - return this.Random.ArrayElement(this.mimeKeys); + return this.Random.ArrayElement(this.mimeKeys.Value); } @@ -181,7 +187,7 @@ public string CommonFileExt() /// public string FileType() { - return this.Random.ArrayElement(this.types); + return this.Random.ArrayElement(this.types.Value); } @@ -194,13 +200,13 @@ public string FileType() public string FileExt(string mimeType = null) { if( mimeType != null && - lookup.TryGetValue(mimeType, out var mime) && + lookup.Value.TryGetValue(mimeType, out var mime) && mime.ContainsKey("extensions") ) { return this.Random.ArrayElement(mime["extensions"] as BArray); } - return this.Random.ArrayElement(exts); + return this.Random.ArrayElement(exts.Value); } ///