ASP.NET Core Web API Interview Question #105
What is the service layer in a Web API?
Dependency Injection, Services, EF Core, Async & Data Access Mid-Level Intermediate
Quick Interview Answer
A service layer contains application use cases and coordinates business operations independently from the HTTP controller. It may validate business rules, invoke repositories or DbContext, call external services, and return application results.
Detailed Explanation
A service layer contains application use cases and coordinates business operations independently from the HTTP controller. It may validate business rules, invoke repositories or DbContext, call external services, and return application results.
The service layer should not simply duplicate controller code. Its purpose is to establish a useful boundary around application behavior.
Code Example
public interface IProductService
{
Task<ProductResponse?> GetAsync(
int id,
CancellationToken token);
}