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 structureThe 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
internalclass EventInfo { publicstring EventName { get; set; } publicstring Location { get; set; } public DateTime? Start { get; set; } public DateTime? End { get; set; } publicstring EntryDescription { get; set; } publicstring OrganizerOrgName { get; set; } publicstring ActivityName { get; set; } publicstring EntryContactPersonName { get; set; } publicstring EntryContactPhone { get; set; } publicstring EntryContactEmail { get; set; } publicstring EntryContactHomePage { get; set; } publicstring Level { get; set; } publicstring SeasonName { get; set; } publicstring OrganizerContactPhone { get; set; } publicstring OrganizerContactEmail { get; set; } publicstring OrganizerContactHomePage { get; set; } publicint ArrangingOrganizaionId { get; set; } publicint ActivityId { get; set; } publicint ParentActivityId { get; set; } public DateTime? DeadLine { get; set; } } internalclass Event { publicstring EventName { get; set; } publicstring Location { get; set; } public DateTime? Start { get; set; } public DateTime? End { get; set; } publicstring 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() { returnnew Deadline {Date = DateTime.Now}; } } internalclass EventOrganiser { public Contact Contact { get; set; } public String OrgName { get; set; } } internalclass Contact { public String TelephoneNo1 { get; set; } public String Email { get; set; } public String HomePage { get; set; } } internalclass EntryInfo { public String Description { get; set; } public String ContactPersonName { get; set; } public Contact Contact { get; set; } } internalclass Activity { public String Name { get; set; } publicint Id { get; set; } publicint ParentActivityId { get; set; } } internalclass Season { public String Name { get; set; } } internalclass Deadline { public DateTime Date { get; set; } } internalclass OrgElement { publicint 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; }