i am currently working on an Web api project where we are using repository pattern but unfortunately we do not use EF.
So for the data access layer we use ADO.NET in which we call the stored procedures on the database. Bad architecture i know.
Anyway the problem i recently discovered (due to the fact i was working on the other layers) is that even if all the layers are implemented by using tasks and the data access layer is not and if i am not mistaken if in a chain of asynchronous method calls there is an synchronous method call that turns the whole process into synchronous.
So initially i had methods like this one:
public DataTable GetClients()
{
using (var conn = new SqlConnection(CMS))
{
using (var cmd = new SqlCommand("[dbo].[ClientsReturn]"))
{
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
var adapter = new SqlDataAdapter(cmd);
var ds = new DataSet();
adapter.Fill(ds);
var T = ds.Tables[0];
return T;
}
}
}
So after some search i change the method and generally the classes to something like
public async Task<DataTable> GetClients()
{
var ds = new DataSet();
using (var conn = new SqlConnection(CMS))
{
using (var cmd = new SqlCommand("[dbo].[ClientsReturn]"))
{
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
await conn.OpenAsync();
var adapter = new SqlDataAdapter(cmd);
await Task.Run(() => adapter.Fill(ds));
var T = ds.Tables[0];
return T;
}
}
}
Is this looking right to you?Any suggestions?
Thanks in advance.