Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping using Expression Trees? Is it possible to implement this?

Tags:

c#

.net-4.0

public static MapFrom Map(this IDataReader idr, params string[] columns)
{
     return new MapFrom { Columns = columns };
}
public static IEnumerable<TEntity> To<TEntity>(this MapFrom from, Func<TEntity, object> map)
{
     // logic here.
}    

public IEnumerable<FooEntity> Execute()
{
    using (DbCommand cmd = db.GetStoredProcCommand("GetListOfFoo"))
    {
        using (IDataReader idr = db.ExecuteReader(cmd))
        {
            return idr.Map("FooID", "FooOfPooAmount", "AnotherFooColumn", "BarID")
              .To<FooEntity>(x => new object[]{
                          x.FooID, x.FooOfPooAmount, x.AnotherFoo, x.BarID
            });
         }
     }
}

I wish to take a data reader and create a simple mapper that is easy to use and is also performant. I haven't used expression trees much, but I feel like they may be needed. What I want to do is inspect the relationship of x to it's array (The lambda expression), and then use this relationship to automatically map values appropriately and create a new List of Foo Entitiies.

MapFrom is just a container to carry information and allow fluent extension methods.

The point of these acrobatics is to have a simple way to specify relationships. I realize I could easily do something like in a different form, but I wanted to be able to list columns "fluently" [1] (as in typing column name as a string followed by comma then next column name) and then list the object properties in sequential order and infer the mapping from this. It it possible to implement the To method above? Are Expression Trees appropriate here? I figure I can easily enough infer type casting for the data reader fields from the FooEntity property types.

[1] In this context I do not mean fluent as in "Fluent API"

like image 812
Joshua Enfield Avatar asked Jan 20 '26 12:01

Joshua Enfield


1 Answers

I wrote here your MapFrom class, than you have to write the code to copy the value from columns to the property of an instance of FooEntity and use the yield return statement to return a collection of your entity, so add another method to the MapFrom class:

public class MapFrom
{
    private readonly IDictionary<string, string> columnMapDictionary =
        new Dictionary<string, string>();

    public MapFrom(string[] columns)
    {
        foreach (var column in columns)
        {
            columnMapDictionary.Add(column, null);
        }
    }

    public void To<T>(params Expression<Func<T, object>>[] properties)
    {
        var index = 0;
        foreach (var columnMap in columnMapDictionary.ToDictionary(k => k.Key))
        {
            var member = (properties[index++].Body as MemberExpression);
            var propertyName = member.Member.Name;
            columnMapDictionary[columnMap.Key] = propertyName;  
        }
    }
}

Use it as:

var mapper = new MapFrom("Name", "Surname");
mapper.To<FooEntity>(p => p.FirstName, p => p.LastName);
like image 94
Matteo Migliore Avatar answered Jan 22 '26 04:01

Matteo Migliore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!