@@ -16,6 +16,7 @@ public abstract class TestDataBuilder<TObject, TBuilder>
16
16
where TBuilder : TestDataBuilder < TObject , TBuilder > , new ( )
17
17
{
18
18
private readonly Dictionary < string , object > _properties = new Dictionary < string , object > ( StringComparer . InvariantCultureIgnoreCase ) ;
19
+ private readonly Dictionary < string , Func < object > > _propFactories = new Dictionary < string , Func < object > > ( ) ;
19
20
private ProxyBuilder < TObject > _proxyBuilder ;
20
21
21
22
/// <summary>
@@ -107,19 +108,32 @@ public TBuilder AsProxy()
107
108
/// </summary>
108
109
/// <param name="proxy">The proxy object</param>
109
110
protected virtual void AlterProxy ( TObject proxy ) { }
110
-
111
+
111
112
/// <summary>
112
113
/// Records the given value for the given property from {TObject} and returns the builder to allow chaining.
113
114
/// </summary>
114
115
/// <typeparam name="TValue">The type of the property</typeparam>
115
116
/// <param name="property">A lambda expression specifying the property to record a value for</param>
116
- /// <param name="value">The builder so that other method calls can be chained</param>
117
+ /// <param name="value">The value to set the property to</param>
118
+ /// <returns>The builder so that other method calls can be chained</returns>
117
119
public virtual TBuilder Set < TValue > ( Expression < Func < TObject , TValue > > property , TValue value )
118
120
{
119
121
_properties [ Reflector . GetPropertyNameFor ( property ) ] = value ;
120
122
return this as TBuilder ;
121
123
}
122
124
125
+ /// <summary>
126
+ /// Records a given value provider for the given property from {TObject} and returns the builder to allow chaining.
127
+ /// </summary>
128
+ /// <typeparam name="TValue">The type of the property</typeparam>
129
+ /// <param name="property">A lambda expression specifying the property to record a value for</param>
130
+ /// <param name="factory">A method which produces instances of {TValue} for the property.</param>
131
+ public virtual TBuilder Set < TValue > ( Expression < Func < TObject , TValue > > property , Func < TValue > factory )
132
+ {
133
+ _propFactories [ Reflector . GetPropertyNameFor ( property ) ] = ( ) => factory ( ) as object ;
134
+ return this as TBuilder ;
135
+ }
136
+
123
137
/// <summary>
124
138
/// Gets the recorded value for the given property from {TObject} or an anonymous
125
139
/// value if there isn't one specified.
@@ -129,10 +143,7 @@ public virtual TBuilder Set<TValue>(Expression<Func<TObject, TValue>> property,
129
143
/// <returns>The recorded value of the property or an anonymous value for it</returns>
130
144
public TValue Get < TValue > ( Expression < Func < TObject , TValue > > property )
131
145
{
132
- if ( ! Has ( property ) )
133
- return Any . Get ( property ) ;
134
-
135
- return ( TValue ) _properties [ Reflector . GetPropertyNameFor ( property ) ] ;
146
+ return ( TValue ) Get ( typeof ( TValue ) , Reflector . GetPropertyNameFor ( property ) ) ;
136
147
}
137
148
138
149
/// <summary>
@@ -144,9 +155,13 @@ public TValue Get<TValue>(Expression<Func<TObject, TValue>> property)
144
155
/// <returns></returns>
145
156
public object Get ( Type type , string propertyName )
146
157
{
147
- if ( ! Has ( propertyName ) )
148
- return Any . Get ( type , propertyName ) ;
149
- return _properties [ propertyName ] ;
158
+ object value ;
159
+ if ( _properties . TryGetValue ( propertyName , out value ) ) return value ;
160
+
161
+ Func < object > factory ;
162
+ if ( _propFactories . TryGetValue ( propertyName , out factory ) ) return factory ( ) ;
163
+
164
+ return Any . Get ( type , propertyName ) ;
150
165
}
151
166
152
167
/// <summary>
@@ -193,7 +208,7 @@ protected bool Has<TValue>(Expression<Func<TObject, TValue>> property)
193
208
/// <returns>Whether or not there is a recorded value for the property</returns>
194
209
protected bool Has ( string propertyName )
195
210
{
196
- return _properties . ContainsKey ( propertyName ) ;
211
+ return _properties . ContainsKey ( propertyName ) || _propFactories . ContainsKey ( propertyName ) ;
197
212
}
198
213
199
214
/// <summary>
0 commit comments