Given these classes:

public class GuiPerson
{
    public String Name { get; set; }
    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 flattening is when you have a nested structure like DomainPerson has a DomainAddress in its Address-property, the properties of DomainAddress (the nested type) will be mapped not to a nested structure on GuiPerson, but to GuiPerson itself: DomainPerson.Address.City is mapped to GuiPerson.City.

Lets demonstrate. First we want to relate the City property of DomainAddress to the City property of GuiPerson:

var addressMapping = new Mapping<DomainAddress, GuiPerson>();
addressMapping.AutoRelateEqualNames();

Then we want to relate the two main types, DomainPerson and GuiPerson:

var personMapping = new Mapping<DomainPerson, GuiPerson>();

Then we need to tell Glue that when it maps the values in the nested DomainAddress, then it should map it to GuiPerson itself, not a nested object on GuiPerson. Then we have to flatten:

personMapping.Flatten(domain => domain.Address, addressMapping);

We state the nested property, and the mapping that defines how the relations are set up. Now to demonstrate the actual mapping:

var domainPerson = new DomainPerson { Address = new DomainAddress { City = "Oslo"} };
var guiperson = personMapping.Map(domainPerson, new GuiPerson());
Assert.Equal("Oslo", guiperson.City);

Last edited Jun 30, 2009 at 1:55 PM by ToreVestues, version 2

Comments

No comments yet.