title: How can I share state between Bogus (C# Faker) rules?
body: <p>I'm using <a href="https://github.com/bchavez/Bogus" rel="nofollow noreferrer">Bogus</a> to generate test data but I have some fields that depend on parts of another object (that I want to be chosen randomly for each generation) but that have to be consistent with each other.</p>
  <p>That's probably not the best explanation so hopefully this example explains it better.</p>
  <p>I have an <code>Order</code> which includes the ID and currency from a <code>Customer</code>.</p>
  <pre><code>public class Customer
  {
      public Guid Id { get; set; }
      public string Currency { get; set; }
  }
  
  public class Order
  {
      public Guid Id { get; set; }
      public Guid CustomerId { get; set; } // The ID of a customer
      public string Currency { get; set; } // The currency for the customer identified by CustomerId
      public decimal Amount { get; set; }
  }
  </code></pre>
  <p>I can generate some customers using:</p>
  <pre><code>var customerFaker = 
      new Faker&lt;Customer&gt;()
      .StrictMode(true)
      .RuleFor(&quot;Id&quot;, f =&gt; f.Random.Guid())
      .RuleFor(&quot;Id&quot;, f =&gt; f.Finance.Currency());
  
  var customers = customerFaker.Generate(10);
  </code></pre>
  <p>But I get stuck when it comes to &quot;sharing&quot; the customer that's been chosen between rules in the order generator:</p>
  <pre><code>var orderFaker =
      new Faker&lt;Order&gt;()
      .StrictMode(true)
      .RuleFor(&quot;Id&quot;, f =&gt; f.Random.Guid())
      .RuleFor(&quot;Amount&quot;, f =&gt; f.Finance.Amount())
      .RuleFor(&quot;CustomerId&quot;, f =&gt; f.PickRandom(customers).Id)
      //How do I share the Customer that's been chosen to use it in the rule below?
      .RuleFor(&quot;Currency&quot;, f =&gt; f.PickRandom(customers).Currency);
  </code></pre>
  <p>I've come up with a few less-than-ideal ways of doing it (like instantiating a new Faker each time and passing in a random customer) but I'm working with quite complicated objects and dependencies so I'd like to avoid that if possible.</p>
  <p>My current thinking is that the best way might be to extend the <code>Order</code> class to be able to store the <code>Customer</code> and then cast it back to being an order later.  I'd like to avoid this if possible given the number of models I'll need to do this for.</p>
  <pre><code>public class OrderWithCustomer : Order
  {
      public Customer Customer { get; set; }
  }
  
  var orderWithCustomerFaker =
      new Faker&lt;OrderWithCustomer&gt;()
      .StrictMode(true)
      .RuleFor(&quot;Id&quot;, f =&gt; f.Random.Guid())
      .RuleFor(&quot;Amount&quot;, f =&gt; f.Finance.Amount())
      .RuleFor(&quot;Customer&quot;, f =&gt; f.PickRandom(customers))
      .RuleFor(&quot;CustomerId&quot;, (f, o) =&gt; o.Customer.Id)
      .RuleFor(&quot;Currency&quot;, (f, o) =&gt; o.Customer.Currency);
  
  var orders = 
      orderWithCustomerFaker
      .Generate(10)
      .Select(withCustomer =&gt; (Order)withCustomer);
  </code></pre>