Note that there are some explanatory texts on larger screens.

plurals
  1. POA C# Refactoring Question
    text
    copied!<p>I came accross the following code today and I didn't like it. It's fairly obvious what it's doing but I'll add a little explanation here anyway:</p> <p>Basically it reads all the settings for an app from the DB and the iterates through all of them looking for the DB Version and the APP Version then sets some variables to the values in the DB (to be used later).</p> <p>I looked at it and thought it was a bit ugly - I don't like switch statements and I hate things that carry on iterating through a list once they're finished. So I decided to refactor it.</p> <p><strong>My question to all of you is how would you refactor it? Or do you think it even needs refactoring at all?</strong></p> <p>Here's the code:</p> <pre><code> using (var sqlConnection = new SqlConnection(Lfepa.Itrs.Framework.Configuration.ConnectionString)) { sqlConnection.Open(); var dataTable = new DataTable("Settings"); var selectCommand = new SqlCommand(Lfepa.Itrs.Data.Database.Commands.dbo.SettingsSelAll, sqlConnection); var reader = selectCommand.ExecuteReader(); while (reader.Read()) { switch (reader[SettingKeyColumnName].ToString().ToUpper()) { case DatabaseVersionKey: DatabaseVersion = new Version(reader[SettingValueColumneName].ToString()); break; case ApplicationVersionKey: ApplicationVersion = new Version(reader[SettingValueColumneName].ToString()); break; default: break; } } if (DatabaseVersion == null) throw new ApplicationException("Colud not load Database Version Setting from the database."); if (ApplicationVersion == null) throw new ApplicationException("Colud not load Application Version Setting from the database."); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload