0

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. 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).

So here are my bll objects:

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.

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):

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.

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)

Community
  • 1
  • 1

1 Answers1

0

Shouldn't it be better -

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

I think you should have methods to get object by Id.

EDIT : If your mapper doesn't DataAccess code, then you have to map the child within your Repository. As your Repository already have DataObjects ready, you can do it the following way -

ParentMapper:

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

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.

atiyar
  • 7,762
  • 6
  • 34
  • 75
  • I don't think so, this method doesn't make database calls, it just maps database objects that I have already fetched, to a bll object. –  Aug 26 '12 at 20:58
  • so you wanna pass in the DataObject to the mapper method and get the DomainObject, right? and who calls this mapper methods? do you have a repository? If you have a mapping-layer, then your domain-layer should not have direct access to a DataObject. Who actually has the reference to the Mapper objects? – atiyar Aug 26 '12 at 21:04
  • My reference to the mapper is in the repository I'm using. My repository gets a parent by id and then I call the mapper in my repository to convert the database object to my domain object –  Aug 26 '12 at 21:07
  • Okay, so you are suggesting I should use the the childMapper class in my ParentRepository instead of using it in my ParentMapper? –  Aug 26 '12 at 21:42
  • Why do you even have separate repositories? To isolate your Data Access Layer from your Domain Layer you need a central repository. For convenience you can use them in pieces as `partial` classes. But always use the `Repository` as central. – atiyar Aug 26 '12 at 21:50
  • I have interface driven repositories. I use 1 main repository IRepository after that I create a more specific interface repository IParentRepository which inherits from IRepository and then I have an implementation which inherits from IParentRepository. –  Aug 26 '12 at 21:59
  • If you're doing it `IRepository`-way, then i think, your DomainObjects should be mapped before they reach the Repositories. You can let your mapping layer have access to data access code and get `Child` by Id from there, or better use an ORM, like NHibernate or Entity Framework. – atiyar Aug 26 '12 at 22:13
  • I'm already using NHibernate, I'm just adding another manual mapping from nhibernate objects to my own domain objects, because I don't want to us the nhibernate objects in my UI. But I think we're straying from my question, I just want to know whether it's okay to call a mapper inside another mapper or not –  Aug 26 '12 at 22:17
  • then map your object before the repositories. your mapping layer *can* actually have access to your data access code, there's nothing wrong with it. see this - http://stackoverflow.com/questions/5881872/ddd-how-the-layers-should-be-organized – atiyar Aug 26 '12 at 22:21
  • Okay, I think I sort of got it, still not too comfortable with it, but it doesn't bother me as much as the other methods. Maybe I'll figure out a better way along the process. Thanks for your help! –  Aug 28 '12 at 08:20