CSharp · Console
using MyMapper.UnitTests.Entities;
using MyMapper.UnitTests.Mappers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Xml.Serialization;
public class Program
{
public static void Main()
{
Console.WriteLine("MyMapper");
var dob = DateTime.Now;
IResponseMapper mapper = new ResponseMapper();
Response1 source = new Response1
{
ConsumerID = 123,
Name = "XYZ",
AvgNoOfPurchasesPerMonth = 10,
PeriodInMonths = 12,
Details = new Details1()
{
DOB = dob,
IsDisabled = true
},
MutualFund = new Fund1
{
BankIdNo = 10,
Name = "XYZ",
FundId = 1,
Address = new Address1
{
StreetNo = "123N",
State = new State1
{
Name = "Victoria",
Abbr = "VIC"
},
Country = new Country1()
{
Name = "Australia",
Abbr = "AU"
}
},
FundKeys = new Dictionary<string, string>()
{
{ "ABN", "123456" },
{ "TFN", "9876543" }
}
},
InsuranceMutualFund = new InsuranceMutualFund { MutualFundNumber = "123", TaxNo = "456" },
InsuranceSuperannuation = new InsuranceSuperannuation { SuperannuationNumber = "789", TaxFileNumber = "456" },
InsuranceEmployment = new InsuranceEmployment { EmploymentNumber = "678", TaxNumber = "456" },
InsuranceType = InsuranceType.Employment,
BankingInfos = new List<BankingInfo1>()
{
new BankingInfo1
{
AccountName = "ABC",
AccountNo = "1"
},
new BankingInfo1
{
AccountName = "XYZ",
AccountNo = "2"
}
}
};
//Mapping source to new destination
var destination = mapper.Map(source);
Console.WriteLine("destination.IDNumber == source.ConsumerID : " + (destination.IDNumber == source.ConsumerID).ToString());
Console.WriteLine("destination.Name == source.Name : " + (destination.Name == source.Name).ToString());
Console.WriteLine("destination.TotalPurchases == source.AvgNoOfPurchasesPerMonth * response1.PeriodInMonths : " + (destination.TotalPurchases == source.AvgNoOfPurchasesPerMonth * source.PeriodInMonths).ToString());
Console.WriteLine("destination.Details.DateOfBirth == source.Details.DOB : " + (destination.Details.DateOfBirth == source.Details.DOB).ToString());
Console.WriteLine("destination.Details.IsHandicapped == source.Details.IsDisabled : " + (destination.Details.IsHandicapped == source.Details.IsDisabled).ToString());
Console.WriteLine("destination.InsuranceInfo.MembershipNo == source.InsuranceEmployment.EmploymentNumber : " + (destination.InsuranceInfo.MembershipNo == source.InsuranceEmployment.EmploymentNumber).ToString());
Console.WriteLine("source.PeriodInMonths : " + (source.PeriodInMonths == 12).ToString());
Console.WriteLine("destination.Period == \"Year\" : " + (destination.Period == "Year").ToString());
Console.WriteLine("source.InsuranceType == InsuranceType.Employment : " + (source.InsuranceType == InsuranceType.Employment).ToString());
Console.WriteLine("destination.InsuranceInfo.TaxNumber == source.InsuranceEmployment.TaxNumber : " + (destination.InsuranceInfo.TaxNumber == source.InsuranceEmployment.TaxNumber).ToString());
Console.WriteLine("destination.Fund.BankIdNo == source.MutualFund.BankIdNo : " + (destination.Fund.BankIdNo == source.MutualFund.BankIdNo).ToString());
Console.WriteLine("destination.Fund.FundId == source.MutualFund.FundId : " + (destination.Fund.FundId == source.MutualFund.FundId).ToString());
Console.WriteLine("destination.Fund.Name == source.MutualFund.Name : " + (destination.Fund.Name == source.MutualFund.Name).ToString());
Console.WriteLine("destination.Fund.Address.StreetNo == source.MutualFund.Address.StreetNo : " + (destination.Fund.Address.StreetNo == source.MutualFund.Address.StreetNo).ToString());
Console.WriteLine("destination.Fund.Address.State.Name == source.MutualFund.Address.State.Name : " + (destination.Fund.Address.State.Name == source.MutualFund.Address.State.Name).ToString());
Console.WriteLine("destination.Fund.Address.State.Abbr == source.MutualFund.Address.State.Abbr : " + (destination.Fund.Address.State.Abbr == source.MutualFund.Address.State.Abbr).ToString());
Console.WriteLine("destination.Fund.Address.Country.Name == source.MutualFund.Address.Country.Name : " + (destination.Fund.Address.Country.Name == source.MutualFund.Address.Country.Name).ToString());
Console.WriteLine("destination.Fund.Address.Country.Abbr == source.MutualFund.Address.Country.Abbr : " + (destination.Fund.Address.Country.Abbr == source.MutualFund.Address.Country.Abbr).ToString());
Console.WriteLine("destination.Fund.FundKeys[\"ABN\"] == source.MutualFund.FundKeys[\"ABN\"] : " + (destination.Fund.FundKeys["ABN"] == source.MutualFund.FundKeys["ABN"]).ToString());
Console.WriteLine("destination.Fund.FundKeys[\"TFN\"] == source.MutualFund.FundKeys[\"TFN\"] : " + (destination.Fund.FundKeys["TFN"] == source.MutualFund.FundKeys["TFN"]).ToString());
Console.WriteLine("destination.BankingInformation.Count == source.BankingInfos.Count : " + (destination.BankingInformation.Count == source.BankingInfos.Count).ToString());
Console.WriteLine("destination.BankingInformation[0].AccountName == source.BankingInfos[0].AccountName : " + (destination.BankingInformation[0].AccountName == source.BankingInfos[0].AccountName).ToString());
Console.WriteLine("destination.BankingInformation[0].AccountNumber == source.BankingInfos[0].AccountNo : " + (destination.BankingInformation[0].AccountNumber == source.BankingInfos[0].AccountNo).ToString());
Console.WriteLine("destination.BankingInformation[1].AccountName == source.BankingInfos[1].AccountName : " + (destination.BankingInformation[1].AccountName == source.BankingInfos[1].AccountName).ToString());
Console.WriteLine("destination.BankingInformation[1].AccountNumber == source.BankingInfos[1].AccountNo : " + (destination.BankingInformation[1].AccountNumber == source.BankingInfos[1].AccountNo).ToString());
}
}
#region Mappers
namespace MyMapper.UnitTests.Mappers
{
using MyMapper;
using MyMapper.Converters;
using MyMapper.UnitTests.Entities;
using System.Threading.Tasks;
public interface IResponseMapper
{
Response3 Map(Response1 response1);
Task<Fund3> MapAsync(Fund1 fund1);
}
public class ResponseMapper : IResponseMapper
{
public ResponseMapper()
{
}
public Fund3 Map(Fund1 fund1)
{
//Both Classes (Fund1 & Fund3) have properties by the same name
return Mapper<Fund1, Fund3>.Exec<EntityConverter<Fund1, Fund3>>(fund1);
}
public async Task<Fund3> MapAsync(Fund1 fund1)
{
//Both Classes (Fund1 & Fund3) have properties by the same name
return await Mapper<Fund1, Fund3>.MapAsync(fund1).Exec();
}
public Details3 Map(Details1 details1)
{
return Mapper<Details1, Details3>.Map(details1)
.With(d1 => d1.DOB, (d3, dob) => d3.DateOfBirth = dob)
.With(d1 => d1.IsDisabled, (d3, disabled) => d3.IsHandicapped = disabled)
.Exec();
}
public async Task<Details3> MapAsync(Details1 details1)
{
return await Mapper<Details1, Details3>.MapAsync(details1)
.With(d1 => d1.DOB, (d3, dob) => d3.DateOfBirth = dob)
.With(d1 => d1.IsDisabled, (d3, disabled) => d3.IsHandicapped = disabled)
.Exec();
}
public BankingInfo3 Map(BankingInfo1 bankingInfo1)
{
return Mapper<BankingInfo1, BankingInfo3>.Map(bankingInfo1)
.With(bi1 => bi1.AccountNo, (bi3, accNo) => bi3.AccountNumber = accNo)
.Exec();
}
public InsuranceInfo Map(InsuranceMutualFund insuranceMutualFund)
{
return Mapper<InsuranceMutualFund, InsuranceInfo>.Map(insuranceMutualFund)
.With(imf => imf.MutualFundNumber, (insuranceInfo, mutualFundNumber) => insuranceInfo.MembershipNo = mutualFundNumber)
.With(imf => imf.TaxNo, (insuranceInfo, taxNo) => insuranceInfo.TaxNumber = taxNo)
.Exec();
}
public InsuranceInfo Map(InsuranceSuperannuation insuranceSuperannuation)
{
return Mapper<InsuranceSuperannuation, InsuranceInfo>.Map(insuranceSuperannuation)
.With(imf => imf.SuperannuationNumber, (insuranceInfo, mutualFundNumber) => insuranceInfo.MembershipNo = mutualFundNumber)
.With(imf => imf.TaxFileNumber, (insuranceInfo, taxFileNo) => insuranceInfo.TaxNumber = taxFileNo)
.Exec();
}
public InsuranceInfo Map(InsuranceEmployment insuranceEmployment)
{
return Mapper<InsuranceEmployment, InsuranceInfo>.Map(insuranceEmployment)
.With(ie => ie.EmploymentNumber, (insuranceInfo, employmentNumber) => insuranceInfo.MembershipNo = employmentNumber)
.With(imf => imf.TaxNumber, (insuranceInfo, taxNumber) => insuranceInfo.TaxNumber = taxNumber)
.Exec();
}
public Response3 Map(Response1 response1)
{
return Mapper<Response1, Response3>.Map(response1)
.With(r1 => r1.ConsumerID, (r3, consumerId) => r3.IDNumber = consumerId)
//Calculated field
.With(r1 => r1.AvgNoOfPurchasesPerMonth * r1.PeriodInMonths, (r3, total) => r3.TotalPurchases = total)
//Using Switch
.Switch(r1 => r1.PeriodInMonths)
.Case(periodInMonths => periodInMonths > 0 && periodInMonths <= 3, (r3, periodInMonths) => r3.Period = "Quarter")
.Case(periodInMonths => periodInMonths > 3 && periodInMonths <= 6, (r3, periodInMonths) => r3.Period = "Half")
.Case(periodInMonths => periodInMonths > 6 && periodInMonths <= 9, (r3, periodInMonths) => r3.Period = "Three Quarter")
.Case(periodInMonths => periodInMonths > 9 && periodInMonths <= 12, (r3, periodInMonths) => r3.Period = "Year")
.Else((r3, periodInMonths) => r3.Period = "Unknown")
.End()
//Switch mapping with case mappings
.Switch(r1 => r1.InsuranceType)
.CaseMap(insuranceType => insuranceType == InsuranceType.MutualFund,
//Mapping source InsuranceMutualFund to destination InsuranceInfo using Map
//You can debug using using Debugger.Break()
mapper => { Debugger.Break(); mapper.With(r1 => r1.InsuranceMutualFund, (r3, insuranceMutualFund) => r3.InsuranceInfo = insuranceMutualFund, Map); }
)
.CaseMap(insuranceType => insuranceType == InsuranceType.Superannuation,
//Mapping source InsuranceSuperannuation to destination InsuranceInfo using Map
mapper => mapper.With(r1 => r1.InsuranceSuperannuation, (r3, insuranceSuperannuation) => r3.InsuranceInfo = insuranceSuperannuation, Map)
)
//Mapping source InsuranceEmployment to destination InsuranceInfo using Map by default
.ElseMap(mapper => mapper.With(r1 => r1.InsuranceEmployment, (r3, insuranceEmployment) => r3.InsuranceInfo = insuranceEmployment, Map))
.End()
//Mapping List
.With(r1 => r1.BankingInfos, (r3, bankingInfos) => r3.BankingInformation = bankingInfos, Map)
//Using another map - When Details1 is not null then map Details1 to Details3 using another map
.When(r1 => r1.Details != null, mapper => mapper.With(r1 => r1.Details, (r3, details3) => r3.Details = details3, Map))
//Using another map - When Fund1 is not null then map Fund1 to Fund3 using another map
.When(r1 => r1.MutualFund != null, mapper => mapper.With(r1 => r1.MutualFund, (r3, fund3) => r3.Fund = fund3, Map))
.Exec();
}
}
}
#endregion
#region Entities
namespace MyMapper.UnitTests.Entities
{
#region Response1
public class Details1
{
public DateTime DOB { get; set; }
public bool IsDisabled { get; set; }
}
public class Address1
{
public string StreetNo { get; set; }
public State1 State { get; set; }
public Country1 Country { get; set; }
}
public class State1
{
public string Name { get; set; }
public string Abbr { get; set; }
}
public class Country1
{
public string Name { get; set; }
public string Abbr { get; set; }
}
public class Fund1
{
public int? BankIdNo { get; set; }
public string Name { get; set; }
public int FundId { get; set; }
public Address1 Address { get; set; }
public Dictionary<string, string> FundKeys { get; set; }
}
public class BankingInfo1
{
public string AccountNo { get; set; }
//Automapped
public string AccountName { get; set; }
}
public class InsuranceMutualFund
{
public string MutualFundNumber { get; set; }
public string TaxNo { get; set; }
}
public class InsuranceSuperannuation
{
public string SuperannuationNumber { get; set; }
public string TaxFileNumber { get; set; }
}
public class InsuranceEmployment
{
public string EmploymentNumber { get; set; }
public string TaxNumber { get; set; }
}
public enum InsuranceType
{
MutualFund = 0,
Superannuation = 1,
Employment
}
public class Response1
{
public int ConsumerID { get; set; }
public string Name { get; set; }
public int AvgNoOfPurchasesPerMonth { get; set; }
public int PeriodInMonths { get; set; }
public Details1 Details { get; set; }
public Fund1 MutualFund { get; set; }
//List
public IList<BankingInfo1> BankingInfos { get; set; }
public InsuranceMutualFund InsuranceMutualFund { get; set; }
public InsuranceSuperannuation InsuranceSuperannuation { get; set; }
public InsuranceEmployment InsuranceEmployment { get; set; }
public InsuranceType InsuranceType { get; set; }
}
#endregion
#region Response3
public class Details3
{
public DateTime DateOfBirth { get; set; }
public bool IsHandicapped { get; set; }
}
public class Address3
{
public string StreetNo { get; set; }
public State3 State { get; set; }
public Country3 Country { get; set; }
}
public class State3
{
public string Name { get; set; }
public string Abbr { get; set; }
}
public class Country3
{
public string Name { get; set; }
public string Abbr { get; set; }
}
public class Fund3
{
public int? BankIdNo { get; set; }
public string Name { get; set; }
public int FundId { get; set; }
public Address3 Address { get; set; }
public Dictionary<string, string> FundKeys { get; set; }
}
public class BankingInfo3
{
public string AccountNumber { get; set; }
//Automapped
public string AccountName { get; set; }
}
public class InsuranceInfo
{
public string MembershipNo { get; set; }
public string TaxNumber { get; set; }
}
public class Response3
{
public int IDNumber { get; set; }
public string Name { get; set; }
//Calculated Field
public int TotalPurchases { get; set; }
public Details3 Details { get; set; }
//Automapped
public Fund3 Fund { get; set; }
//List
public IList<BankingInfo3> BankingInformation { get; set; }
public string Period { get; set; }
public InsuranceInfo InsuranceInfo { get; set; }
//Mapping to an existing destination object
public string Existing { get; set; }
}
#endregion
}
#endregion