BlackMamba wrote:
Otis wrote:
You should use a unitofwork for this, as save is saving changes in entities, not deleting entities. It might seem weird at first, but it's more natural. For example, you remove an entity from a collection in your example and then save the parent entity. Imagine that I'd do that to just remove the entity from the in-memory collection
. I then suddenly will experience my entity being removed from the database as well!
Yes what you say is true but then I feel that since we're using an object relational mapper, then there must be a way to map the removal of an entity from a collection to the persistence layer since adding one to the collection will result in adding a corresponding record in the db. That's why in our old framework we had a Remove method and a LogicalRemove method. The first one would remove and that's it, the second one would remember to delete from the db as well.
You have a point, but you can also see the collections in an entity as the result of a filtered view on the set of related entities and see them as such and not as part of the entity they're contained in.
For example, if I fetch a customer and its last 10 orders, if I remove one order from that list, you could say, it's removed from the filtered view on the total set of orders for that customer.
The reason why this ambiguity is not implemented in code is that deletes aren't reversable. A bad update can be undone by setting the data again to the right values, but a delete (perhaps a whole graph!) is much harder to track back, especially if the user didnt intent do delete anything, just removing a row from the collection.
I know it's an ambiguity and I know it depends on which way you look at things to understand the way it is now and I also understand your point and in fact I do think you have a point and are right, but there is a downside to that position, which is why up till now haven't proceeded with implementing it. The same goes for cascading deletes.
The unitofwork is there to collect work to be done on the entities in question, which can help you with scheduling the deletes you want to perform.