Thursday, November 26, 2015

Generate SQL update queries in C# for models with empty data


Here's the scenario:
You get a payload using a model that contains only a subset of the fields populated. The remaining fields contain null\default values, however, while writing an update query, you cannot set the default values in your statement since you'd like to ignore them.

So, I wrote a small helper method that allows you to handle this case.
You can then build your update statement using the following method. I'm using the Dapper library as ORM.

There are risks using this method such as SQL Injection, etc. so you're better of using Prepared Statements 99% of the time. However, if you'd like to write ad-hoc SQL queries, this should suffice.

Tuesday, July 14, 2015

What my job is like

Imaging you're entering a building. Within the lobby you have a door on the side. Opening the door leads you to a space where there's another building. Often it looks like the same building as the one you entered the first time, sometimes it's different. Do this 5 times. Now you have to get a smoke signal in the 5th building out to the watchman of the first building, all the time while traps keep opening and monsters check your progress. Welcome to crazy land. This is what I do for a living. I'm a coder.