Skip to content

Granit.Analyzers

Granit.Analyzers provides Roslyn analyzers that enforce Granit conventions at compile time, with immediate feedback in the IDE (red squiggles). They complement runtime checks and architecture tests with zero-cost static analysis.

Terminal window
dotnet add package Granit.Analyzers

The package ships both analyzers and code fix providers. No additional configuration is required — rules activate automatically based on the project’s dependencies.

Rules enforcing modular monolith boundaries.

RuleSeverityCodeFixDescription
GRMOD001ErrorYesCross-module reference to internal type — use Contracts

GRMOD001 — Cross-module reference detection

Section titled “GRMOD001 — Cross-module reference detection”

Detects when code in one module references internal types from another module instead of using the .Contracts namespace.

Applies to: projects using the *.Modules.{ModuleName} namespace convention.

Opt-in: the analyzer only activates when *.Modules.* namespaces are present in the compilation. Projects without this convention pay zero runtime cost.

Allowed references:

  • Types in *.Modules.{Module}.Contracts and sub-namespaces
  • Types within the same module
  • Types outside *.Modules.* (framework, BCL, shared libraries)

Forbidden references:

  • Types in *.Modules.{OtherModule}.Domain
  • Types in *.Modules.{OtherModule}.Handlers
  • Types in *.Modules.{OtherModule}.Services
  • Any non-Contracts namespace of another module

CodeFix: if a type with the same name exists in the target module’s .Contracts namespace, the code fix suggests replacing the using directive.

// ❌ GRMOD001: InventoryService from module Inventory cannot be
// referenced from module Orders
using MyApp.Modules.Inventory.Services;
namespace MyApp.Modules.Orders.Handlers;
public class OrderHandler(InventoryService inventory) { }

Rules enforcing zero-downtime migration patterns (expand/migrate/contract).

RuleSeverityCodeFixDescription
GRMIGA001ErrorDropColumn requires a Contract-phase annotation
GRMIGA002ErrorRenameColumn is not zero-downtime safe
GRMIGA003WarningAddColumn NOT NULL without default risks table lock
GRMIGA004WarningAlterColumn type change requires Contract-phase annotation

Rules enforcing testability, secret management, and GDPR compliance.

RuleSeverityCodeFixDescription
GRSEC001WarningYesAvoid DateTime.Now/UtcNow — use IClock
GRSEC002WarningYesAvoid Guid.NewGuid() — use IGuidGenerator
GRSEC003ErrorPotential hardcoded secret detected
GRSEC004WarningYesAvoid direct IResponseCookies — use IGranitCookieManager

Rules enforcing async-first EF Core patterns.

RuleSeverityCodeFixDescription
GREF001WarningYesUse SaveChangesAsync() instead of SaveChanges()

Rules enforcing Minimal API conventions and RFC 7807 compliance.

RuleSeverityCodeFixDescription
GRAPI001WarningYesUse TypedResults instead of Results for OpenAPI
GRAPI002WarningYesUse TypedResults.Problem() instead of BadRequest (RFC 7807)

To suppress a specific rule for a file or project:

// File-level suppression
#pragma warning disable GRMOD001
<!-- Project-level suppression in .csproj -->
<PropertyGroup>
<NoWarn>$(NoWarn);GRMOD001</NoWarn>
</PropertyGroup>