1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . Collections . Generic ;
3
4
using System . Linq ;
4
5
using Bogus ;
@@ -19,6 +20,9 @@ public class AutoFaker<TType> : Faker<TType> where TType : class
19
20
{
20
21
public AutoFakerConfig Config { get ; set ; }
21
22
23
+ private ConcurrentDictionary < int , AutoFakerContext > ContextLoopUp { get ; } = new ( ) ;
24
+
25
+
22
26
/// <summary>
23
27
/// The <see cref="AutoFakerBinder"/> instance to use for the generation request.
24
28
/// </summary>
@@ -63,14 +67,23 @@ public override TType Generate(string? ruleSets = null)
63
67
{
64
68
Initialize ( ) ;
65
69
66
- AutoFakerContext context = CreateContext ( ruleSets ) ;
70
+ CreateGenerateContext ( ruleSets ) ;
67
71
68
- PrepareCreate ( context ) ;
69
- PrepareFinish ( context ) ;
72
+ PrepareCreate ( ) ;
73
+ PrepareFinish ( ) ;
70
74
71
75
return base . Generate ( ruleSets ) ;
72
76
}
73
77
78
+ private void CreateGenerateContext ( string ? ruleSets )
79
+ {
80
+ lock ( ContextLoopUp )
81
+ {
82
+ // Think 1 needs to be added since the generate method increments the context
83
+ ContextLoopUp [ FakerHub . IndexFaker + 1 ] = CreateContext ( ruleSets ) ;
84
+ }
85
+ }
86
+
74
87
/// <summary>
75
88
/// Generates a collection of instances of type <typeparamref name="TType"/>.
76
89
/// </summary>
@@ -81,10 +94,10 @@ public override List<TType> Generate(int count, string? ruleSets = null)
81
94
{
82
95
Initialize ( ) ;
83
96
84
- AutoFakerContext context = CreateContext ( ruleSets ) ;
85
-
86
- PrepareCreate ( context ) ;
87
- PrepareFinish ( context ) ;
97
+ CreateGenerateContext ( ruleSets ) ;
98
+
99
+ PrepareCreate ( ) ;
100
+ PrepareFinish ( ) ;
88
101
89
102
return base . Generate ( count , ruleSets ) ;
90
103
}
@@ -96,8 +109,8 @@ public override List<TType> Generate(int count, string? ruleSets = null)
96
109
/// <param name="ruleSets">An optional list of delimited rule sets to use for the populate request.</param>
97
110
public override void Populate ( TType instance , string ? ruleSets = null )
98
111
{
99
- AutoFakerContext context = CreateContext ( ruleSets ) ;
100
- PrepareFinish ( context ) ;
112
+ CreateGenerateContext ( ruleSets ) ;
113
+ PrepareFinish ( ) ;
101
114
102
115
base . Populate ( instance , ruleSets ) ;
103
116
}
@@ -141,14 +154,24 @@ private List<string> ParseRuleSets(string? ruleSets)
141
154
return validRuleSets ;
142
155
}
143
156
144
- private void PrepareCreate ( AutoFakerContext context )
157
+ protected AutoFakerContext GetContext ( Faker fakerHub )
158
+ {
159
+ if ( ! ContextLoopUp . TryGetValue ( fakerHub . IndexFaker , out var context ) )
160
+ throw new Exception ( "I done F'd up" ) ;
161
+
162
+ return context ;
163
+ }
164
+
165
+ private void PrepareCreate ( )
145
166
{
146
167
// Check a create handler hasn't previously been set or configured externally
147
168
if ( _createInitialized || CreateActions [ currentRuleSet ] != null )
148
169
return ;
149
170
150
171
CreateActions [ currentRuleSet ] = faker =>
151
172
{
173
+ var context = GetContext ( faker ) ;
174
+
152
175
// Only auto create if the 'default' rule set is defined for generation
153
176
// This is because any specific rule sets are expected to handle the full creation
154
177
if ( context . RuleSets != null && context . RuleSets . Contains ( currentRuleSet ) )
@@ -198,7 +221,7 @@ private void PrepareCreate(AutoFakerContext context)
198
221
_createInitialized = true ;
199
222
}
200
223
201
- private void PrepareFinish ( AutoFakerContext context )
224
+ private void PrepareFinish ( )
202
225
{
203
226
if ( _finishInitialized )
204
227
return ;
@@ -207,6 +230,8 @@ private void PrepareFinish(AutoFakerContext context)
207
230
208
231
FinishWith ( ( faker , instance ) =>
209
232
{
233
+ var context = GetContext ( faker ) ;
234
+
210
235
if ( instance == null )
211
236
return ;
212
237
0 commit comments