HI lets say I have a C# code
private const string INSERT = "INSERT INTO Person VALUES (@FirstName, @LastName)";
public static DbCommand INSERTCOMMAND(IPerson person)
{
DbCommand command = null;
var sqlParams = new List<DbParameter>();
if(SQLManagerFactory.SQLManager is SQLServerManager)
{
command = new SqlCommand(INSERT);
sqlParams.Add(new SqlParameter("@FirstName", person.FirstName);
sqlParams.Add(new SqlParameter("@LastName", person.LastName);
}
else // SQLiteManager
{
command = new SQLiteCommand(INSERT);
sqlParams.Add(new SQLiteParameter("@FirstName", person.FirstName);
sqlParams.Add(new SQLiteParameter("@LastName", person.LastName);
}
command.Parameters.AddRange(sqlParams.ToArray());
return command;
}
Which is working perfectly fine. Of course in my production code, it is quite bigger and has a lot more location that does the similar things for different commands.
My question is that is there way to make this shorter? I do not wish to copy and paste code which essentially does the same thing except for calling the different constructors.
Thank you very much in advance.
Aucun commentaire:
Enregistrer un commentaire