英文:
Mapping complex objects using Automapper
问题
public class ProductEntity
{
public string Name { get; set; }
public CategoryEntity Category { get; set; }
}
public class CategoryEntity
{
public string Name { get; set; }
}
public class ProductDTO
{
public string Name { get; set; }
public CategoryDTO Category { get; set; }
}
public class CategoryDTO
{
public string Name { get; set; }
}
public class CategoryProfile : Profile
{
public CategoryProfile()
{
CreateMap<CategoryEntity, CategoryDTO>().ReverseMap();
}
}
public class ProductProfile : Profile
{
public ProductProfile()
{
CreateMap<ProductEntity, ProductDTO>().ReverseMap();
}
}
嘿,我是一位初学者开发者,我想在这个示例中得到一些帮助,是否可以重用CategoryProfile的映射配置在ProductProfile中?我的代码总是返回空值,我不想在ProductProfile中包含"CreateMap<CategoryEntity, CategoryDTO>().ReverseMap();"。
我正在使用dotnet Core 6。
我已经包括了"CreateMap<CategoryEntity, CategoryDTO>()",它有效了,但我希望以一种可以重用配置的方式来实现这一点。
英文:
public class ProductEntity
{
public string Name {get; set;}
public CategoryEntity Category {get; set;}
}
public class CategoryEntity
{
public string Name {get; set;}
}
public class ProductDTO
{
public string Name {get; set;}
public CategoryDTO Category {get; set;}
}
public class CategoryDTO
{
public string Name {get; set;}
}
public class CategoryProfile : Profile
{
public CategoryProfile()
{
CreateMap<CategoryEntity, CategoryDTO>().ReverseMap();
}
}
public class ProductProfile: Profile
{
public ProductProfile()
{
CreateMap<ProductEntity, ProductDTO>().ReverseMap();
}
}E
Hey guys, I'm a beginner developer, and I would like some help in this example it is possible to reuse the CategoryProfile mepement in the ProductProfile, my code always returns as empty, I would not like to include the "CreateMap<CategoryEntity, CategoryDTO>().ReverseMap();" in ProductProfile.
I'm using dotnet Core 6.
I included CreateMap<CategoryEntity, CategoryDTO>() and it worked, but I would like to do this in a way that it was possible to reuse the logic applied in the profile.
答案1
得分: 2
你可以通过在MapperConfiguration
实例上调用AddProfiles
方法将这两个配置文件都添加到AutoMapper
中。
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new ProductProfile());
cfg.AddProfile(new CategoryProfile());
});
IMapper mapper = config.CreateMapper();
通过将这两个配置文件添加到同一配置中,AutoMapper
将能够在不同类型之间映射对象时使用在这两个配置文件中定义的映射配置。
可以通过多种方式将配置文件添加到主映射配置中,可以直接添加,也可以通过自动扫描配置文件的方式:
// 扫描程序集中的所有配置文件
// ... 使用实例方式:
var config = new MapperConfiguration(cfg => {
cfg.AddMaps(myAssembly);
});
var configuration = new MapperConfiguration(cfg => cfg.AddMaps(myAssembly));
// 也可以使用程序集名称:
var configuration = new MapperConfiguration(cfg =>
cfg.AddMaps(new [] {
"Foo.UI",
"Foo.Core"
})
);
// 或者使用标记类型来指定程序集:
var configuration = new MapperConfiguration(cfg =>
cfg.AddMaps(new [] {
typeof(HomeController),
typeof(Entity)
})
);
英文:
You can add both profiles to AutoMapper
by calling the AddProfiles
method on the MapperConfiguration
instance.
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new ProductProfile());
cfg.AddProfile(new CategoryProfile());
});
IMapper mapper = config.CreateMapper();
By adding both profiles to the same configuration, AutoMapper
will be able to use the mapping configurations defined in both profiles when mapping objects between types.
Profiles can be added to the main mapper configuration in a number of ways, either directly or by automatically scanning for profiles:
// Scan for all profiles in an assembly
// ... using instance approach:
var config = new MapperConfiguration(cfg => {
cfg.AddMaps(myAssembly);
});
var configuration = new MapperConfiguration(cfg => cfg.AddMaps(myAssembly));
// Can also use assembly names:
var configuration = new MapperConfiguration(cfg =>
cfg.AddMaps(new [] {
"Foo.UI",
"Foo.Core"
});
);
// Or marker types for assemblies:
var configuration = new MapperConfiguration(cfg =>
cfg.AddMaps(new [] {
typeof(HomeController),
typeof(Entity)
});
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论