IC Improve Code Recipes

Appendix A

Legacy-to-Modern Cheat Sheet

Use this table when you meet an old pattern in a code review or a migration. Each row points to the chapter with the full recipe.

Data and SQL

Legacy pattern Modern replacement Chapter
System.Data.SqlClient Microsoft.Data.SqlClient 1
SqlHelper (Data Access Application Block) A small connection factory plus Dapper or your own thin helper 1
SQL built with + and user input Parameters on every value; sp_executesql for dynamic SQL 1, 2
SqlConnection without using await using var connection = ... 1
Row-by-row inserts in a loop SqlBulkCopy or a table-valued parameter 1
@@ERROR after each statement BEGIN TRY ... BEGIN CATCH with THROW 2
ROW_NUMBER() paging wrapper OFFSET ... FETCH NEXT 2
Cursors for set operations Set-based UPDATE, INSERT ... SELECT, window functions 2
WITH (NOLOCK) for speed Proper indexes; READ COMMITTED SNAPSHOT where suitable 2
Audit triggers on every table System-versioned temporal tables (SQL Server 2016+) 2
HttpRuntime.Cache / static dictionaries IMemoryCache with expiration; IDistributedCache for farms 3

Cross-cutting concerns

Legacy pattern Modern replacement Chapter
Homegrown LogHelper writing text files ILogger<T> with message templates; Serilog sinks 4
String-concatenated log messages Structured templates: "Order {OrderId} failed" 4
catch { } Catch what you can handle; let the rest reach a global handler 5
throw ex; throw; 5
Error codes and bool returns mixed with exceptions A Result<T> for expected failures, exceptions for the unexpected 5
Thread.Sleep retry loops Exponential backoff; Microsoft.Extensions.Http.Resilience / Polly 5, 13
ConfigurationManager.AppSettings["Key"] IOptions<T> with validation at startup 6
Connection strings and passwords in source User secrets, environment variables, Azure Key Vault 6
MD5 or SHA1 for passwords PBKDF2 (Rfc2898DeriveBytes.Pbkdf2) or the Identity password hasher 7
Homemade encryption, RijndaelManaged with fixed IVs AesGcm with a random nonce per message 7
new Random() for tokens RandomNumberGenerator 7
Validation scattered through code-behind Guard clauses plus validation at the boundary 8

Integration

Legacy pattern Modern replacement Chapter
System.Net.Mail.SmtpClient MailKit and MimeKit 9
Hard-coded HTML e-mail bodies Templates with a plain-text alternative 9
File.WriteAllText over the live file Write to a temp file, then replace atomically 10
FtpWebRequest SFTP with SSH.NET 10
Path strings joined with + Path.Combine/Path.GetFullPath with a root check 10
CSV written with string.Join(",") A writer that quotes and escapes correctly 11
Excel automation (Interop) on a server ClosedXML (no Office installation needed) 11
JavaScriptSerializer / hand-built JSON System.Text.Json, with source generation where it matters 12
XmlDocument with string XPath everywhere XDocument / LINQ to XML; XmlReader for huge files 12
WebClient / HttpWebRequest IHttpClientFactory with typed clients 13
"Add Service Reference" on .NET Framework dotnet-svcutil and System.ServiceModel client packages 13

Applications

Legacy pattern Modern replacement Chapter
Windows Service with a Timer and Thread.Sleep Worker Service, BackgroundService and PeriodicTimer 14
Hand-rolled queue tables polled in a loop System.Threading.Channels in process; a real queue across processes 14
DateTime.Now stored in the database DateTime.UtcNow / DateTimeOffset; convert at the edges 15
ArrayList, Hashtable List<T>, Dictionary<TKey, TValue> 15
string += in loops StringBuilder or string.Join 15
A 3,000-line Utility.cs Small, focused extension classes 15
Web Forms BasePage Filters, middleware and base controllers 16
Session["key"] casts everywhere A typed session wrapper 16
Big-bang rewrite of a Web Forms site Incremental migration behind a reverse proxy (YARP) 16