writing-server-code

Conventions de code serveur Bitwarden pour C# et .NET. À utiliser lors du travail dans le référentiel serveur, de la création de commandes, de requêtes, de services ou de points de terminaison API. À utiliser également lors de…

npx skills add https://github.com/bitwarden/server --skill writing-server-code

Architectural Rationale

Command Query Separation (CQS)

New features should use the CQS pattern — discrete action classes instead of large entity-focused services. See ADR-0008.

Why CQS matters at Bitwarden: The codebase historically grew around entity-focused services (e.g., CipherService) that accumulated hundreds of methods. CQS breaks these into single-responsibility classes (CreateCipherCommand, GetOrganizationApiKeyQuery), making code easier to test, reason about, and modify without unintended side effects.

Commands = write operations. Change state, may return result. Named after the action: RotateOrganizationApiKeyCommand.

Queries = read operations. Return data, never change state.

When NOT to use CQS: When modifying existing service-based code, follow the patterns already in the file. Don't refactor to CQS unless explicitly asked. If asked to refactor, apply the pattern only to the scope requested.

Caching

When caching is needed, follow the conventions in CACHING.md. Use IFusionCache instead of IDistributedCache.

Don't implement caching unless requested. If a user describes a performance problem where caching might help, suggest it — but don't implement without confirmation.

GUID Generation

Always use CoreHelpers.GenerateComb() for entity IDs — never Guid.NewGuid(). Sequential COMBs prevent SQL Server index fragmentation that random GUIDs cause on clustered indexes, which is critical for Bitwarden's database performance at scale.

Library shape

When creating or modifying code under src/Libraries/, read src/Libraries/LIBRARY.md — it is the canonical shape and covers public surface, settings, endpoints, repositories, and cross-library dependencies.

Critical Rules

These are the most frequently violated conventions. Claude cannot fetch the linked docs at runtime, so these are inlined here:

  • Use TryAdd* for DI registration (TryAddScoped, TryAddTransient) — prevents duplicate registrations when multiple modules register the same service
  • File-scoped namespacesnamespace Bit.Core.Vault; not namespace Bit.Core.Vault { ... }
  • Nullable reference types are enabled (ADR-0024) — use ! (null-forgiving) when you know a value isn't null; use required modifier for properties that must be set during construction
  • Async suffix on all async methodsCreateAsync, not Create, when the method returns Task
  • Controller actions return ActionResult<T> — not IActionResult or bare T
  • Testing with xUnit — use [Theory, BitAutoData] (not [AutoData]), SutProvider<T> for automatic SUT wiring, and Substitute.For<T>() from NSubstitute for mocking

Examples

GUID generation

// CORRECT — sequential COMB prevents index fragmentation
var id = CoreHelpers.GenerateComb();

// WRONG — random GUIDs fragment clustered indexes
var id = Guid.NewGuid();

DI registration

// CORRECT — idempotent, won't duplicate
services.TryAddScoped<ICipherService, CipherService>();

// WRONG — silently duplicates registration, last-wins causes subtle bugs
services.AddScoped<ICipherService, CipherService>();

Namespace style

// CORRECT — file-scoped
namespace Bit.Core.Vault.Commands;

// WRONG — block-scoped
namespace Bit.Core.Vault.Commands
{
    // ...
}

Further Reading

Plus de skills de bitwarden

analyzing-git-sessions
bitwarden
Analyse les commits et modifications Git dans un intervalle de temps ou une plage de commits, fournissant des résumés structurés pour la revue de code, les rétrospectives, les journaux de travail ou les sessions…
official
figma-to-angular
bitwarden
Cette compétence transforme un cahier des charges Figma en un composant Angular entièrement implémenté avec des stories Storybook dans le monorepo Bitwarden Clients. Le résultat doit correspondre visuellement au design tout en respectant toutes les conventions du codebase.
official
agent-access
bitwarden
Retrieve login credentials, API keys, and secrets (username, password, TOTP) from the user's Bitwarden vault via aac. Use when you need credentials to sign…
official
action-audit
bitwarden
Auditer l'utilisation des actions GitHub Actions dans une organisation. Recherche une action spécifique (mode incident) ou analyse tous les fichiers de workflow pour détecter les actions non conformes…
official
action-remediate
bitwarden
Remediate GitHub Actions action findings identified by the action-audit skill. Applies the appropriate fix per action type — `@main` ref for internal…
official
analyzing-code-security
bitwarden
This skill should be used when the user asks to "analyze code for security issues", "check for OWASP vulnerabilities", "review code against CWE Top 25", "find…
official
applying-bitwarden-branding
bitwarden
Appliquer les normes de marque Bitwarden — utilisation du logo, palette de couleurs, typographie, iconographie et règles de capitalisation — fondées sur bitwarden.com/brand et les…
official
architecting-solutions
bitwarden
Architecting solutions at the team level while staying coherent with Bitwarden's holistic architecture. Covers security mindset, architectural judgment,…
official