-
-
Save mythz/dc7d2c2ba523ba657c219f4d40a1591c to your computer and use it in GitHub Desktop.
Auto Mapping Converter Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using ServiceStack; | |
using ServiceStack.Text; | |
using System.Collections.Generic; | |
AutoMapping.RegisterConverter((CarRevisionDbo from) => { | |
var to = from.ConvertTo<CarRevisionDto>(skipConverters:true); | |
to.PopulateWith(from.Car); | |
return to; | |
}); | |
public class Car | |
{ | |
public string Model {get; set;} | |
public int Year {get; set;} | |
public int MaxSpeed {get; set;} | |
} | |
public class TechRevision | |
{ | |
public bool Ok {get; set;} | |
public string RevisionDate {get; set;} | |
} | |
public class CarRevisionDbo | |
{ | |
public Car Car {get; set;} | |
public List<TechRevision> TechRevisions {get; set;} | |
} | |
public class CarRevisionDto | |
{ | |
public string Model {get; set;} | |
public int Year {get; set;} | |
public int MaxSpeed {get; set;} | |
public List<TechRevision> TechRevisions {get; set;} | |
} | |
var carRevisions = new [] | |
{ | |
new CarRevisionDbo | |
{ | |
Car = new Car { Model = "BMW", Year = 2000, MaxSpeed = 190}, | |
TechRevisions = new List<TechRevision> | |
{ | |
new TechRevision {Ok = true, RevisionDate = "2004"}, | |
new TechRevision {Ok = true, RevisionDate = "2008"} | |
} | |
}, | |
new CarRevisionDbo | |
{ | |
Car = new Car { Model = "VW", Year = 2002, MaxSpeed = 190}, | |
TechRevisions = new List<TechRevision> | |
{ | |
new TechRevision {Ok = true, RevisionDate = "2006"}, | |
new TechRevision {Ok = true, RevisionDate = "2010"} | |
} | |
} | |
}; | |
var converted = carRevisions.ConvertTo<List<CarRevisionDto>>(); | |
converted.PrintDump(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="System.Memory" version="4.5.2" targetFramework="net45" /> | |
<package id="ServiceStack.Text" version="5.5.0" targetFramework="net45" /> | |
<package id="ServiceStack.Client" version="5.5.0" targetFramework="net45" /> | |
<package id="ServiceStack.Interfaces" version="5.5.0" targetFramework="net45" /> | |
</packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment