Just found a bit of code someone here had written to access some DB Entities...
public static OurCustomObject GetOurCustomObject(int primaryKey)
{
return GetOurCustomObject<int>(primaryKey, "usp_GetOurCustomObjectByID");
}
public static OurCustomObject GetOurCustomObject(Guid uniqueIdent)
{
return GetOurCustomObject<Guid>(uniqueIdent, "usp_GetOurCustomObjectByGUID");
}
private static OurCustomObject<T>(T identifier, string sproc)
{
if((T != typeof(int)) && (T == typeof(Guid)))
{
throw new ArgumentException("Identifier must be a string or an int");
}
//ADO.NET Code to make DB Call with supplied sproc.
}
Theres just something about it that doesn't seem very generic
. The fact that the
sprocs are passed into the inner method feels ugly. but the only way I can see around that is to have an if/else in the private method along the lines of
if(type == int)
sproc = "GetByID";
else if (type == Guid)
sproc = "GetByGUID";
Also the exception throwing looks ugly as well... is there anyway to use a where T : clause
e.g.
private static OurCustomObject<T>(T identifier) where T : int OR Guid
Any suggestions on how to clean this up a little.