HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Asynchronous Pattern

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
asynchronouspatternstackoverflow

Problem

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 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 data = from user in _Entities.mdUsers
where user.Deleted == 0
select new DTOUser
{
UserID = user.UserID,
FirstName = user.FirstName,
MiddleName = user.MiddleName,

Solution

In general, you shouldn't expose synchronous operations as asynchronous, see Should I expose asynchronous wrappers for synchronous methods? If a method doesn't have an async version, just expose it synchronously. The caller can then choose to run it on another thread, if that makes sense.

Context

StackExchange Code Review Q#51278, answer score: 3

Revisions (0)

No revisions yet.