v4. add a predicateExpression to the Odata query

Posts   
 
    
raist
User
Posts: 114
Joined: 19-Apr-2010
# Posted on: 22-Aug-2013 17:40:53   

We have tried the Odata services with VS 2010 and llblgen 4.0: awesome!! But we are trying to get more control on the results, injecting a dynamic filter (predicateExpression) based on the authenticated userId. Is it possible? How and where can we plug it? Thanks in advance.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Aug-2013 07:12:28   
  • Wouldn't be easier to use WebGet methods for that? (or even Controllers)

  • How are you authenticating the user, and how would you know how to create the dynamic filter and inject that on the IQueryable?

David Elizondo | LLBLGen Support Team
raist
User
Posts: 114
Joined: 19-Apr-2010
# Posted on: 23-Aug-2013 08:10:56   

We can inject the auth realm directly in the httpHeaders (eg. basic auth or session cookie with previous auth). Trying right now. Once we have the realm we can get the userId and the asociated authorization filters for each entity (ex. Product.ProviderId == userId.CompanyId). We where considering custom WebGet methods (or even WebAPI) too, but: - 1. We 'd lose all the OData clients already developed (JayData.js, Data.Services.Client...) - 2. We'd lose the expressiveness of OData and go to reinvent the API, as in Matt Jcowan solution with ServiceStack (really great, indeed). http://www.mattjcowan.com/funcoding/2013/03/10/rest-api-with-llblgen-and-servicestack/ We could intercept the query at the DAL level, but somehow it doesn't feel right.

TIA, Jose

raist
User
Posts: 114
Joined: 19-Apr-2010
# Posted on: 23-Aug-2013 09:58:45   

It was easier as thought: insert QueryInterceptors in the Service class


[QueryInterceptor("Product")]
public Expression<Func<ProductEntity, bool>> OnReadProducts()
{
    return o => true; // context.Product.Where(p => p.CategoryId == 2); returns 12 products
    return o => o.Discontinued == false; // same query returns 11 products
}

Thanks

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 23-Aug-2013 10:55:45   

indeed, that's what they're there for simple_smile

Frans Bouma | Lead developer LLBLGen Pro
raist
User
Posts: 114
Joined: 19-Apr-2010
# Posted on: 23-Aug-2013 10:59:45   

It simply worked fine!!! (As usual) I share the VS 2010 solution with - Llblgen v.4.0 adapter DAL projects (2) - Northwind service - Console client

NOTE: 1. Change the connection string in web.config 2. Install the nuget Microsoft.Data.Services packages on both the client and service projects

PM>Install-Package Microsoft.Data.Services -version 5.3.0.0

Interesting parts: In NorthwindService.svc.cs: - Hook to CustomAuthenticationProvider.Authenticate (from http://blogs.msdn.com/b/astoriateam/archive/2010/07/19/odata-and-authentication-part-4-server-side-hooks.aspx) - Little QueryInterceptor OnReadProducts

In ClienteOData.Program: - Two changes to the generated NorthwindService constructor - Added constructor DataServiceProtocolVersion parameter (default V2). To support OData V3 - Added hook to add the authentication header (also from http://blogs.msdn.com/b/astoriateam/archive/2010/05/24/odata-and-authentication-part-3-clientside-hooks.aspx)

this.SendingRequest += OnSendingRequest;
  • UpdateObject call

Regards,

Jose

Attachments
Filename File size Added on Approval
NorthwindOData.zip 178,086 23-Aug-2013 11:07.52 Approved
Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 23-Aug-2013 11:42:32   

Thanks for the feedback. Surely others taking the same path would appreciate it.