interface IDbInteraction
{
int GetValueAsInt(string sql);
}
class SQLDbInteraction : IDbInteraction
{
private string _connStr;
public SQLDbInteraction (string connStr)
{
_connStr = connStr;
}
public int GetValueAsInt(string sql)
{
int value;
using(SqlConnection conn=new SqlConnection(_connStr))
{
SqlCommand command = new SqlCommand(sql, conn);
conn.Open();
value = Convert.ToInt32(command.ExecuteScalar());
}
return value;
}
}
This is a simple example on how to program an interface when access to a SQL Server database is needed.
It was offered to me as advice to replace an old code that I've presented in a question.
So ..., as I am fresh developer and new to the interface concept / approach / methodology, and as I don't really connect to the term Repository I've renamed that...
Could you please give a little broad example on how would you use these two : class & interface above ?
And also if you could please offer alternative to IRepository (original name)
What would you replace it with (if you don't mind)?
As the point of this post, is about moving from the unsafe and kinda obsolete technique I was using, and now while trying to move on to "next generation" coding methodology,
I could use your help on this sample code to learn usage.
update
what i did now was researching the web.. and still, i have this issue in question :
is the way to call/invoke GetValueAsInt("select aColumn ...")
now...when having an interface added to the "equation" ,
did it change anything as far as the invocation act cares ?
do you still just call GetValueAsInt() as u did till now?
is it true that in this manner, nothing realy changed.. as for the actual usage
do you now call
IDbInteraction.GetValueAsInt("select aColumn from...")
or
SQLDbInteraction.GetValueAsInt("select aColumn from...")
or calling both ways is now availble ?