使用 Automapper 将方法映射到字段
Map a method to a field with Automapper
我想使用Automapper,需要映射一个方法到字段(比如命令部分)。
我该怎么做?
这是我的第一个代码
foreach(source item in sources)
{
ss.ServerStatusType = ServerStatusTypeName(item);
ss.A = item.A ;
ss.B =item.B;
destinations.Add(dto);
}
我想使用 Automapper,我只是对 ServerStatusTypeName 方法有问题
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<IList<source >, IList<Destination>>());
IList<Source> statuses = context.Sp_ServerUpdateStatus(userName).ToList();
var mapper = new Mapper(config);
IList<Source> dto = mapper.Map<IList<Destination>>(statuses);
一切正常只是ss.ServerStatusType为null因为我用了一个方法来填充这个项目
你可以试试
class YourObjectProfile : Profile
{
public YourObjectProfile()
{
CreateMap<UI_Object, Domain_Object>()
.ForMember(c => c.name, p => p.MapFrom(dbc => dbc.name));
CreateMap<Domain_Object, UI_Object>()
.ForMember(dbc => dbc.name, dbp => dbp.MapFrom(c => c.name));
}
}
我用这个代码没问题
CreateMap<Sp_ServerUpdateStatus_Result, ServerStatus>()
.ForMember(dest=>dest.ServerStatusType, opt => opt.MapFrom(src=> ServerStatusTypeName(src))
);
...
ServerStatusTypeName(src) 是一种方法
我想使用Automapper,需要映射一个方法到字段(比如命令部分)。 我该怎么做? 这是我的第一个代码
foreach(source item in sources)
{
ss.ServerStatusType = ServerStatusTypeName(item);
ss.A = item.A ;
ss.B =item.B;
destinations.Add(dto);
}
我想使用 Automapper,我只是对 ServerStatusTypeName 方法有问题
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<IList<source >, IList<Destination>>());
IList<Source> statuses = context.Sp_ServerUpdateStatus(userName).ToList();
var mapper = new Mapper(config);
IList<Source> dto = mapper.Map<IList<Destination>>(statuses);
一切正常只是ss.ServerStatusType为null因为我用了一个方法来填充这个项目
你可以试试
class YourObjectProfile : Profile
{
public YourObjectProfile()
{
CreateMap<UI_Object, Domain_Object>()
.ForMember(c => c.name, p => p.MapFrom(dbc => dbc.name));
CreateMap<Domain_Object, UI_Object>()
.ForMember(dbc => dbc.name, dbp => dbp.MapFrom(c => c.name));
}
}
我用这个代码没问题
CreateMap<Sp_ServerUpdateStatus_Result, ServerStatus>()
.ForMember(dest=>dest.ServerStatusType, opt => opt.MapFrom(src=> ServerStatusTypeName(src))
);
... ServerStatusTypeName(src) 是一种方法