Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a repository delete/remove an entity by passing in an id or or the entity itself

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?

like image 788
testing123 Avatar asked Apr 18 '12 18:04

testing123


2 Answers

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.

like image 53
MikeSW Avatar answered Sep 21 '22 20:09

MikeSW


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.

like image 36
eulerfx Avatar answered Sep 17 '22 20:09

eulerfx