Member-only story
Comparing 3 options for partially updating a C# object from a list of name/value pairs
Ok, this isn’t super-common, but here are 3 ways this can be done, including a review of the performance impact of each
I came across this problem recently, and I thought it was interesting (and niche) enough to justify an article! So here we go.
I had a really simple C# object, containing a whole bunch of boolean properties. Nothing fancy at all.
I then had to update an instance of this object based on some data that’s (ultimately) coming from a database.
Sounds simple enough, right?
Well, it is.
But, at runtime, I don’t know which properties (if any) are going to be coming back from the database. I also can’t 100% trust that the list of properties in the database will match what I have in C#. And I know that new properties are going to be added semi-regularly.
So I wanted to make sure that whatever solution I used made it easy to cater for all these things. Essentially, it had to:
- Be as easy as possible to add new properties
- Not fall over if someone added duff data into the database