Let's say we've got a type Book with ten properties. This type is a representation of the table in database.
What's the best solution to update such a type? I've used repository pattern where I got update method that take Book type and updates all its fields.
So when I want to update a Book, I'm getting it by Id from database, update fields I want (I can update 1, 2 or all its fields) and then invoke Update method on repository.
My friend by contrast tells that we should update only the fields we want, so for example if i only want to update field bookTitle I should create method in repo UpdateTitle etc.
What the best solution? To be honest I see one method in repo update with all fields much better than multiple methods to update some parts/ones properties.
Dapper do not generate queries for you. You write the query and pass it to Dapper. Dapper do not track your types. You maintain the track of changes. Dapper just executes what you passed in and maps the result with your type/poco/entity/class whatever if applicable.
That said, how to update selected fields/columns? question is out of frame.
Now about other question â€” what approach to be taken? Both the approaches you mention have their own pros and cons.
Remember that, early optimization is root of all evil. Dapper is simple; keep it simple. Update entire type to begin with. Over the time, if you face critical performance issues AND you are sure the issue is due to updating all the fields, go ahead and optimize the query (or write new one) to update selected fields.
This will save you on efforts â€” will help improve performance if needed -- will keep the things simple.
Dapper itself doesn't support Tracking of database entities, there are ORMs that does that (e.g. Entity Framework)
Tracking allows you to load database objects into model class instances, once these are updated in code, you can save changes into the database, the generated query will update only the changed fields, as said, this is not supported in Dapper.
If you just use Dapper, I would recommend to keep it simple, save the whole updated object and consider changing that method if you have performance issues.