Skip to main content
Bitwarden Server follows clean architecture principles with clear separation of concerns between domain logic, data access, and application services.

Architectural Patterns

Layered Architecture

The application is organized into distinct layers:

Layer Responsibilities

Presentation Layer (src/Api/Controllers/)
  • HTTP request/response handling
  • Input validation
  • DTO mapping
  • Authorization checks
Application Layer (src/Core/Services/)
  • Business logic orchestration
  • Transaction management
  • Cross-cutting concerns
  • Domain event coordination
Domain Layer (src/Core/Entities/, src/Core/{Domain}/)
  • Entity definitions
  • Business rules
  • Domain invariants
  • Value objects
Data Access Layer (src/Infrastructure.Dapper/, src/Infrastructure.EntityFramework/)
  • Database queries
  • ORM mappings
  • Data persistence
  • Query optimization

Core Patterns

Repository Pattern

Repositories abstract data access and provide a collection-like interface:
src/Core/Repositories/IRepository.cs
Benefits:
  • Decouples business logic from data access
  • Enables unit testing with mocks
  • Allows switching between Dapper and EF
  • Centralizes query logic
Example Implementation:
src/Infrastructure.Dapper/Repositories/UserRepository.cs

Service Pattern

Services contain business logic and coordinate between repositories:
src/Core/Services/IUserService.cs
Service Responsibilities:
  • Validate business rules
  • Coordinate multiple repositories
  • Manage transactions
  • Trigger side effects (emails, events, push notifications)
Example:

Entity Pattern

Entities represent domain objects with identity:
src/Core/Entities/User.cs
Entity Interfaces:
src/Core/Entities/ITableObject.cs
Key Characteristics:
  • Have unique identity (Id)
  • Contain business logic methods
  • Maintain their own invariants
  • Track creation and revision dates

Domain Organization

Domain-Driven Design Structure

The codebase is organized by domain:

Bounded Contexts

Each domain represents a bounded context with:
  • Entities - Core domain objects
  • Services - Domain operations
  • Repositories - Data access contracts
  • Enums - Domain-specific enumerations
  • Models - DTOs and value objects

Key Entities

User

Represents a Bitwarden user account:
src/Core/Entities/User.cs

Organization

Represents a shared vault organization:
src/Core/AdminConsole/Entities/Organization.cs

Cipher

Represents a vault item (password, note, card, identity):
src/Core/Vault/Entities/Cipher.cs

Entity Relationships

Key Relationships

  • User ↔ Cipher: One-to-many (personal vault items)
  • Organization ↔ Cipher: One-to-many (organization vault items)
  • User ↔ Organization: Many-to-many via OrganizationUser
  • Cipher ↔ Collection: Many-to-many via CollectionCipher
  • User ↔ Group: Many-to-many via GroupUser

Dependency Injection

All dependencies are registered in Startup.cs:
src/Api/Startup.cs

Service Lifetimes

  • Scoped - Most services and repositories (per HTTP request)
  • Singleton - Application cache, configuration
  • Transient - Lightweight utilities

Command Pattern

Complex operations use the command pattern:

Error Handling

Custom Exceptions

src/Core/Exceptions/

Exception Middleware

Exceptions are caught and converted to appropriate HTTP responses:

Authorization

Permission-Based Authorization

Event Sourcing

Audit events are tracked for compliance:
Events are written to the Events service and processed asynchronously.

Caching Strategy

Cached data:
  • Organization abilities (permissions)
  • User premium status
  • Feature flags

See Also