Avoid sequential coupling and circular dependencies - with examples
Sequential coupling and circular dependencies create hidden constraints, so constructor injection, builders, interfaces and events make code easier to test, change and evolve.
Avoid sequential coupling and circular dependencies - with examples
Last year, we discussed static code analysis and how it helps us to find code quality problems before they are reaching production. Static analysis can identify many problems, but it cannot always explain why one class is depending on the calling order of another class, or why two projects are depending on each other. These dependencies are creating hidden constraints, and later they are making the code difficult to test and change.
In this article, we are going to discuss sequential coupling and circular dependencies, with examples from a fund domain. We will also see how constructor injection, a builder, a single method and dependency inversion can remove these problems.
TL;DR Summary
- Sequential coupling means that methods must be called in a particular order for an object to work correctly.
- Constructor injection makes required initialization explicit, instead of asking every caller to remember
Init()first. - A builder is useful when a valid object needs several construction steps, and a single public method is useful when the sequence is one business operation.
- Circular dependency means that classes or projects are depending on each other, such as
A -> B -> A. - Circular dependencies break layering and can make an Inversion of Control (IoC) container fail while it is creating services.
- Extract an interface, introduce a third abstraction, publish an event or invert the dependency, otherwise the cycle will keep coming back.
Challenges
Hidden calling order
A class is having sequential coupling when one method must be called before another method, but the compiler is not enforcing this rule. For example, a fund price processor may expose Init(), LoadPrices() and Process(). The caller is expected to remember that Init() must come first, then LoadPrices(), and only after that Process() can be called.
When the sequence is not visible, a new developer can call Process() directly from a scheduled job or a test. The result can be a null reference, an empty price list or, even worser, a calculation based on an incomplete state.
Dependencies moving in a circle
Circular dependency is another hidden problem. A FundService may call DocumentService to create a document, while DocumentService calls FundService to read fund details. At class level the direction is FundService -> DocumentService -> FundService.
The same problem can happen at project level. The Funds.Business project references Funds.Documents, and Funds.Documents references Funds.Business. The project may compile only after adding workarounds, or the dependency injection container may fail at runtime when it is trying to construct the services. Above all, the code is no longer having a clear direction.
What is sequential coupling?
Sequential coupling is a form of temporal coupling where two operations are coupled because one operation must happen before another operation, even though the type system is not expressing that requirement.
In simple terms, the object is having an invisible instruction manual: “call this method first, then call that method”. The object is valid only after a sequence, but any caller can use it before the sequence is complete.
Example:
public class FundPriceProcessor
{
private IEnumerable<FundPrice> prices;
public void Init(IPriceRepository repository)
{
// The caller must remember to call this first.
}
public void LoadPrices()
{
// The caller must remember to call this second.
}
public UnitValue Process()
{
return CalculateUnitValue(prices);
}
private UnitValue CalculateUnitValue(IEnumerable<FundPrice> source)
{
return new UnitValue(source.Sum(x => x.Amount));
}
}
Process() is not explaining that Init() and LoadPrices() are required. A compiler can happily accept the following code,
var processor = new FundPriceProcessor();
var unitValue = processor.Process();
The failure is coming later, far away from the place where the invalid object was created.
Why sequential coupling is important?
Sequential coupling is important because it is moving correctness from the class to every caller. If there are ten callers, there are ten places which must remember the sequence. When the application is growing, one missed caller is enough to create a production issue.
It is also making unit tests more complicated. Each test needs to know the setup order before it can verify one behaviour. Refer the FIRST properties from the unit testing article, a test which requires hidden setup is not independent.
Use constructor injection for required dependencies
If the processor cannot work without a repository, inject it through the constructor. A constructed object should be ready to perform its supported operations.
public class FundPriceProcessor
{
private readonly IPriceRepository repository;
public FundPriceProcessor(IPriceRepository repository)
{
this.repository = repository;
}
public UnitValue Process(FundId fundId)
{
var prices = repository.GetPrices(fundId);
return new UnitValue(prices.Sum(x => x.Amount));
}
}
The dependency is now explicit and the IoC container can provide it. There is no Init() method which can be forgotten, and a test can inject a fake IPriceRepository.
Use a builder when construction has several valid steps
Sometimes an object is having many optional values, and a builder is clearer than a constructor with many parameters. The builder can enforce the sequence before it creates the final object.
public class FundDocumentBuilder
{
private FundId fundId;
private UnitValue unitValue;
private DateTime valuationDate;
public FundDocumentBuilder ForFund(FundId value)
{
fundId = value;
return this;
}
public FundDocumentBuilder WithUnitValue(UnitValue value)
{
unitValue = value;
return this;
}
public FundDocumentBuilder On(DateTime value)
{
valuationDate = value;
return this;
}
public FundDocument Build()
{
if (fundId == null || unitValue == null)
throw new InvalidOperationException("Fund and unit value are required.");
return new FundDocument(fundId, unitValue, valuationDate);
}
}
The caller is still using a sequence, but the sequence is now named and the builder is responsible for validating it.
Hide one business sequence behind one method
If several operations are always part of one business operation, expose the business operation rather than each internal step.
public class FundReportService
{
private readonly IPriceRepository prices;
private readonly IDocumentWriter documents;
public FundReportService(
IPriceRepository prices,
IDocumentWriter documents)
{
this.prices = prices;
this.documents = documents;
}
public Document GenerateDailyReport(FundId fundId, DateTime date)
{
var price = prices.GetPrice(fundId, date);
var report = FundReport.Create(fundId, price, date);
return documents.Write(report);
}
}
The service owns the sequence, and the caller only requests GenerateDailyReport.
What is a circular dependency?
A circular dependency exists when a dependency graph contains a path which returns to its starting point, for example FundService -> DocumentService -> FundService.
In simple terms, class A needs class B, and class B needs class A. Neither class is standing on its own, so constructing or changing one class is requiring the other class at the same time.
Example:
public class FundService
{
private readonly DocumentService documents;
public FundService(DocumentService documents)
{
this.documents = documents;
}
}
public class DocumentService
{
private readonly FundService funds;
public DocumentService(FundService funds)
{
this.funds = funds;
}
}
When an IoC container is resolving FundService, it asks for DocumentService. To create that service, it asks again for FundService, failing with a circular dependency exception at runtime. The compiler is not protecting us because each individual type is valid.
The following diagram depicts a circular dependency and the layered dependency after extracting an interface.

Why circular dependencies are important?
Circular dependencies are breaking the direction of a layered design. The presentation layer should call the application layer, and the application layer should use domain abstractions and infrastructure abstractions. When the infrastructure project calls back into the application project, the boundary is no longer meaningful.
It also makes testing difficult. A unit test for FundService must construct DocumentService, which then asks for FundService again. The test is not focused on one behaviour, and mocking the cycle only hides the design problem.
Extract an interface
First, identify what one side really needs. If document generation only needs fund details, it should depend on IFundReader, not the complete FundService.
public interface IFundReader
{
FundDetails GetDetails(FundId fundId);
}
public class DocumentService
{
private readonly IFundReader funds;
public DocumentService(IFundReader funds)
{
this.funds = funds;
}
}
public class FundService : IFundReader
{
private readonly DocumentService documents;
public FundService(DocumentService documents)
{
this.documents = documents;
}
public FundDetails GetDetails(FundId fundId)
{
return new FundDetails(fundId);
}
}
Now the dependency is DocumentService -> IFundReader, and the concrete FundService can implement that abstraction without the document project referencing the business implementation. Register the interface at the composition root, not inside either business class.
Introduce a third abstraction
If both classes need a shared concept, extract an IUnitValueCalculator or a unit-value domain service. The two original classes then depend on the third abstraction, rather than on each other.
Use events for notifications
If the document service only needs to know that a fund valuation was completed, it should not be called synchronously by the fund service. The fund application can publish a FundValued event, and a document handler can react to it. This removes the direct dependency and also makes the business intention clearer.
Events are not a magic solution. Define delivery and failure behaviour, and use dependency inversion where the caller needs an immediate result.
Must Do / Should / Don’t
Must Do
- Must make required dependencies constructor parameters, otherwise an invalid object can be created.
- Must keep project references pointing in one direction, otherwise the layering is only a picture in a document.
- Must register interfaces and implementations at the composition root, where the object graph is visible.
Should
- Should use a builder when the construction sequence is meaningful and has optional parts.
- Should expose one method for one business operation, and keep internal steps private.
- Should use events when another component only needs a notification and does not need a synchronous return value.
- Should review project references regularly, because cycles are easier to remove when they are small.
Don’t
- Don’t expose
Init(),Load()andProcess()as unrelated public methods when one caller must always call them in that order. - Don’t inject a concrete service into another service only to reuse one small method.
- Don’t solve a circular dependency by using a service locator or a static variable. It hides the cycle and creates another blind spot.
- Don’t create a new project just to move the cycle somewhere else, first identify the business abstraction which is missing.
Wrap up
In this article, we have looked at sequential coupling, temporal coupling, circular dependencies between classes and projects, and the ways to fix them with constructor injection, builders, a single business method, interfaces, events and dependency inversion.
I really appreciate that you took the time to read this article. In the next article, I will walk you through why dead code is risky and how to find and remove it safely.
Related Posts
Remove dead code - why and how to find it safely
Dead code increases cognitive load and deployment risk, so this guide explains how to find candidates, confirm they are unused and remove them safely with history as a backup.
Resilient HTTP calls with Polly - retry, timeout and circuit breaker
Polly 5.x adds retry, timeout and circuit breaker policies around HTTP calls, with guidance on composition, transient failures, idempotency and safe handling of writes.
Secure coding basics - OWASP Top 10 (2017) for .NET developers
OWASP Top 10 2017 risks are translated into practical .NET safeguards for injection, authentication, access control, XSS, deserialization, dependencies and logging.