How to brings the Entity Framework Include extention method to a Generic IQueryable<TSource> -
here's thing.
i have interface, , put include
extension method, belongs entityframework library, irepository
layer wich dont needs knows entityframework.
public interface irepository<tentity> { iqueryable<tentity> entities { get; } tentity getbyid(long id); tentity insert(tentity entity); void update(tentity entity); void delete(tentity entity); void delete(long id); }
so have extension method:
public static class includeextension { static iqueryable<tentity> include<tentity>(this iqueryable<tentity> query, string path) { throw new notimplementedexception(); } }
but don't know how implement in layer, , send entityframework (or whatever implement irepository) deal with.
i need same interface extension method.
any light?
this question bit old, here 2 ef-independent solutions if or else still looking:
1. reflection-based solution
this solution .net framework falls if iqueryable
not cast dbquery
or objectquery
. skip these casts (and efficiency provides) , you've decoupled solution entity framework.
public static class includeextension { private static t queryinclude<t>(t query, string path) { methodinfo includemethod = query.gettype().getmethod("include", new type[] { typeof(string) }); if ((includemethod != null) && typeof(t).isassignablefrom(includemethod.returntype)) { return (t)includemethod.invoke(query, new object[] { path }); } return query; } public static iqueryable<t> include<t>(this iqueryable<t> query, string path) t : class { return queryinclude(query, path); } // add other include overloads. }
2. dyanmics-based solution
here queryinclude<t>
method uses dynamic
type avoid reflection.
public static class includeextension { private static t queryinclude<t>(t query, string path) { dynamic querytwithincludemethod = query dynamic; try { return (t)querytwithincludemethod.include(path); } catch (runtimebinderexception) { return query; } } public static iqueryable<t> include<t>(this iqueryable<t> query, string path) t : class { return queryinclude(query, path); } // add other include overloads. }
Comments
Post a Comment