-
Notifications
You must be signed in to change notification settings - Fork 0
Extension ICircularDependencyService
Paul Keller edited this page Mar 14, 2026
·
5 revisions
Use this extension to control how circular dependencies are handled during object creation.
- You use the
SetDependencyDefaultmethod to define a default property, that depends on another property. - You have a circular
SetDependencyDefaultconfiguration, where property A depends on property B, and property B depends on property A in some way - You want to provide default values for circular dependencies, instead of throwing an exception.
using ConstructorCustomization.AutoFixture.Customization.Application.Ports;
public sealed class CircularDependencyService : ICircularDependencyService
{
public object? HandleCircularDependency(string propertyName, Func<IFixture, object?> valueFactory)
{
return null;
}
}
public sealed class CircularDependencyServiceFactory : ICircularDependencyServiceFactory
{
public ICircularDependencyService Create() => new CircularDependencyService();
}Call UseCircularDependencyServiceFactory inside your Configure override:
public class MyCustomization : ConstructorCustomization<MyType, MyCustomization>
{
protected override void Configure()
{
UseCircularDependencyServiceFactory(new CircularDependencyServiceFactory());
}
}By default, circular dependencies will cause a CircularDependencyException to be thrown.