SOLID & Design Patterns Interview Question #107
Where should interfaces be placed in a layered or Clean Architecture solution?
Architectural Patterns, Application Boundaries & Distributed Design Patterns Senior Advanced
Quick Interview Answer
An interface should normally be owned by the layer that needs the abstraction, not automatically by the class that implements it. If the application layer needs persistence behavior, the persistence port can live in Application or Domain depending on the design.
Detailed Explanation
An interface should normally be owned by the layer that needs the abstraction, not automatically by the class that implements it. If the application layer needs persistence behavior, the persistence port can live in Application or Domain depending on the design.
Infrastructure then implements that contract. This keeps dependency direction aligned with the consumer's needs and prevents the core from depending on infrastructure.
Code Example
// Application
public interface IEmailSender
{
Task SendAsync(EmailMessage message);
}
// Infrastructure
public class SmtpEmailSender : IEmailSender
{
}