Automapper 返回一个空集合,我想要一个 null
Automapper returning an empty collection, I want a null
public class Person
{
Name { get; set; }
IEnumerable<Address> Addresses { get; set; }
}
public class PersonModel
{
Name { get; set; }
IEnumerable<AddressModel> Addresses { get; set; }
}
如果我像这样将 Person
映射到 PersonModel
:
Mapper.DynamicMap<Person, PersonModel>(person);
如果 Person
上的 Addresses
属性 为空,则它们在 PersonModel
上映射为空 Enumerable
而不是空。
如何让 PersonModel
具有空 Addresses
而不是空 Enumerable
?
因此,您可能有多种方法可以使用 Automapper 完成此操作,这只是其中一种:
Mapper.CreateMap<Person, PersonMap>()
.AfterMap( (src, dest) => dest.Addresses = dest.Addresses?.Any() ? dest.Addresses : null );
此代码使用新的 c# ?.
运算符来确保空值安全,因此如果您不能在代码中使用该功能,则可能需要删除它并检查是否为空值。
您应该能够为您想要启用此行为的 属性 定义自定义解析器。所以像:
Mapper.CreateMap<Address, AddressModel>();
Mapper.CreateMap<Person, PersonModel>()
.ForMember(
dest => dest.Addresses,
opt => opt.ResolveUsing(person => person.Addresses.Any() ? person.Addresses.Select(Mapper.Map<Address, AddressModel>) : null));
另一种替代方法是使用条件,因此仅在值不为空时映射值。
这可能需要该值默认为 null(因为它不会被映射)
Mapper.CreateMap<Person, PersonModel>()
.ForMember(
dest => dest.Addresses,
opt => opt => opt.Condition(source=> source.Addresses!= null));
简单的答案是使用 AllowNullCollections
:
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.AllowNullCollections = true;
});
或者如果您使用实例 API
new MapperConfiguration(cfg =>
{
cfg.AllowNullCollections = true;
}
除了在 the mapper configuration initialization (as noted in 中设置 AllowNullCollections
之外,您还可以选择在 Profile
定义中设置 AllowNullCollections
,如下所示:
public class MyMapper : Profile
{
public MyMapper()
{
// Null collections will be mapped to null collections instead of empty collections.
AllowNullCollections = true;
CreateMap<MySource, MyDestination>();
}
}
如果您希望将其作为 API 中的一般规则,您可以像这样在配置服务方法中配置 Automapper。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddControllers();
[...]
// configure automapping
services.AddAutoMapper(cfg => cfg.AllowNullCollections = true, typeof(Startup));
}
或者,在单独的 DLL 中使用自动映射的情况下(例如:对于 DTO 服务),我更喜欢使用辅助函数,所以配置也应该在那里完成:
public static class MappingHelper
{
private static readonly Lazy<IMapper> _lazy = new(() =>
{
var config = new MapperConfiguration(cfg =>
{
// This line ensures that internal properties are also mapped over.
cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
cfg.AddProfile<DomainToRepositoryProfile>();
cfg.AddProfile<RepositoryToDomainProfile>();
cfg.AllowNullCollections = true;
});
var mapper = config.CreateMapper();
return mapper;
});
public static IMapper Mapper => _lazy.Value;
}
public class Person
{
Name { get; set; }
IEnumerable<Address> Addresses { get; set; }
}
public class PersonModel
{
Name { get; set; }
IEnumerable<AddressModel> Addresses { get; set; }
}
如果我像这样将 Person
映射到 PersonModel
:
Mapper.DynamicMap<Person, PersonModel>(person);
如果 Person
上的 Addresses
属性 为空,则它们在 PersonModel
上映射为空 Enumerable
而不是空。
如何让 PersonModel
具有空 Addresses
而不是空 Enumerable
?
因此,您可能有多种方法可以使用 Automapper 完成此操作,这只是其中一种:
Mapper.CreateMap<Person, PersonMap>()
.AfterMap( (src, dest) => dest.Addresses = dest.Addresses?.Any() ? dest.Addresses : null );
此代码使用新的 c# ?.
运算符来确保空值安全,因此如果您不能在代码中使用该功能,则可能需要删除它并检查是否为空值。
您应该能够为您想要启用此行为的 属性 定义自定义解析器。所以像:
Mapper.CreateMap<Address, AddressModel>();
Mapper.CreateMap<Person, PersonModel>()
.ForMember(
dest => dest.Addresses,
opt => opt.ResolveUsing(person => person.Addresses.Any() ? person.Addresses.Select(Mapper.Map<Address, AddressModel>) : null));
另一种替代方法是使用条件,因此仅在值不为空时映射值。 这可能需要该值默认为 null(因为它不会被映射)
Mapper.CreateMap<Person, PersonModel>()
.ForMember(
dest => dest.Addresses,
opt => opt => opt.Condition(source=> source.Addresses!= null));
简单的答案是使用 AllowNullCollections
:
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.AllowNullCollections = true;
});
或者如果您使用实例 API
new MapperConfiguration(cfg =>
{
cfg.AllowNullCollections = true;
}
除了在 the mapper configuration initialization (as noted in AllowNullCollections
之外,您还可以选择在 Profile
定义中设置 AllowNullCollections
,如下所示:
public class MyMapper : Profile
{
public MyMapper()
{
// Null collections will be mapped to null collections instead of empty collections.
AllowNullCollections = true;
CreateMap<MySource, MyDestination>();
}
}
如果您希望将其作为 API 中的一般规则,您可以像这样在配置服务方法中配置 Automapper。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddControllers();
[...]
// configure automapping
services.AddAutoMapper(cfg => cfg.AllowNullCollections = true, typeof(Startup));
}
或者,在单独的 DLL 中使用自动映射的情况下(例如:对于 DTO 服务),我更喜欢使用辅助函数,所以配置也应该在那里完成:
public static class MappingHelper
{
private static readonly Lazy<IMapper> _lazy = new(() =>
{
var config = new MapperConfiguration(cfg =>
{
// This line ensures that internal properties are also mapped over.
cfg.ShouldMapProperty = p => p.GetMethod.IsPublic || p.GetMethod.IsAssembly;
cfg.AddProfile<DomainToRepositoryProfile>();
cfg.AddProfile<RepositoryToDomainProfile>();
cfg.AllowNullCollections = true;
});
var mapper = config.CreateMapper();
return mapper;
});
public static IMapper Mapper => _lazy.Value;
}