I have tree layer of classes to get data from database and serve it within an Action. I get data from database old way, using SqlParameter
classes.
I need to/want to reconstruct this methods to asynchronous methods. What truly I want is to learn how to make these synchronous methods to Asynchronous. And make them work different threads and use them across entire app.
What I don't want to do is to Use Ado.Net or using ready methods like HttpClient.GetAsync
I read this question: Calling async methods from non-async code but couldn't apply to my structure and couldn't be sure if it is work properly, avoids deadlocks.
My old structure is like that:
My Action in My BlockController
:
public ActionResult Index()
{
return View(new BlockDb().Select());
}
The Select
Method in BlockDb
class:
public List<Block> Select()
{
SqlDataReader dr = DbJobs.ExecuteReader("BlockSelect");
List<Block> list = new List<Block>();
Block cs;
while (dr.Read())
{
cs = new Block();
cs.ID = Convert.ToInt32(dr["ID"]);
cs.Name= Convert.ToString(dr["Name"]);
cs.Dt = Convert.ToDateTime(dr["Dt"]);
cs.Ok = Convert.ToBoolean(dr["Ok"]);
list.Add(cs);
}
dr.Close();
dr.Dispose();
return list;
}
And the last one ExecuteReader
Method in DbJobs
static class:
public static SqlDataReader ExecuteReader(string ProcName, params SqlParameter[] prmtr)
{
SqlDataReader Result = null;
SqlConnection connect = new SqlConnection(cnn);
try
{
SqlCommand command = new SqlCommand(ProcName, connect);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter item in prmtr)
command.Parameters.Add(item);
if (connect.State == ConnectionState.Closed)
connect.Open();
Result = command.ExecuteReader(CommandBehavior.CloseConnection);
//connect.Close();
//connect.Dispose();
}
catch { }
return Result;
}
I can't be sure bu the result of an Action could be like that:
public async Task<ActionResult> Index()
{
return View(await new BlockDb().SelectAsync());
}
How can I achive that?