I have some classes (entities) that represent database tables
public class ALBUM
{
public int Id { get; set; }
public string Name { get; set; }
public int Tracks { get; set; }
}
public class BOOK
{
public int Id { get; set; }
public string Author { get, set; }
}
Then to get parameters to respective stored procedures, insert, update, any
public Command getParameters(string spName, dynamic entity, Connection conn)
{
Command cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
// Get properties (fields) from entity (table)
PropertyInfo[] properties = entity.GetType().GetProperties();
for (int = 0; i < properties.Length; i++)
{
// here I set Parameters: name, DBType, and Value from properties
}
}
Now I call
public void Some()
{
ALBUM someAlbum = new ALBUM();
BOOK anyBook = new BOOK(); // all respective data from DB is set
Command newCommand = getParameters("myAlbumSP", someAlbum, myConnection);
Command newCommand = getParameters("mySPBooks", anyBook, myConnection);
}
How else colud I define "entity" parameter?, not to be dynamic.
It works, just I'm lookin for some other way to do it, I´m sure there is plenty of.