Creating a DbSet <T> from an expression to create a dynamic query service

I am trying to create a dynamic query service using the Entity Framework.

My approach is to somehow create an easy client to create DTO queries by creating IQueryable<T>or Expression, and then send this query \ expression over the network to the runtime service, which will translate IQueryable<T>\ Expressionto SQL, run it and return it IEnumerable<S>.

My thought was to use Enumerable.Empty<T>.AsQueryable()to create queries, but then, when I need to execute the queries, I need the ability to translate them into SQL - and how the Entity Framework can help me.

Entity Framework does this magic using classes that implement IQueryable<T>like DbSet<T>and DbQuery<T>.
I can create a client that will create IQueryable<T>'susing the Entity Framework dummy DbContext, so I can basically create queries and be able to create SQL from them on the client.

I tried to combine between the frameworks and use the Entity Framework to create SQL from mine IQueryable<T>'s, and then run it using Dapper, but it gets complicated when the attached navigation properties are involved, and I still want to be able to modify queries using custom ones Expression<Func<T,T>>'son my service.
Thus, making queries on the client and sending SQL to the service is not really the right solution for me.

DbSet<T> Expression? - "" a DbSet<T>?
, ?

→ pseduo , :

:

var query = QueryCreator
.Get<MyDto>()
.Include(d => d.MySonDto)
.Where(d => d.IsActive)
.OrderBy(d => d.MySonDto.Date)
.Take(10);

 var result = service.Query(query); // or `service.Query(query.Expression);`

- , SQL, .

Entity Framework , , IQueryable<T> Expression DbSet<T> ? (DbSet<T>.Parse(expression) :-))

+4
1

Givent, T ( ), Expression CreateQuery:

var context = new YourContext();
var data = context.Set<T>()
    .AsQueryable()
    .Provider.CreateQuery(yourReceivedExpressionOfTEntity)
    .ToList();
0

Source: https://habr.com/ru/post/1691927/


All Articles