Browse pages
LS2018: File Names a Type Outside Its Layer
LS2018 is a compiler check that reports an Application or WebApi file naming a type outside its layer, because a reference against the layer direction couples the file to work its layer never owns. This page states what the rule reports, where the error sits, and how to name only the types the layer allows.
Reference#
What the Rule Enforces#
LS2018 enforces Follow Layer Direction in Every File (litespec/backend/dotnet/architecture#follow-layer-direction-in-every-file). It runs in Application and WebApi. Its category is LiteSpec.Layers. Severity and enablement follow the Analyzers overview.
What the Rule Reports#
The rule reports a name in an Application file that resolves to an Infrastructure or WebApi type, an ASP.NET Core type, or an HttpContext. It reports a name in a WebApi file that resolves to an Infrastructure type, a repository, or a Domain type other than a typed identifier or a shared-kernel value. The diagnostic sits on the name, and its message names the file's layer, the type, and why the layer may not name it.
Failing Example#
using Microsoft.AspNetCore.Http;
internal sealed class SubmitExpenseCommandHandler(IHttpContextAccessor http)
{
}Passing Example#
internal sealed class SubmitExpenseCommandHandler(IExpenseRepository expenses, IClock clock)
{
}How to Fix#
Take only the types the file's layer allows: repositories, ports, and the clock in Application, and the mediator with request and response models in WebApi. Move web input the handler reads into the endpoint, which maps it onto the command before dispatch.
Notes#
What the Rule Ignores#
The rule does not report a name in a Domain, Infrastructure, Worker, or Test file: it runs in Application and WebApi only. From WebApi it does not report a typed identifier, which is a type whose name ends in Id, or a shared-kernel value, which is a type in the product Domain/ folder. It does not report a name that resolves to no project source file.