Domain service for Book business logic.
Encapsulates complex business rules that don't belong to a single entity. Uses dependency injection for repository access.
const queryRepo: IBooksQueryRepository = new PrismaBooksQueryRepository();const domainService = new BooksDomainService(queryRepo);try { await domainService.ensureNoDuplicate(isbn, userId); // Safe to create the book} catch (error) { if (error instanceof DuplicateError) { // Handle duplicate ISBN }} Copy
const queryRepo: IBooksQueryRepository = new PrismaBooksQueryRepository();const domainService = new BooksDomainService(queryRepo);try { await domainService.ensureNoDuplicate(isbn, userId); // Safe to create the book} catch (error) { if (error instanceof DuplicateError) { // Handle duplicate ISBN }}
Creates a new BooksDomainService instance.
The query repository for checking duplicates
Validates that no book with the same ISBN exists for the user.
The ISBN to check for duplicates
The user ID for tenant isolation
When a book with this ISBN already exists
This is a domain invariant check that should be called before creating books.
Domain service for Book business logic.
Remarks
Encapsulates complex business rules that don't belong to a single entity. Uses dependency injection for repository access.
Example
See