Reputation: 11447
I am currently creating a repository and was wondering what the "best practice" is for the delete operation of an entity. In the options below make and model make up the key for the Car entity.
Option 1:
deleteCar(Car car)
Option 2:
deleteCar(String make, String model)
Option 3:
deleteCar(CarKey carKey)
At first I thought Option 1, but in practice Option 2 seems more appealing (I don't want to have to get an object when I only have the id just so that I can pass it into the delete method). I put option 3 because I have seen stuff like that but that doesn't seem right to me because CarKey isn't really a domain object.
Thoughts?
Upvotes: 9
Views: 1930
Reputation: 37709
If strictly adhering to the definition of repository in DDD then option 1 is the way to go since that way a repository emulates an in-memory collection. However, I don't see that as a critical component of a repository and can lead to leaky abstractions if take too far. On the other hand, requiring deletion by the entity object in its entirety can be an indication that the caller of the repository (such as application service) should retrieve the entity to be deleted by the ID initially, address any business concerns, and them remove it. ORMs like Hibernate can delete by a query, so that you only need the ID to invoke a delete, but it ends up loading the entity from the database anyway.
Upvotes: 4
Reputation: 16348
Option 3.
It doesn't matter that CarKey isn't a domain object (it can be a value object though), an id is all you need for that action to happen. That's because, if the Car is an AR, the repository should know how to GetIt and how to handle deletes.
Upvotes: 4