How to use Dapper Async method correctly & get data from Task<IEnumerable<T>>

Viewed 465

I am trying to learn using QueryAsync method to retrieve multiple records and returning a list of objects. When the async call returns Task<IEnumerable>, I could not figure out how to actually extract data from IEnumerable.

TEST ENVIRONMENT

App: .NET 6 console app with class library: DapperDbOps IDE: Visual Studio 2022 Test framework: XUnit

TEST CODE

In class library, I created class: DapperDbOps.DapperHelper. It contains a method: QueryMultipleAsync (see code below). I tried to return type as "Task <List>" and got complier error. It looks like the only way to make the code work is using "async Task<IEnumerable>"

public async Task <IEnumerable<T>> QueryMultipleAsync<T>(string sqlCmd)
{
    using IDbConnection db = new SqlConnection(_dbConnStr);

    var dtList = await db.QueryAsync<T>(sqlCmd).ConfigureAwait(false);

    return dtList;
}

Under DapperDbOps, I created a data object class: EmployeeDO.cs

namespace DapperDBOps.Models
{
    public class EmployeeDO
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int Salary { get; set; }
        public string Dept { get; set; }
    }
}

In XUnit, setup the test project: DapperDbOpsUnitTests, and the test case DapperDbHelperTests.
To test QueryMultipleAsync, I setup the test method: QueryMultipleAsyncTest (see following code).

[Fact]
public void QueryMultipleAsyncTest()
{
    //Arrange
    string sqlCmd = "SELECT * FROM Emloyee";
    
    //Act
    Task <IEnumerable<EmployeeDO>> elist = dbHelper.QueryMultipleAsync<EmployeeDO>(sqlCmd);

    //Assert
    Assert.True(elist != null);

    //foreach(var e in elist)
    //{

    //}

}

PROBLEM

Eventhough the test run successfully, I was unable to access the returned data that wrapped in the Task<IEumerable>. After the Assert statement, I tried to interate the returned elist in the foreach loop (see the statements commented out above). It failed. I also tried to use "foreach(EmployeeDO e in elist)" and got the same error. The error message was the following:

enter image description here

QUESTIONS

  1. Am I using the correct return type with the DapperDbHelper.QueryMultipleAsync?

  2. Can the QueryAsync method be used for retrieving a single record and multiple records?

  3. What is the correct way of access returned data with type of Task<IEnumerable>?

I've tried to read many posts at the site and still unable to find the answer. Any advice or assistance will be greatly appreciated.

1 Answers

You are asserting on the Task which your Async() call returns, convert your test to async and "await" the call instead:

[Fact]
public async Task QueryMultipleAsyncTest()
{
    //Arrange
    string sqlCmd = "SELECT * FROM Emloyee";
    
    //Act
    IEnumerable<EmployeeDO> elist = await dbHelper.QueryAsync<EmployeeDO>(sqlCmd);

    //Assert
    Assert.True(elist != null);
}
Related