The concept is that you define the relations between two classes. These relations can be used to map data between the classes.

Given these two classes:

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

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

Bidirectional mapping

To be able to map a property both ways between these classes, you first define the bidirectional relation:

mapping = new Mapping<DomainPerson, GuiPerson>();
mapping.Relate(domainperson => domainperson.Name, guiperson => guiperson.Name);
// or you could automate this with: mapping.AutoRelateEqualNames();

Then you can map data both ways:

first, map "TowardsRight":
domainPerson = new DomainPerson();
guiPerson = new GuiPerson();

domainPerson.Name = "Tore";
mapping.Map(domainPerson, guiPerson);

Assert.Equal("Tore",guiPerson.Name);

then the other way, "TowardsLeft":
domainPerson = new DomainPerson();
guiPerson = new GuiPerson();

guiPerson.Name = "Tore";
mapping.Map(guiPerson, domainPerson);

Assert.Equal("Tore", domainPerson.Name);

Unidirectional mapping

Sometimes you might be in a position where you want to map a property one way, but deny it to be mapped the other way. Typical examples are ID fields that you do not want to map back to a domain model. This is easy with Glue.

In this example, we want to map the Name property to the GuiPerson, but not back to the DomainPerson. This is how you do it:

mapping = new Mapping<DomainPerson, GuiPerson>();
mapping.RelateTowardsRight(domainperson => domainperson.Name, guiperson => guiperson.Name);

Now it maps the name from domainPerson to guiPerson (TowardsRight):

domainPerson.Name = "Tore";
mapping.Map(domainPerson, guiPerson);

Assert.Equal("Tore", guiPerson.Name);

But not the other way (TowardsLeft):

guiPerson.Name = "Tore";
domainPerson.Name = "Henning";
mapping.Map(guiPerson, domainPerson);

Assert.Equal("Tore", guiPerson.Name);
Assert.Equal("Henning", domainPerson.Name);

Last edited Aug 8, 2009 at 6:39 AM by ToreVestues, version 3

Comments

No comments yet.