I have an n-tier solution which consist of DAL,BL and a ASP.net WebAPI project. I'm new with Asynchronous Pattern and I'm trying to add it to my Framework. Am I using the asynchronous pattern correctly? I'm not convinced with my implementation of it. Could you give me a recommendation on how to use it correctly or any other improvements I can make on my system?
This is my BL.
public async Task<DTOUserList> GetUserList(DTOSearch source)
{
var result = new DTOUserList();
try
{
string strQuery;
Object[] objValues;
string strOrderBy;
CommonFunctions.ParseQuery(out strQuery, out objValues, out strOrderBy, source);
//This is my implementation of async/await which im not convinced.
result = await Task.Run(() => _RepositoryUser.GetUserList(strQuery, objValues, source.PageNo, source.PageSize, source.OrderBy));
}
catch (Exception ex)
{
result.IsSuccessful = false;
result.ErrorMsg = "Business Layer Error - " + ex.Message;
}
return result;
}
and this is my DAL.
public DTOUserList GetUserList(String Query, object[] values, Int32 pageNo, Int32 pageSize, String orderby)
{
var result = new DTOUserList();
try
{
_Entities = new Entities();
IQueryable<DTOUser> data = from user in _Entities.mdUsers
where user.Deleted == 0
select new DTOUser
{
UserID = user.UserID,
FirstName = user.FirstName,
MiddleName = user.MiddleName,
LastName = user.LastName,
Email = user.Email,
Telephone = user.Telephone,
Mobile = user.Mobile,
Address = user.Address,
City = user.City,
Country = user.Country,
Active = user.Active,
DateCreated = user.DateCreated
};
if (!string.IsNullOrWhiteSpace(Query)) data = data.Where(Query, values);
result.TotalRecords = data.Count();
result.UserList = data.OrderBy(a => a.FirstName).Skip(((pageNo > 0 ? pageNo : 1) - 1) * (pageSize > 0 ? pageSize : 1000)).Take((pageSize > 0 ? pageSize : 1000)).ToList();
result.IsSuccessful = true;
}
catch (Exception ex)
{
result.IsSuccessful = false;
result.ErrorMsg = "Data Access Layer Error - " + ex.Message;
}
return result;
}