I want to use autofixture to create an object graph where children have a reference to the parent object. For example :
class A
{
public List<B> Children { get; set; }
}
class B
{
public A Parent { get; set; }
}
I tried to make a behavior that handles the recursion, but I don't know how to emit the parent object as the content of the property.
public class AutoParentOnRecursionBehavior : ISpecimenBuilderTransformation
{
public ISpecimenBuilder Transform(ISpecimenBuilder builder)
{
if (builder == null)
throw new ArgumentNullException("builder");
return new RecursionGuard(builder, new AutoParentOnRecursionHandler());
}
}
public class AutoParentOnRecursionHandler : IRecursionHandler
{
public object HandleRecursiveRequest(
object request,
IEnumerable<object> recordedRequests)
{
object handleRecursiveRequest = recordedRequests.First(x => x.Equals(request));
return ....
}
}
Thanks.
Edit : I am thinking of a generic way, without having to specify the types A and B or even the property Children. For all properties of a property type that contains the object, set them to the parent object. In other words all properties of a type that trigger the recursion guard set them to the last object in the creation hierarchy.