Saves an entity (insert or update).
This is the only required method following DDD principles. Repositories should act like collections where you add/update entities.
The entity to save
OptionalfindOptionalfindFinds all entities.
Optional - use with caution in production systems with large datasets. Consider pagination or specific query methods instead.
An array of all entities
OptionaldeleteDeletes an entity by its ID.
Optional - consider soft deletes or event sourcing patterns instead. Hard deletes may not be appropriate for all use cases.
The entity ID
OptionalexistsChecks if an entity exists by its ID.
Optional - can often be implemented using findById instead.
The entity ID
true if the entity exists, false otherwise
Generic repository interface for domain entities.
Following DDD principles, the only required method is
save(). Repositories represent collections of domain entities, not data access objects.Additional methods are optional and should be added to specific repository interfaces based on your use case requirements.
Example