AutoMapper 根据外部值有条件地映射 属性
AutoMapper conditionally map a property based on an external value
我有从 API 搜索方法返回的搜索结果,我希望尽可能缩短响应内容的长度。
我还设置了 AutoMapper,它本身与配置文件中配置的各种映射配合得很好。
搜索结果的其中一个属性可能相当重要,我不想包含该数据,因为它不太可能总是需要的。所以在搜索条件上我添加了一个标志来包含这个 属性.
有没有办法根据其他外部因素有条件地映射 属性?
目前,在地图配置中我已经告诉它忽略权重 属性,然后如果条件指定它,我随后映射另一个集合并将其分配给搜索结果。
例如在映射配置文件中:
this.CreateMap<myModel, myDto>()
.ForMember((dto) => dto.BigCollection,
(opt) => opt.Ignore())
然后在代码中:
results.MyDtos = myModels.Select((m) => Mapper.Map<myDto>(m));
if (searchCriteria.IncludeBigCollection)
{
foreach(MyDto myDto in results.MyDtos)
{
// Map the weighty property from the appropriate model.
myDto.BigCollection = ...
}
}
如果您使用的是 Automapper 5.0,那么您可以使用 IMappingOperationOptions
和 IValueResolver
将值从方法范围传递到映射器本身。
这是一个例子:
您的价值解析器:
class YourValueResolver : IValueResolver<YourSourceType, YourBigCollectionType>
{
public YourBigCollectionType Resolve(YourSourceType source, YourBigCollectionType destination, ResolutionContext context)
{
// here you need your check
if((bool)context.Items["IncludeBigCollection"])
{
// then perform your mapping
return mappedCollection;
}
// else return default or empty
return default(YourBigCollectionType);
}
}
配置您的映射:
new MapperConfiguration(cfg =>
{
cfg.CreateMap<YourSourceType, YourDestinationType>().ForMember(d => d.YourBigCollection, opt => opt.ResolveUsing<YourValueResolver>());
});
然后您可以调用 Map
方法,例如:
mapper.Map<YourDestinationType>(yourSourceObj, opt.Items.Add("IncludeBigCollection", IncludeBigCollectionValue));
IncludeBigCollectionValue
将被传递到值解析器并根据您在其中编写的内容使用。
我有从 API 搜索方法返回的搜索结果,我希望尽可能缩短响应内容的长度。
我还设置了 AutoMapper,它本身与配置文件中配置的各种映射配合得很好。
搜索结果的其中一个属性可能相当重要,我不想包含该数据,因为它不太可能总是需要的。所以在搜索条件上我添加了一个标志来包含这个 属性.
有没有办法根据其他外部因素有条件地映射 属性?
目前,在地图配置中我已经告诉它忽略权重 属性,然后如果条件指定它,我随后映射另一个集合并将其分配给搜索结果。
例如在映射配置文件中:
this.CreateMap<myModel, myDto>()
.ForMember((dto) => dto.BigCollection,
(opt) => opt.Ignore())
然后在代码中:
results.MyDtos = myModels.Select((m) => Mapper.Map<myDto>(m));
if (searchCriteria.IncludeBigCollection)
{
foreach(MyDto myDto in results.MyDtos)
{
// Map the weighty property from the appropriate model.
myDto.BigCollection = ...
}
}
如果您使用的是 Automapper 5.0,那么您可以使用 IMappingOperationOptions
和 IValueResolver
将值从方法范围传递到映射器本身。
这是一个例子:
您的价值解析器:
class YourValueResolver : IValueResolver<YourSourceType, YourBigCollectionType>
{
public YourBigCollectionType Resolve(YourSourceType source, YourBigCollectionType destination, ResolutionContext context)
{
// here you need your check
if((bool)context.Items["IncludeBigCollection"])
{
// then perform your mapping
return mappedCollection;
}
// else return default or empty
return default(YourBigCollectionType);
}
}
配置您的映射:
new MapperConfiguration(cfg =>
{
cfg.CreateMap<YourSourceType, YourDestinationType>().ForMember(d => d.YourBigCollection, opt => opt.ResolveUsing<YourValueResolver>());
});
然后您可以调用 Map
方法,例如:
mapper.Map<YourDestinationType>(yourSourceObj, opt.Items.Add("IncludeBigCollection", IncludeBigCollectionValue));
IncludeBigCollectionValue
将被传递到值解析器并根据您在其中编写的内容使用。