Haribabu
Haribabu

Reputation: 41

ASP .NET Web API Queryable capabilities with deferred execution

Should my api method always tie up to the underlying datasource (datacontext of an ORM) to expose Queryable capabilities supporting oData with the benefit of Deferred Execution?

Snippet

public class ProductController : ApiController { 
    public IQueryable<Product> Get() { 
        var dbContext = new DBContext(); 
        return dbContext.Product.AsQueryable(); 
    } 
}
  1. If I have to tie the always then, how and where can I apply business validation if I have to ? Eg: I want to return on the Products which are not expired.
  2. Aren't we violating the seperation of concerns and doing tight coupling by exposing the data entities directly?

Upvotes: 1

Views: 517

Answers (1)

Aliostad
Aliostad

Reputation: 81680

You can have the rule applied as below:

return dbContext.Product.Where(p=> !p.IsExpired).AsQueryable(); 

With regard to your second question, it is up to you. If you need to expose your data, then you should. If you want to expose an alternate view of the data, you can create a ViewModel and expose that.

Upvotes: 1

Related Questions