Ignacio Perez
Ignacio Perez

Reputation: 2361

How can I do async await in the repository pattern?

I have code that is long running i/o bound perfect for async/await . I am doing the repository pattern and can not figure out how to do await in the controller as I'm getting an object does not contain awaiter method on this code

 public class HomeController : Controller
     {
    private ImainRepository _helper = null;

    public HomeController()
    {
        this._helper = new MainRepository();
    }
   public async Task<string> Aboutt()
    {
       // Here I get the error
       object main = await _helper.Top_Five() ?? null;
        if(main != null)
        {
            return main.ToString();
        }
        else
        {
            return null;
        }
    }
 }

The method that I am implementing works fine as you can see below. I get data out of the database and return it in string format. What I would like is to find a way to make object main = await _helper.Top_Five() ?? null; await otherwise I would be mixing async with synchronous code. Any suggestions would be great ...

    public async Task<string> Top_Five()
    {
        try
        {

            using (NpgsqlConnection conn = new NpgsqlConnection("Config"))
            {
                conn.Open();
                string Results = null;
                NpgsqlCommand cmd = new NpgsqlCommand("select * from streams limit 15)t", conn);


                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (reader.Read())
                    {

                        Results = reader.GetString(0);
                    }

                    return  Results;
                }
            }
        }
        catch(Exception e)
        {
            // Log it here
            return null;
        }

    }

Upvotes: 2

Views: 1555

Answers (4)

Servy
Servy

Reputation: 203830

Your entire method is accomplishing nothing. ?? null replaces null with null and anything else with itself. And that's your only transformation of the result, so you can just write:

public Task<string> Aboutt()
{
        return _helper.Top_Five();
}

Upvotes: 0

David Pine
David Pine

Reputation: 24535

The issue is the use of the null coalesce operator on the Task method. You need to handle that differently, consider the following:

public class HomeController : Controller
{
    private ImainRepository _helper = null;

    public HomeController()
    {
        this._helper = new MainRepository();
    }

    public async Task<string> Aboutt()
    {
        string main = await _helper.Top_Five();
        return main;
    }
}

Notice that when you await is all you need as you're returning as string, so declare it as a string - not an object.

Upvotes: 0

Eddie Paz
Eddie Paz

Reputation: 2241

Considering that Top_Five returns null or a string, the code is unnecessary:

object main = await _helper.Top_Five() ?? null;

Instead, do the following:

var main = await _helper.Top_Five(); // the "awaiter" completes now
return main; // returns a string or null like in your example

Upvotes: 0

hvaughan3
hvaughan3

Reputation: 11105

Does this work for you?

object main = (await _helper.Top_Five()) ?? null;

Notice the extra ( and ) since you need to await the method and then check for null.

Upvotes: 1

Related Questions