使用 Automapper 展平属性的子类

发布于 2024-09-01 14:21:53 字数 1060 浏览 13 评论 0原文

给定类:

public class Person
{
    public string Name { get; set; }
}

public class Student : Person
{
    public int StudentId { get; set; }
}

public class Source
{
    public Person Person { get; set; }
}

public class Dest
{
    public string PersonName { get; set; }
    public int? PersonStudentId { get; set; }
}

我想使用 Automapper 来映射 Source ->目的地。

该测试显然失败了:

Mapper.CreateMap<Source, Dest>();
var source = new Source() { Person = new Student(){ Name = "J", StudentId = 5 }};

var dest = Mapper.Map<Source, Dest>(source);
Assert.AreEqual(5, dest.PersonStudentId);

鉴于“Person”实际上是整个域模型中广泛使用的数据类型,映射此问题的最佳方法是什么。

编辑:目的是保留“Dest”对象,该对象将具有为“Person”子类型的所有属性定义的字段。因此,我们可以拥有如下所示的源对象,并且不希望为“Person”子类的每种可能的组合创建 Dest 对象:

public class Source2
{
    public Person Value1 { get; set; }
    public Person Value2 { get; set; }
    public Person Value3 { get; set; }
    public Person Value4 { get; set; }
    public Person Value5 { get; set; }
}

Given the classes:

public class Person
{
    public string Name { get; set; }
}

public class Student : Person
{
    public int StudentId { get; set; }
}

public class Source
{
    public Person Person { get; set; }
}

public class Dest
{
    public string PersonName { get; set; }
    public int? PersonStudentId { get; set; }
}

I want to use Automapper to map Source -> Dest.

This test obviously fails:

Mapper.CreateMap<Source, Dest>();
var source = new Source() { Person = new Student(){ Name = "J", StudentId = 5 }};

var dest = Mapper.Map<Source, Dest>(source);
Assert.AreEqual(5, dest.PersonStudentId);

What would be the best approach to mapping this given that "Person" is actually a heavily used data-type throughout our domain model.

Edit: The intent is to persist the "Dest" objects which will have fields defined for all properties of the sub-types of "Person". Hence we could have source objects like the following and would prefer not to have to create Dest objects for every possible combination of "Person" sub-classes:

public class Source2
{
    public Person Value1 { get; set; }
    public Person Value2 { get; set; }
    public Person Value3 { get; set; }
    public Person Value4 { get; set; }
    public Person Value5 { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-09-08 14:21:53

好吧,利用吉米的建议,我已经确定了以下解决方案:

public class Person
{
    public string Name { get; set; }
}

public class Student : Person
{
    public int StudentId { get; set; }
}
//all subtypes of person will map to this dto
public class PersonDto
{
    public string Name { get; set; }
    public int? StudentId { get; set; }
}

public class Source
{
    public Person Person { get; set; }
}

public class DestDto
{
    public PersonDto Person { get; set; }
}

public class Dest
{
    public string PersonName { get; set; }
    public int? PersonStudentId { get; set; }
}

[TestFixture]
public class RandomTests
{
    [Test]
    public void Test1()
    {
        Mapper.CreateMap<Person, PersonDto>();
        Mapper.CreateMap<Student, PersonDto>();

        Mapper.CreateMap<Source, DestDto>();
        Mapper.CreateMap<DestDto, Dest>();

        var source = new Source() { Person = new Student() { Name = "J", StudentId = 5 } };

        var destDto = Mapper.Map<Source, DestDto>(source);
        var destFinal = Mapper.Map<DestDto, Dest>(destDto);

        Assert.AreEqual(5, destFinal.PersonStudentId);
    }
}

很想听到建议/改进。

Well using Jimmy's suggestion I've settled on the following solution:

public class Person
{
    public string Name { get; set; }
}

public class Student : Person
{
    public int StudentId { get; set; }
}
//all subtypes of person will map to this dto
public class PersonDto
{
    public string Name { get; set; }
    public int? StudentId { get; set; }
}

public class Source
{
    public Person Person { get; set; }
}

public class DestDto
{
    public PersonDto Person { get; set; }
}

public class Dest
{
    public string PersonName { get; set; }
    public int? PersonStudentId { get; set; }
}

[TestFixture]
public class RandomTests
{
    [Test]
    public void Test1()
    {
        Mapper.CreateMap<Person, PersonDto>();
        Mapper.CreateMap<Student, PersonDto>();

        Mapper.CreateMap<Source, DestDto>();
        Mapper.CreateMap<DestDto, Dest>();

        var source = new Source() { Person = new Student() { Name = "J", StudentId = 5 } };

        var destDto = Mapper.Map<Source, DestDto>(source);
        var destFinal = Mapper.Map<DestDto, Dest>(destDto);

        Assert.AreEqual(5, destFinal.PersonStudentId);
    }
}

Would love to hear suggestions/improvements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文