地图异步模型系列,以异步视图模型集合 [英] Map Async Model Collection to Async ViewModel Collection

查看:132
本文介绍了地图异步模型系列,以异步视图模型集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我需要异步编程C#上班工作。我使用Automapper型号和视图模型之间的映射。对于异步数据我创建了一个地图的方法如下:

 公共静态异步任务<&IEnumerable的LT; PersonView>> ModelToViewModelCollectionAsync(此任务<&IEnumerable的LT;人>>人)
{
    返回等待Mapper.Map<任务<&IEnumerable的LT;人>>中任务<&IEnumerable的LT; PersonView>>>(人);
}

和我称这种映射方法如下(在我的服务类):

 公共异步任务<&IEnumerable的LT; PersonView>> GetAllAsync()
{
    返回等待_personRepository.GetAllAsync(DisplayAll)ModelToViewModelCollectionAsync()。
}

最后,我打电话给我的服务类的内部控制。

 公共异步任务<&的ActionResult GT;指数()
{
    返回查看(等待PersonFacade.GetAllAsync());
}

但是,当我跑我的项目,它为我以下异常

 缺少类型映射配置或不支持的映射。映射类型:
Task`1 - > Task`1
System.Threading.Tasks.Task`1 [System.Collections.Generic.IEnumerable`1 [PF.Model.Person,PF.Model,版本= 1.0.0.0,文化=中立,公钥=空],mscorlib中,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089]] - > System.Threading.Tasks.Task`1[[System.Collections.Generic.IEnumerable`1[[PF.Services.ViewModel.PersonView, PF.Services,版本= 1.0.0.0,文化=中立,公钥=空],mscorlib程序,版本= 4.0.0.0,文化=中性公钥= b77a5c561934e089]目标路径:
Task`1来源值:
System.Threading.Tasks.Task`1 [System.Collections.Generic.IEnumerable`1 [PF.Model.Person]

据我的项目结构的无法避免automapper。

请注意:我对GETALL库方法如下:

  public虚拟异步任务<&IEnumerable的LT; T>> GetAllAsync(字符串的StoredProcedure)
{
    返回等待_conn.QueryAsync< T>(StoredProcedure的);
}


解决方案

解决了这个问题。我在这里申请点点把戏。相反,在服务层中产生的扩展方法异步的,我写我的code如下:

 公共异步任务<&IEnumerable的LT; PersonView>> GetAllAsync()
        {
            变种人=等待_personRepository.GetAllAsync(DisplayAll);
            VAR personList = PersonExtension.ModelToViewModelCollection(人);
            返回personList;
        }

剩下的都是不变的。

现在,它做工精细。

I am working with a project where I need to work with Async programming C#. I am using Automapper for 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 mapping method as follows (In my service class):

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

Finally I called my service class inside controller.

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

But when I run my project it shows me 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 its not possible to avoid automapper.

Note: My repository method for getall is as follows:

public virtual async Task<IEnumerable<T>> GetAllAsync(string storedProcedure)
{
    return await _conn.QueryAsync<T>(storedProcedure);
}

解决方案

Solved this issue. I applied little bit trick here. Instead of creating extension method for Async in service layer I wrote my code as follows:

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

remaining all are unchanged.

now it work fine.

这篇关于地图异步模型系列,以异步视图模型集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆