This project is read-only.

Generally, mapping between lists and arrays are done just as you map between any other type.

The supported types are:
  • Arrays
  • List<T>
  • IList<T>
  • ICollection<T>
  • IEnumerable<T>

(Only generic types are supported since Glue must know the type of objects the lists contains)

You can map between any of the list types to any other of the list types.


Given these two classes:

class DomainPerson
    public int[] Numbers { get; set; }
class GuiPerson
    public IList<int> Numbers { get; set; }

We create an object domainPerson with an array of Numbers:

var domainNumbers = new[] { 1, 2, 3 };
var domainPerson = new DomainPerson { Numbers = domainNumbers };

And we want to map that object to a GuiPerson-object. This is done as a normal mapping. First we relate:

var mapping = new Mapping<DomainPerson, GuiPerson>();
mapping.Relate(domain => domain.Numbers, gui => gui.Numbers);

Then we map, and verify:

var guiPerson = mapping.Map(domainPerson, new GuiPerson());
var guiNumbers = guiPerson.Numbers;

Assert.Equal(domainNumbers.Length, guiNumbers.Count);
Assert.Equal(domainNumbers[0], guiNumbers[0]);
Assert.Equal(domainNumbers[1], guiNumbers[1]);
Assert.Equal(domainNumbers[2], guiNumbers[2]);

Lists with nested types

Given these classes:

class DomainPerson { public DomainAddress[] Addresses { get; set; } }
class DomainAddress { public String City { get; set; } }

class GuiPerson { public GuiAddress[] Addresses { get; set; } }
class GuiAddress { public String City { get; set; } }

First what we have to do is to describe how to map between DomainAddress and GuiAddress

var addressMapping = new Mapping<DomainAddress, GuiAddress>(x => new DomainAddress(), x => new GuiAddress());

Note how we instantiate Mapping. Glue does not want to create objects for you, since it cannot guarantee that it creates valid objects (with all the correct dependencies and so on), so you have to tell Glue how to create types. When dealing with arrays and lists, Glue will have to create an arbitrary number of objects to put in the destination array or list. When you create a mapping between two types used in the arrays you want to map between, you must always supply how to create a valid object of its types. This you do here, in the Mapping-constructor.

When that is done, the rest is done the normal way. We relate the properties containing the array with the nested types. As you see, this is handled like a normal nested relation:

var mapping = new Mapping<DomainPerson, GuiPerson>();
mapping.Relate(domain => domain.Addresses, gui => gui.Addresses, addressMapping);

Then we create the object to map from:

var domainPerson = new DomainPerson
       Addresses = new DomainAddress[]
                   new DomainAddress {City = "City1"}, 
                   new DomainAddress {City = "City2"}

And then we map and verify:

var guiPerson = mapping.Map(domainPerson, new GuiPerson());

Assert.Equal(domainPerson.Addresses.Length, guiPerson.Addresses.Length);
Assert.Equal(domainPerson.Addresses[1].City, guiPerson.Addresses[1].City);

Last edited Sep 19, 2009 at 11:11 AM by ToreVestues, version 11


No comments yet.