Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For doing conversions between a model class and a DTO, my preference is to do one of four things:</p> <p>a. use an implicit conversion operator (especially when dealing json-to-dotnet transitions).</p> <pre><code>public class Car { public Color Color {get; set;} public int NumberOfDoors {get; set;} } public class CarJson { public string color {get; set;} public string numberOfDoors { get; set; } public static implicit operator Car(CarJson json) { return new Car { Color = (Color) Enum.Parse(typeof(Color), json.color), NumberOfDoors = Convert.ToInt32(json.numberOfDoors) }; } } </code></pre> <p>and then usage is</p> <pre><code> Car car = Json.Decode&lt;CarJson&gt;(inputString) </code></pre> <p>or more simply</p> <pre><code> var carJson = new CarJson {color = "red", numberOfDoors = "2"}; Car car = carJson; </code></pre> <p>voila, instant conversion :)</p> <p><a href="http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx</a></p> <p>b. Use linq projection to change the shape of the data</p> <pre><code>IQueryable&lt;Car&gt; cars = CarRepository.GetCars(); cars.Select( car =&gt; new { numberOfDoors = car.NumberOfDoors.ToString(), color = car.Color.ToString() } ); </code></pre> <p>c. Use some combination of the two </p> <p>d. Define an extension method (that could also be used in the linq projection)</p> <pre><code>public static class ConversionExtensions { public static CarJson ToCarJson(this Car car) { return new CarJson {...}; } } CarRepository.GetCars().Select(car =&gt; car.ToCarJson()); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload