使用Automapper映射复杂对象

huangapple go评论58阅读模式
英文:

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&lt;CategoryEntity, CategoryDTO&gt;().ReverseMap();
    }
}

public class ProductProfile: Profile
{
    public ProductProfile()
    {
        CreateMap&lt;ProductEntity, ProductDTO&gt;().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&lt;CategoryEntity, CategoryDTO&gt;().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 =&gt;
{
    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 =&gt; {
    cfg.AddMaps(myAssembly);
});
var configuration = new MapperConfiguration(cfg =&gt; cfg.AddMaps(myAssembly));

// Can also use assembly names:
var configuration = new MapperConfiguration(cfg =&gt;
    cfg.AddMaps(new [] {
        &quot;Foo.UI&quot;,
        &quot;Foo.Core&quot;
    });
);

// Or marker types for assemblies:
var configuration = new MapperConfiguration(cfg =&gt;
    cfg.AddMaps(new [] {
        typeof(HomeController),
        typeof(Entity)
    });
);

huangapple
  • 本文由 发表于 2023年2月27日 04:04:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定