1

我需要设置一个AfterMapforAutoMapper但我没有使用通用版本,因为我在运行时根据需要创建地图。如果我使用通用版本,我会这样做:

Mapper.CreateMap<DALEntity, BLLEntity>()
    .AfterMap((DALEntity dalEntity, BLLEntity bllEntity) =>
        (bllEntity as DomainEntityBase).State = DomainEntityState.Unchanged);

工作正常。直到运行时我才知道的其他地图是这样创建的:

Type BLLClassType = Type.GetType(BLLClassName);
Type DALClassType = Type.GetType(DALClassName);
Mapper.CreateMap(DALClassType, BLLClassType);

但现在我无法设置AfterMap。有什么建议么?我只需要在 AutoMapper 完成后设置State属性。bllEntity

4

2 回答 2

3

创建自定义值解析器:

public class StateResolver : IValueResolver
{
    public ResolutionResult Resolve(ResolutionResult source)
    {
        return source.New(DomainEntityState.Unchanged);
    }
}

用法:

Type BLLClassType = Type.GetType(BLLClassName);
Type DALClassType = Type.GetType(DALClassName);

Mapper.CreateMap(DALClassType, BLLClassType)
      .ForMember("State", opt => opt.ResolveUsing<StateResolver>());
于 2012-12-17T17:49:06.660 回答
0

它必须是一个AfterMap吗?例如,您可以UseValue这样使用:

Mapper.CreateMap<DALEntity, BLLEntity>()
    .ForMember(dest => dest.State,
               opt => opt.UseValue(DomainEntityState.Unchanged));
于 2012-12-17T17:26:19.813 回答