using System.Collections;
using System.Collections.Generic;
public static void Main()
var data = new Dictionary<string, object>
{ "Manufacturer", "Opel" },
var mapping = new Mapping
ColumnMappings = new List<ColumnMapping>
new ColumnMapping { Source = "Brand", Destination = "Car.Name" },
new ColumnMapping { Source = "Type", Destination = "Car.Type" },
new ColumnMapping { Source = "EngineId", Destination = "Car.Engine.Id" },
new ColumnMapping { Source = "Manufacturer", Destination = "Car.Engine.Manufacturer" },
new ColumnMapping { Source = "Power (hp)", Destination = "Car.Engine.Power", DestinationType = typeof(double) }
var valid = new Validator().Validate(data, mapping);
Console.WriteLine(valid);
var car = CarFactory.Create(data, mapping);
Console.WriteLine(car.Name + " " + car.Type);
public static class CarFactory {
public static Car Create(Dictionary<string, object> data, Mapping mapping) {
var carType = typeof(Car);
var car = new Car { Engine = new Engine() };
foreach (var pair in data) {
Console.WriteLine(pair.Key);
var fieldName = mapping.ColumnMappings
.Select(c => new { Source = c.Source, Destination = c.Destination ?? c.Source})
.FirstOrDefault(c => c.Source == pair.Key);
if(fieldName != null && !fieldName.Destination.Contains('.')) {
var field = carType.GetField(fieldName.Destination);
Console.WriteLine("{0}: {1}", field.Name, pair.Value);
field.SetValue(car, pair.Value);
public string Manufacturer;
public Dictionary<string,string> OtherAttributes;
public bool Validate(Dictionary<string, object> data, Mapping mapping)
foreach (var pair in data)
if (Invalid(pair, mapping.ColumnMappings.FirstOrDefault(c => c.Source == pair.Key)))
private bool Invalid(KeyValuePair<string, object> pair, ColumnMapping columnMapping)
if(columnMapping == null) return false;
Console.WriteLine(columnMapping.DestinationType.Name);
switch (columnMapping.DestinationType.Name) {
return pair.Value.GetType() != typeof(Double);
public IEnumerable<ColumnMapping> ColumnMappings
public class ColumnMapping
public string Destination;
public Type DestinationType = typeof(string);