CRUD Repository Update Method – Should It Always Return Something?

crudentity-framework

I am writing a repository class with entity framework. One of the method is an update method, which simply updates an entity. The controller does not need the entity. However, from a testing perspective, it may be useful to just return the entity. Than I easily have something to assert. It may be even more useful to return two things (with a value tuple): the saved entity and the number of saved changes. Then I have more to assert. On the other hand, it is an update method, not a read method that sould absolutely return something.

What choice should I make considering the good practices?

Best Answer

You should be returning values because you need them not because it makes testing easier. That being said, it is more valuable to know if the operation succeeded since you already know what's going to be updated. If something goes wrong you can use exceptions to communicate it to the upper layers. Also, I don't think that returning a value representing the number of affected records would provide much value since you'll probably be updating aggregates that have updated children.