Example: Flattening a large structure

This example uses explicit nested mappings. Often you would want to map this with simplified nested mappings, here's an example of that: Flattening a large structure

The actual mapping (printed with the PrintTool)

Event <--------------------------> EventInfo
-----------------------------------------------------------
Activity.Id ---------------------> ActivityId
Activity.Name -------------------> ActivityName
Activity.ParentActivityId -------> ParentActivityId
ArrangingOrganization.OrgId -----> ArrangingOrganizaionId
End <----------------------------> End
EntryInfo.Contact.Email ---------> EntryContactEmail
EntryInfo.Contact.HomePage ------> EntryContactHomePage
EntryInfo.Contact.TelephoneNo1 --> EntryContactPhone
EntryInfo.ContactPersonName -----> EntryContactPersonName
EntryInfo.Description -----------> EntryDescription
EventName <----------------------> EventName
GetLastDeadLine.Date ------------> DeadLine
Level <--------------------------> Level
Location <-----------------------> Location
Organizer.Contact.Email ---------> OrganizerContactEmail
Organizer.Contact.HomePage ------> OrganizerContactHomePage
Organizer.Contact.TelephoneNo1 --> OrganizerContactPhone
Organizer.OrgName ---------------> OrganizerOrgName
Season.Name ---------------------> SeasonName
Start <--------------------------> Start


The classes

internal class EventInfo
{
    public string EventName { get; set; }
    public string Location { get; set; }
    public DateTime? Start { get; set; }
    public DateTime? End { get; set; }
    public string EntryDescription { get; set; }
    public string OrganizerOrgName { get; set; }
    public string ActivityName { get; set; }
    public string EntryContactPersonName { get; set; }
    public string EntryContactPhone { get; set; }
    public string EntryContactEmail { get; set; }
    public string EntryContactHomePage { get; set; }
    public string Level { get; set; }
    public string SeasonName { get; set; }
    public string OrganizerContactPhone { get; set; }
    public string OrganizerContactEmail { get; set; }
    public string OrganizerContactHomePage { get; set; }
    public int ArrangingOrganizaionId { get; set; }
    public int ActivityId { get; set; }
    public int ParentActivityId { get; set; }
    public DateTime? DeadLine { get; set; }
}

internal class Event
{
    public string EventName { get; set; }
    public string Location { get; set; }
    public DateTime? Start { get; set; }
    public DateTime? End { get; set; }
    public string Level { get; set; }
    public EventOrganiser Organizer { get; set; }
    public EntryInfo EntryInfo { get; set; }
    public Activity Activity { get; set; }
    public Season Season { get; set; }
    public OrgElement ArrangingOrganization { get; set; }

    public Deadline GetLastDeadLine()
    {
        return new Deadline {Date = DateTime.Now};
    }
}

internal class EventOrganiser
{
    public Contact Contact { get; set; }
    public String OrgName { get; set; }
}

internal class Contact
{
    public String TelephoneNo1 { get; set; }
    public String Email { get; set; }
    public String HomePage { get; set; }
}

internal class EntryInfo
{
    public String Description { get; set; }
    public String ContactPersonName { get; set; }
    public Contact Contact { get; set; }
}

internal class Activity
{
    public String Name { get; set; }
    public int Id { get; set; }
    public int ParentActivityId { get; set; }
}

internal class Season
{
    public String Name { get; set; }
}

internal class Deadline
{
    public DateTime Date { get; set; }
}

internal class OrgElement
{
    public int OrgId { get; set; }
}

The mapping

private Mapping<Event, EventInfo> SetUpMapper()
{
    var mapping = new Mapping<Event, EventInfo>();
    mapping.AutoRelateEqualNames(true);
    mapping.Flatten(x => x.Organizer, GetOrganizerMapping());
    mapping.Flatten(x => x.EntryInfo, GetEntryMapping());
    mapping.Flatten(x => x.Activity, GetActivityMapping());
    mapping.Flatten(x => x.Season, GetSeasonMapping());
    mapping.Flatten(x => x.GetLastDeadLine(), GetDeadlineMapping());
    mapping.Flatten(x => x.ArrangingOrganization, GetArrangingOrganizationMapping());
    return mapping;
}

private Mapping<OrgElement, EventInfo> GetArrangingOrganizationMapping()
{
    var mapping = new Mapping<OrgElement, EventInfo>();
    mapping.RelateTowardsRight(x => x.OrgId, y => y.ArrangingOrganizaionId);
    return mapping;
}

private Mapping<Deadline, EventInfo> GetDeadlineMapping()
{
    var mapping = new Mapping<Deadline, EventInfo>();
    mapping.RelateTowardsRight(x => x.Date, y => y.DeadLine);
    return mapping;
}

private Mapping<Season, EventInfo> GetSeasonMapping()
{
    var mapping = new Mapping<Season, EventInfo>();
    mapping.RelateTowardsRight(x => x.Name, y => y.SeasonName);
    return mapping;
}

private Mapping<Activity, EventInfo> GetActivityMapping()
{
    var mapping = new Mapping<Activity, EventInfo>();
    mapping.RelateTowardsRight(x => x.Name, y => y.ActivityName);
    mapping.RelateTowardsRight(x => x.Id, y => y.ActivityId);
    mapping.RelateTowardsRight(x => x.ParentActivityId, y => y.ParentActivityId);
    return mapping;
}

private Mapping<EntryInfo, EventInfo> GetEntryMapping()
{
    var mapping = new Mapping<EntryInfo, EventInfo>();
    mapping.RelateTowardsRight(x => x.Description, y => y.EntryDescription);
    mapping.RelateTowardsRight(x => x.ContactPersonName, y => y.EntryContactPersonName);
    mapping.Flatten(x => x.Contact, GetEntryContactMapping());
    return mapping;
}

private Mapping<Contact, EventInfo> GetEntryContactMapping()
{
    var mapping = new Mapping<Contact, EventInfo>();
    mapping.RelateTowardsRight(x => x.TelephoneNo1, y => y.EntryContactPhone);
    mapping.RelateTowardsRight(x => x.Email, y => y.EntryContactEmail);
    mapping.RelateTowardsRight(x => x.HomePage, y => y.EntryContactHomePage);
    return mapping;
}

private Mapping<EventOrganiser, EventInfo> GetOrganizerMapping()
{
    var mapping = new Mapping<EventOrganiser, EventInfo>();
    mapping.Flatten(x => x.Contact, GetOrganiserContactMapping());
    mapping.RelateTowardsRight(x => x.OrgName, y => y.OrganizerOrgName);
    return mapping;
}

private Mapping<Contact, EventInfo> GetOrganiserContactMapping()
{
    var mapping = new Mapping<Contact, EventInfo>();
    mapping.RelateTowardsRight(x => x.Email, y => y.OrganizerContactEmail);
    mapping.RelateTowardsRight(x => x.HomePage, y => y.OrganizerContactHomePage);
    mapping.RelateTowardsRight(x => x.TelephoneNo1, y => y.OrganizerContactPhone);
    return mapping;
}

Last edited Oct 10, 2009 at 6:17 AM by ToreVestues, version 1

Comments

No comments yet.