将异步模型集合映射到异步视图模型集合

3

我正在参与一个涉及C#异步编程的项目。我使用Automapper来映射Model和ViewModel。对于异步数据,我创建了一个以下的映射方法:

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

我称此映射方法为以下内容(在我的服务类中):

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

最后,我在控制器中调用了我的服务类。
public async Task<ActionResult> Index()
{
    return View(await PersonFacade.GetAllAsync());
}

但是当我运行我的项目时,它显示以下异常。
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]]

根据我的项目架构,不可能避免使用automapper。
注意:获取所有内容的存储库方法如下:
public virtual async Task<IEnumerable<T>> GetAllAsync(string storedProcedure)
{
    return await _conn.QueryAsync<T>(storedProcedure);
}
1个回答

1

问题已解决。我在这里使用了一点技巧。我没有为服务层创建异步扩展方法,而是按照以下方式编写了我的代码:

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

剩下的都不变。
现在它工作得很好。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接