AutoMapper和存储库模式 [英] AutoMapper and Repository Pattern

查看:67
本文介绍了AutoMapper和存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Silverlight项目上试用AutoMapper.

我是AutoMapper的新手,实际上是昨天才介绍的.

我的样本的数据通信模式如下:RDBM => ORM =>储存库=> DTO,反之亦然.

完成所有工作后,进入服务器端WCF类以传输给客户端,这似乎是我所缺少的.

手动映射:

I am trying out AutoMapper on a Silverlight project.

I am a newbie on AutoMapper and actually just got introduced to it yesterday.

My Sample''s data communication pattern goes thus: RDBM => ORM => Repository => DTO and vice versa.

Having done everything and getting to my server side WCF class for transmission to client I seem to be lacking there.

Manual Mapping:

public IEnumerable<UserPM> GetByID(int userID)
{
    var user = _repo.GetByID(userID);
    return new UserPM()
    {
        UserName = user.UserName,
        FirstName = user.FirstName
        LastName = user.LastName
    }
}



AutoMapper:



AutoMapper:

public IEnumerable<UserPM> GetByID(int userID) // Conversion error except for public IEnumerable<User> GetByID(int userID)
{
    var user = _repo.GetByID(userID);
    AutoMapper.Mapper.CreateMap<User, UserPM>();
    return user;
}



这是问题所在.我收到一条错误消息,指出不能将用户隐式转换为UserPM.我的存储库在那里工作.原因
一个Presentation Model可以为您的特定请求过滤数据...我在这里错了吗...请帮忙.



Here is the issue. I get an error saying User can not be implicitly converted to UserPM. My repository at work there. The reason
a Presentation Model is to filter your data for specific requests... Am I wrong here... Please help.

推荐答案

我相信您需要使用ForMember方法
I beleave you need to use the ForMember method
AutoMapper.Mapper.CreateMap<user,>()
          .ForMember(dest => dest.UserName, o => o.MapFrom(src => src.UserName)
          .ForMember(dest => dest.FirstName, o => o.MapFrom(src => src.FirstName)
          .ForMember(dest => dest.LastName, o => o.MapFrom(src => src.LstName);


编辑
有关在AutoMapper中进行投影的简短描述 [


Edit
A short description about doing projections in AutoMapper[^]


这篇关于AutoMapper和存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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