A summary of Martin Fowler’s definition of the Repository pattern:
Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
So if we have both add
and update
methods, I could claim it’s not a collection-like interface, right? I shouldn’t need to bother checking if an object’s already there when adding to a set-like collection.
There are two common approaches about add
/update
:
Collection-oriented repositories try to mimic an in-memory collection, so you shouldn’t need to re-add an object if it was updated and already in the collection. The repository (or layers hidden below it, such as an ORM) should handle the changes to an entity and track them. You just add
an object when you first create it and then no more methods are needed after the entity is changed.
Persistence-oriented repositories are aware that an object needs to be explicitly “saved”
after any changes, so you can call the entity.save()
method when an object is created or modified.
(Those are my interpretations of the definitions by Vaughn Vernon in Implementing Domain-Driven Design.)
delete
is fine, but perhaps remove
would be a better name.