Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
How can I store async task in a dictionary and call them according to config value?

Here is my code below:

public interface IResponseEntity
{
}

public class ResponseEntity : IResponseEntity
{
    public string Status { get; set; }
    public string Message { get; set; }
    public string Data { get; set; }
}

public class ObjectForRequest
{
    public string abcd { get; set; }
    public string xyz { get; set; }
    public string pqrs { get; set; }
    public short mnop { get; set; }
}

private async Task<IResponseEntity> Dowork_for_A(ObjectForRequest objReq)
{
    //Code for A
    ....................
}

private async Task<IResponseEntity> Dowork_for_B(ObjectForRequest objReq)
{
    //Code for B
    ....................
}

private async Task<IResponseEntity> Dowork_for_C(ObjectForRequest objReq)
{
    //Code for C
    ....................
}

private async Task<IResponseEntity> Dowork_for_D(ObjectForRequest objReq)
{
    //Code for D
    ....................
}

public async Task<IResponseEntity> method(ObjectForRequest objReq)
{
    if (CONFIGVALUE == 'A')
        return await Dowork_for_A(RequestOBJ);
    else if (CONFIGVALUE == 'B')
        return await Dowork_for_B(RequestOBJ);
    else if (CONFIGVALUE == 'C')
        return await Dowork_for_C(RequestOBJ);
    else if (CONFIGVALUE == 'D')
        return await Dowork_for_D(RequestOBJ);
    else
        return null;
}

As if now I am checking config value in the if condition and then calling async task to return value.

How can I setup <key, value> pair of dictionary as config value as 'key' and 'value' as async task? Or in other words here how can I store async task in a dictionary and call them according to config value?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is very straightforward. Declare your dictionary as such:

    Dictionary<char, Func<ObjectForRequest, Task<IResponseEntity>>> _actions =
         new Dictionary<char, Func<ObjectForRequest, Task<IResponseEntity>>>()
    {
        { 'A', Dowork_for_A },
        { 'B', Dowork_for_B }
        // ...
    }

Then:

public async Task<IResponseEntity> method(ObjectForRequest objReq)
{
     Func<ObjectForRequest, Task<IResponseEntity>> action;
     if (!_actions.TryGetValue(CONFIGVALUE, out action))
     {
          return null;
     }   
     return await action(objReq);
}

Also, please do consider the performance implications of this. Dictionary's are wicked fast for large sets, but if you really do only have 3 or 4 options, a switch or if ... else chain is almost certainly going to be faster. What the exact tipping point is when Dictionary becomes better is hard to say.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!