Everyone knows this
using (var db = new DatabaseEntites())
{
var row = db.SomeTable.SingleOrDefault(r => r.Id == 5)
}
I planned to create a static class with a static method like this
public static class SomeTableRepository
{
public static class GetSomeTableRow(DatabaseEntities db, int id)
{
return db.SomeTable.SingleOrDefault(r => r.Id == 5);
}
}
and the first code would then look like this
using (var db = new DatabaseEntites())
{
var row = SomeTableRepository.GetSomeTableRow(db, id);
}
If this would be a web app…would that kind of programming be ok…or could that kind of programming cause some trouble?…or is this perfectly good code 🙂
As Kirk said, I think the only gain is minor readability. However, if you add
thisin front of your first parameter and make this an extension method, then you might gain some readability for potential readers of your code:UPDATE
I also noticed that
public static class GetSomeTableRowwould not compile. I changed it to be more generic and less confusing for future readers (classtoYourClassName)Furthermore, you could rename this to make it read more like what it actually is:
Yes, it is lengthy, but the only reason to abstract such a simple method as
SingleOrDefaultwould be to make the code even readable at a glance. TheByIdpart is debatable since the parameter is namedid(and it makes it seem redundant), however that only shows up while coding with intellisense. You could leave it out (or take it down to justBywithoutId…but that leaves too much for each implementer IMO)