Skip to content

Commit

Permalink
Lazy load person data
Browse files Browse the repository at this point in the history
  • Loading branch information
JvanderStad committed Feb 25, 2016
1 parent 4990164 commit 6695892
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
1 change: 0 additions & 1 deletion Source/Bogus.Tests/PersonTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using Bogus.Extensions.Brazil;
using Bogus.Extensions.Canada;
using Bogus.Extensions.Denmark;
Expand Down
23 changes: 20 additions & 3 deletions Source/Bogus/Faker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Bogus
/// <summary>
/// A hub of all the categories merged into a single class to ease fluent syntax API.
/// </summary>
public class Faker
public class Faker : ILocaleAware
{
/// <summary>
/// The default mode to use when generating objects. Strict mode ensures that all properties have rules.
Expand All @@ -21,6 +21,8 @@ public class Faker
/// </summary>
public Faker(string locale = "en")
{
Locale = locale;

this.Address = new Address(locale);
this.Company = new Company(locale);
this.Date = new Date {Locale = locale};
Expand All @@ -32,7 +34,6 @@ public Faker(string locale = "en")
this.Name = new Name(locale);
this.Phone = new PhoneNumbers(locale);

this.Person = new Person(locale);
this.Random = new Randomizer();
}

Expand All @@ -54,10 +55,12 @@ public string Parse(string str)
this.Phone);
}


private Person _person;
/// <summary>
/// A contextually relevant fields of a person.
/// </summary>
public Person Person { get; set; }
public Person Person => _person ?? (_person = new Person( Locale ));

/// <summary>
/// Creates hacker gibberish.
Expand Down Expand Up @@ -130,6 +133,20 @@ public T PickRandom<T>(IEnumerable<T> items)
{
return this.Random.Enum<T>();
}

/// <summary>
/// The current locale for the dataset.
/// </summary>
/// <value>The locale.</value>
public string Locale { get; set; }

/// <summary>
/// Resets the data.
/// </summary>
public void ResetData()
{
_person = null;
}
}


Expand Down
16 changes: 2 additions & 14 deletions Source/Bogus/Faker[T].cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Linq.Expressions;
using System.Reflection;


namespace Bogus
{
/// <summary>
Expand Down Expand Up @@ -36,14 +35,6 @@ public Faker(string locale = "en", IBinder binder = null)
TypeProperties = this.binder.GetMembers(typeof(T));
}

/// <summary>
/// Forcibly makes a new person context.
/// </summary>
public virtual void MakeNewContext()
{
this.FakerHub.Person = new Person(this.Locale);
}

/// <summary>
/// Set the binding flags visibility when getting properties. IE: Only public or public+private properties.
/// </summary>
Expand Down Expand Up @@ -184,12 +175,9 @@ public virtual void Populate(T instance)
field?.SetValue(instance, valueFactory(FakerHub, instance));
}

if( FinalizeAction != null )
{
FinalizeAction(this.FakerHub, instance);
}
FinalizeAction?.Invoke(this.FakerHub, instance);

MakeNewContext();
FakerHub.ResetData();
}
}

Expand Down

0 comments on commit 6695892

Please sign in to comment.