AutoMapper和存储库模式 [英] AutoMapper and Repository Pattern
问题描述
我正在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);
Edit
A short description about doing projections in AutoMapper[^]
这篇关于AutoMapper和存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!