mardi 19 février 2019

Adapter for nongeneric delegate

I have a generic class:

public class ProviderAdapter<TDto, TModel>
{
    public ProviderAdapter(IQueryable source, Func<TDto, TModel> mapping)
    {
      ... //not important
    }
}

and extension method:

public static class QueryableExtensions
{
    public static IQueryable<TModel> Map<TDto, TModel>(
        this IQueryable<TDto> query, Func<TDto, TModel> mapping)
    {
      var providerAdapter = new ProviderAdapter<TDto, TModel>(query, mapping);
      var result = new QueryableAdapter<TModel>(providerAdapter);
      return result;
    }
}

Now I want to create another extension method for non-generic IQueryable:

public static class QueryableExtensions
{
    public static IQueryable<TModel> Map<TModel>(
        this IQueryable query, Func<dynamic, TModel> mapping)
    {
      //how to create ProviderAdapter instance having 
      //query.ElementType and mapping Func with dynamic?
      var providerAdapter = new ProviderAdapter<type from query.ElementType, TModel>(query, 
      new Func<type from query.ElementType, TModel> using mapping Func<dynamic,TModel>
    }
}

Is it possible? I am no sure about 'mapping' parameter type - I was planning to create new Func using reflection that invokes

Func<dynamic, TModel> 

but converts first parameter to query.ElementType. Maybe 'mapping' parameter should be of type

Expression<Func<dynamic, TModel>>





Aucun commentaire:

Enregistrer un commentaire