Skip to content

New Configuration Idea: Make S6960 excluded services configurable #9826

Description

@yyueniao

Description

Rule S6960 (Controllers should not have mixed responsibilities) ignores a hardcoded list of well-known services when grouping controller responsibilities:

  • ILogger
  • IMediator
  • IMapper
  • IConfiguration
  • IBus
  • IMessageBus
  • IHttpClientFactory

Projects often have additional shared infrastructure services (for example IOptions<T>, custom logging/telemetry wrappers, or shared application services) that should be treated the same way. Today there is no way to extend or replace this list without suppressing the rule.

Please add a rule parameter so the excluded service type names can be configured via SonarQube / SonarQube Cloud quality profiles and SonarQube for IDE (SonarLint.xml).

Proposed parameter

Key Type Default
excludedServices String (comma-separated type names) ILogger, IMediator, IMapper, IConfiguration, IBus, IMessageBus, IHttpClientFactory

Matching should keep the current behavior: compare against the simple type name (for example ILogger matches ILogger<T>).

Setting the parameter replaces the default list entirely (same pattern as other parametrized rules such as S2068 credentialWords and S110 filteredClasses).

Example

[ApiController]
public class OrdersController(
    IMyTelemetry telemetry, // shared infra; should be excludable
    IOrderService orders,
    IInvoiceService invoices) : ControllerBase
{
    public IActionResult GetOrder(int id) => Ok(orders.Get(id));
    public IActionResult GetInvoice(int id) => Ok(invoices.Get(id));
}

Current behavior: issue is raised (telemetry + two domain services form mixed responsibilities).

Expected with excludedServices=ILogger, IMediator, IMapper, IConfiguration, IBus, IMessageBus, IHttpClientFactory, IMyTelemetry: issue is still raised only for the two domain services; IMyTelemetry is ignored like other shared infrastructure.

Configuration

<!-- SonarLint.xml / quality profile parameter -->
<Rule>
  <Key>S6960</Key>
  <Parameters>
    <Parameter>
      <Key>excludedServices</Key>
      <Value>ILogger, IMediator, IMapper, IConfiguration, IBus, IMessageBus, IHttpClientFactory, IMyTelemetry</Value>
    </Parameter>
  </Parameters>
</Rule>

Product and Version

SonarAnalyzer.CSharp / SonarQube for IDE / SonarQube Server & Cloud (rule S6960)

Noncompliant code snippet

// `excludedServices` = `ILogger, IMediator, IMapper, IConfiguration, IBus, IMessageBus, IHttpClientFactory`
[ApiController]
public class OrdersController(
    IMyTelemetry telemetry,
    IOrderService orders
{
    public IActionResult GetOrder(int id) => Ok(orders.Get(id));
    public IActionResult GetInvoice(int id) => Ok(invoices.Get(id));
}

Compliant code snippet

// `excludedServices` = `ILogger, IMediator, IMapper, IConfiguration, IBus, IMessageBus, IHttpClientFactory, IMyTelemetry`
[ApiController]
public class OrdersController(
    IMyTelemetry telemetry,
    IOrderService orders
{
    public IActionResult GetOrder(int id) => Ok(orders.Get(id));
    public IActionResult GetInvoice(int id) => Ok(invoices.Get(id));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions