Given we have these classes:

public class GuiPerson
{
    public String Name { get; set; }
    public GuiAddress Address { get; set; }
}

public class GuiAddress
{
    public String City { get; set; }
}

public class DomainPerson
{
    public String Name { get; set; }
    public DomainAddress Address { get; set; }
}

public class DomainAddress
{
    public String City { get; set; }
}

And we want to map between DomainPerson and GuiPerson. A nested mapping would then be the mapping between DomainPersons Address-property and the GuiPersons Address-property. This is a nested mapping because the mapping between them has to be set up as a mapping itself. This is how you do it.

First you have to define the relations between the nested types, that is, between DomainAddress and GuiAddress:

var addressMap = new Mapping<DomainAddress, GuiAddress>();
addressMap.AutoRelateEqualNames();

Just like a normal mapping. Then we set up the mapping between our DomainPerson and GuiPerson:

personMapping = new Mapping<DomainPerson, GuiPerson>();
personMapping.AddConverter(addressMap);
personMapping.AutoRelateEqualNames();

Note that we add the addressMap as a converter. This means our personMapping will now how to "convert" between the Address properties.

Now, the only thing left is to do the actual mapping:

var domainPerson = new DomainPerson {Address = new DomainAddress {City = "Nested city name"}};
var guiPerson = new GuiPerson {Address = new GuiAddress()};
personMapping.Map(domainPerson, guiPerson);
Assert.Equal(domainPerson.Address.City,guiPerson.Address.City);

You can ofcouse use the mapping bidirectional.

In essence, the only thing you have to do is to add the mapping of the nested types as a converter, and you're good to go.

Important Note: You have to instanciate the GuiAddress (nested type of the reciever). Glue does not want to take responsibilty for creating objects. This should be done by factories or others that knows how to create objects in a valid state. Letting a framework like Glue do it is not considered good practice.

Last edited Jun 30, 2009 at 6:10 AM by ToreVestues, version 2

Comments

No comments yet.