简体   繁体   English

在父对象内映射子对象

[英]Mapping child object inside parent object

I'm having a bit of an issue. 我有一个问题。 I don't quite know how to handle the situation so I'll just explain a simplified scenario and hopefully you can help me. 我不太了解如何处理这种情况,所以我只向您解释一个简化的情况,希望您能为我提供帮助。

I'm trying to map a parent database object to a parent bll object. 我正在尝试将父数据库对象映射到父bll对象。 In this parent database object, there is a foreign key to the ID of the child, and in my parent bll object I use the child bll object (containing more than just an ID). 在此父数据库对象中,有一个子ID的外键,在我的父bll对象中,我使用了子bll对象(不仅仅包含一个ID)。

So here are my bll objects: 所以这是我的bll对象:

public class Parent
{
    public int ID { get; set; }
    public Child Child { get; set; }
}

public class Child
{
    public int ID { get; set; }
    public string FirstName { get; set; }
}

And here is my mapper class/method: 这是我的映射器类/方法:

public class ParentMapper
{
    public Parent MapFromSource(ParentDatabaseObject parentDO)
    {
        Parent parent = new Parent();
        parent.ID = parentDO.ID;
        parent.Child = ???;
        return parent;
    }
}

I don't think it's very important what the ParentDatabaseObject looks like in this case, I'd just like to know how I should map the parent.Child object. 在这种情况下,我认为ParentDatabaseObject的外观并不重要,我只是想知道如何映射parent.Child对象。

I have considered the following: 我考虑了以下内容:

parent.Child = new Child();
parent.Child.ID = doParent.Child.Id;
parent.Child.FirstName = doParent.Child.FirstName;

Which doesn't feel right, 'cause I kind of have the urge to put this in my ChildMapper, which leads me to my second way of implementing this (assuming I have a seperate child mapper and have an instance of it called childMapper): 感觉不对,因为我有点想把它放在ChildMapper中,这导致我实现了第二种实现此方法的方法(假设我有一个单独的子映射器,并且有一个实例称为childMapper):

parent.Child = childMapper.MapFromSource(parentDO.Child);

But I kind of have the feeling that using this way of mapping is making my code a bit tightly coupled, because I'd be using my ChildMapper in my ParentMapper. 但是我有点感觉,使用这种映射方式会使我的代码紧密耦合,因为我会在ParentMapper中使用ChildMapper。

So I guess my question is: how should I implement this kind of mapping. 所以我想我的问题是:我应该如何实现这种映射。 Is this last method correct or is there something even better? 最后一种方法正确还是有更好的方法? I'm already discarding the first thing I tried. 我已经舍弃了我尝试过的第一件事。

Thanks for your help! 谢谢你的帮助!

(I did research before posting this question and this was the closest I could find: Data Mapper for Child Objects , but I wasn't really content with the only answer in there) (我在发布此问题之前进行了研究,这是我能找到的最接近的数据适用于Child Objects的Data Mapper ,但我对其中的唯一答案并不满意)

Shouldn't it be better - 应该不会更好-

parent.Child = childMapper.MapFromSource(parentDO.FoeignKeyToTheChild);  

I think you should have methods to get object by Id. 我认为您应该具有通过ID获取对象的方法。

EDIT : If your mapper doesn't DataAccess code, then you have to map the child within your Repository . 编辑:如果您的映射器不是DataAccess代码,则必须在Repository映射子代。 As your Repository already have DataObjects ready, you can do it the following way - 由于您的Repository已经准备好DataObjects,因此您可以通过以下方式进行操作-

ParentMapper: ParentMapper:

public class ParentMapper
{
    public Parent MapFromSource(ParentDo parentDo)
    {
        Parent parent = new Parent();
        parent.Id = parentDo.Id;
        return parent;
    }
}  

ChildMapper: ChildMapper:

public class ChildMapper
{
    public Child MapFromSource(ChildDo childDo)
    {
        Child child = new Child();
        child.Id = childDo.Id;
        child.FirstName = childDo.FirstName;

        return child;
    }
}  

Repository: 仓库:

public class Repository
{
    //you already have parentDo
    //you already have childDo

    public Parent GetParent()
    {
        Parent parent = parentMapper.MapFromSource(parentDo);
        parent.Child = childMapper.MapFromSource(childDo);
        return parent;
    }

    public Child GetChild()
    {
        Child child = childMapper.MapFromSource(childDo);
        return child;
    }
}  

Otherwise, your Mapper must have access to DataAccess code. 否则,您的Mapper必须有权访问DataAccess代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM