Request an RIA Services DomainService with an EF Projection that invokes a method and still allows you to configure paging / sorting

Here is an interesting problem that I hope someone can lend a hand to.

I have a DomainService class with the following request:

[Query] public IEnumerable<BatchResult> GetBatchResults(int batchId) { return ObjectContext.BatchQueries .Include("BatchTCResults") .Include("BatchANFResults") .Where(x => x.BatchId == batchId) .Where(x => x.BatchTCResults.Any() || x.BatchANFResults.Any()) .ToArray() // enumerate because projection calls a method that EF will poop it pants on .Select(x => new BatchResult { BatchQueryId = x.Id, Route = x.Routing, Account = x.Account, Amount = x.Amount, CheckNumber = x.CheckNumber, Severity = BatchResult.DetermineOverallSeverity(x) }); } 

This works, but I really need the swap / sort information passed from the Silverlight client for use before the enum happens on the line calling .ToArray()

The client side uses the Silverlight DomainDataSource control.

How can i achieve this?

+4
source share
1 answer

The easiest way is to add the search / sort / filter parameters to the argument list of the method and add the appropriate LINQ query operators to your data context request in the service method. However, in this case, you will lose the IQueryable functions on the client side. I mean, client requests will be available only for the application server, but will not get into the database. And you need some layer between the DomainContext and the user interface: DomainDataSource unlikely to work.

Displaying query results as IQueryable using AsQueryable() will not help, because expression trees will be unconditionally compiled into code to execute your LINQ-to-Objects statements.

It is somewhat more difficult to use arguments, as in the first case, together with creating your own IQueryable shell over the client-side query object. This shell will extract parameters from query expressions and pass them to the service as method arguments. Well, I would try this if I had enough free time.

Another tricky way is a similar server side. It is possible to get an IQueryable object that has an expression tree passed from the client to the DomainService . You need to override the DomainService.Query method. Then you can extract the part of the query expression related to the search call / sort, save it in the field and then apply to the database query. However, the Query method is the only one for all methods in the specified DomainService . Therefore, you will probably end up with a large table method that decides what to do for each of the service request methods. Needless to say, the service will become very complex.

In conclusion, I highly recommend you the first option.

+3
source

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


All Articles