Async Collection Map for Async ViewModel

I am working on a project where I need to work with C # Async programming. I use Automapper to map between Model and ViewModel. For Async data, I created a map method as follows:

public static async Task<IEnumerable<PersonView>> ModelToViewModelCollectionAsync(this Task<IEnumerable<Person>> persons)
{
    return await Mapper.Map<Task<IEnumerable<Person>>, Task<IEnumerable<PersonView>>>(persons);
}

And I called this matching method as follows (in my service class):

public async Task<IEnumerable<PersonView>> GetAllAsync()
{
    return await _personRepository.GetAllAsync("DisplayAll").ModelToViewModelCollectionAsync();
}

Finally, I called my class of service inside the controller.

public async Task<ActionResult> Index()
{
    return View(await PersonFacade.GetAllAsync());
}

But when I run the project, it shows me the following exception

Missing type map configuration or unsupported mapping.

Mapping types:
Task`1 -> Task`1
System.Threading.Tasks.Task`1[[System.Collections.Generic.IEnumerable`1[[PF.Model.Person, PF.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] -> System.Threading.Tasks.Task`1[[System.Collections.Generic.IEnumerable`1[[PF.Services.ViewModel.PersonView, PF.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

Destination path:
Task`1

Source value:
System.Threading.Tasks.Task`1[System.Collections.Generic.IEnumerable`1[PF.Model.Person]]

According to my project architecture, automapper cannot be avoided.

Note. My repository method for getall is as follows:

public virtual async Task<IEnumerable<T>> GetAllAsync(string storedProcedure)
{
    return await _conn.QueryAsync<T>(storedProcedure);
}
+3
source share
1 answer

. . , Async , :

public async Task<IEnumerable<PersonView>> GetAllAsync()
        {
            var persons = await _personRepository.GetAllAsync("DisplayAll");
            var personList = PersonExtension.ModelToViewModelCollection(persons);
            return personList;
        }

.

.

+1

Source: https://habr.com/ru/post/1607652/


All Articles