score:2

Accepted answer

i've managed to do it, source code below. it runs somewhat faster than automapper (not sure if my automapper configuration is the fastest for this task), benchmark is not bulletproof, but on my machine to map 5 million rows took 20.16 seconds using my written mapper and 39.90 using automapper, although it seems that automapper uses less memory for this task (haven't measured it, but with 10 million rows automapper gives result and my mapper fails with outofmemory).

public class mappingparameter<tsource>
{
    private readonly delegate setter;

    private mappingparameter(delegate compiledsetter)
    {
        setter = compiledsetter;
    }

    public static mappingparameter<tsource> create<tresult>(expression<func<tsource, tresult>> expression)
    {
        var newvalue = expression.parameter(expression.body.type);
        var body = expression.assign(expression.body, newvalue);
        var assign = expression.lambda(body, expression.parameters[0], newvalue);

        var compiledsetter = assign.compile();

        return new mappingparameter<tsource>(compiledsetter);
    }

    public void assign(tsource instance, object value)
    {
        object convertedvalue;
        if (!setter.method.returntype.isassignablefrom(typeof(string)))
        {
            convertedvalue = convert.changetype(value, setter.method.returntype);
        }
        else
        {
            convertedvalue = value;
        }

        setter.dynamicinvoke(instance, convertedvalue);
    }
}

public class datarowmappingconfiguration<tsource>
{
    private readonly dictionary<string, mappingparameter<tsource>> mappings =
        new dictionary<string, mappingparameter<tsource>>();

    public datarowmappingconfiguration<tsource> add<tresult>(string columnname,
                                                             expression<func<tsource, tresult>> expression)
    {
        mappings.add(columnname, mappingparameter<tsource>.create(expression));
        return this;
    }

    public dictionary<string, mappingparameter<tsource>> mappings
    {
        get
        {
            return mappings;
        }
    }
}

public class datarowmapper<tsource>
{
    private readonly datarowmappingconfiguration<tsource> configuration;

    public datarowmapper(datarowmappingconfiguration<tsource> configuration)
    {
        this.configuration = configuration;
    }

    public ienumerable<tsource> map(datatable table)
    {
        var list = new list<tsource>(table.rows.count);

        foreach (datarow datarow in table.rows)
        {
            var obj = (tsource)activator.createinstance(typeof(tsource));

            foreach (var mapping in configuration.mappings)
            {
                mapping.value.assign(obj, datarow[mapping.key]);
            }

            list.add(obj);
        }

        return list;
    }
}

public class testclass
{
    public string name { get; set; }
    public int age { get; set; }
}

[testfixture]
public class datarowmappingtests
{      
    [test]
    public void shouldmappropertiesusingownmapper()
    {            
        var mappingconfiguration = new datarowmappingconfiguration<testclass>()
            .add("firstname", x => x.name)
            .add("age", x => x.age);

        var mapper = new datarowmapper<testclass>(mappingconfiguration);                      

        var datatable = new datatable();
        datatable.columns.add("firstname");
        datatable.columns.add("age");

        for (int i = 0; i < 5000000; i++)
        {
            var row = datatable.newrow();
            row["firstname"] = "john";
            row["age"] = 15;

            datatable.rows.add(row);                
        }

        var start = datetime.now;

        var result = mapper.map(datatable).tolist();

        console.writeline((datetime.now - start).totalseconds);

        assert.areequal("john", result.first().name);
        assert.areequal(15, result.first().age);
    }

    [test]
    public void shouldmappropertyusingautomapper()
    {
        mapper.createmap<datarow, testclass>()
            .formember(x => x.name, x => x.mapfrom(y => y["firstname"]))
            .formember(x => x.age, x => x.mapfrom(y => y["age"]));

        var datatable = new datatable();
        datatable.columns.add("firstname");
        datatable.columns.add("age");

        for (int i = 0; i < 5000000; i++)
        {
            var row = datatable.newrow();
            row["firstname"] = "john";
            row["age"] = 15;

            datatable.rows.add(row);
        }

        var start = datetime.now;

        var result = datatable.rows.oftype<datarow>().select(mapper.map<datarow, testclass>).tolist();         

        console.writeline((datetime.now - start).totalseconds);

        assert.areequal("john", result.first().name);
        assert.areequal(15, result.first().age);
    }
}

score:1

something like thid maybe :

public class mapping<tsource>
{
    public void assign<tresult>(tsource instance, tresult value)
    {
        var property = typeof(tsource).getproperties().firstordefault(p => p.propertytype == typeof(tresult)));
        if (property != null)
        {
            property.setvalue(instance, value, new object[0]);
        }

    }
}

but your object need to have one property of each type for this to be accurate

we could even make it more generic, but more dangerous :

    public void assign<tresult>(tsource instance, tresult value)
    {
        var property = typeof(tsource).getproperties().firstordefault(p => p.propertytype.isassignablefrom(typeof(tresult)));
        if (property != null)
        {
            property.setvalue(instance, value, new object[0]);
        }

    }

(this won't work if you have 2 properties inheriting from the same base class)...


Related Query

More Query from same tag