Hey all, fairly new to this stuff. Still working on my first app, and I want some insight

I have an app that has many simple queries that simply retrieve one record from a table, usually as a string.

Here’s an example:

 public string querySerialNumbersForEmail(string serialNumber) { try { string retrievedEmail = _context.SerialNumbers .Where(x => x.SerialNumber == serialNumber) .Select(x => x.CustomerEmail).First(); return retrievedEmail; } catch { return null; } } 

I’m starting to realize that I have this exact pattern many time throughout my query repository, and that this violates the DRY principle.

So a fix would obviously be:

public string retrieveSingleEntry(string searchVar, string table, string tableAttribute, string retrievedAttribute) { try { string retrievedEntry = _context.table .Where(x => x.tableAttribute == searchVar) .Select(x => x.retrievedAttribute).First(); return retrievedEntry; } catch { return null; } } 

This would be re-usable per se, but feels clunky to call.

Calling would look like

retrieveSingleEntry(43235, SerialNumbers, SerialNumber, CustomerEmail)

as opposed to

querySerialNumbersForEmail(43235)

The other downside to the new proposed method would be that it wouldn’t benefit from intellisense when adding in the parameters for table name, attribute name, etc.

What do y’all use for your queries like this?

submitted by /u/ninjoala
[link] [comments]
source