What is CQRS and when would you use it?
Answer: Command Query Responsibility Segregation separates read operations (queries) from write operations (commands) with different models and often different data stores. Use CQRS when:
- Read and write workloads have significantly different scaling needs
- Performance optimization requires different schema designs
- Write operations need stronger consistency while reads can be eventually consistent
- Complex domain requires simpler view models
- Event sourcing is being implemented
However, CQRS adds complexity and should be applied selectively to bounded contexts that benefit from it, not as a system-wide architecture.
Explain the concept of Domain-Driven Design (DDD) and how C# supports it.
Answer: Domain-Driven Design is an approach focusing on the core domain and business logic, using a ubiquitous language to bridge technical and domain experts. C# supports DDD with:
- Classes and encapsulation for Entities and Value Objects
- Interfaces, abstract classes, and events for Domain Services
- Immutable types with records for Value Objects
- Access modifiers to enforce Aggregate boundaries
- Extension methods for domain-specific language
- Attributes for metadata and validation
- Rich domain model capabilities with behavior encapsulation
- Expression trees for specifications pattern
Key DDD concepts like Bounded Contexts, Aggregates, and Repositories map well to C# language features.