Sunday, December 15, 2013

IQueryable drawbacks (Repository pattern, OData)

Developers, developers, developers have new crazy idea "to publish data in IQueryable interface".

I For example, to provide IQueryable with WCF Data Service

public class MyWcfDataService : DataService<MyEfModel>
   [WebGet(ResponseFormat = WebMessageFormat.Json)]
   public IQueryable<GetMyListEF> GetMyList()
       using (MyEfModel context = this.CurrentDataSource)
           return context.GetMyListEF().ToList().AsQueryable();
(example from one of the hundreds common posts in the internet)

At first, what if context.GetMyListEF() will return millions of rows? All of them will be send to the client. Is client expect such thing?

Second point will be a bad nature of the IQueryable in real life. Currently nobody can provide full implementation of this interface. Even Entity Framework with modern sql providers does not have ideal implementation of the interface. There lots of exceptions of impossibility to convert to T-SQL code. Or with success we have bad performance (the reason choose Micro ORM dapper).

If some provide IQueryable<T> interface then clients expect full implementation of it. Not partial, FULL!

II Next example is widely using our lovely interface in Repository pattern, usually it looks like

public class MyRepository<T> : IRepository<T>
   public IQueryable<T> GetModels()
       // implementation

- What is the purpose of abstracting Repository?
- To make it possible exchange in future with another implementation
- Can you make NoSql storage or File storage using in future instead of MS SQL?
- No! Querable providers are very complex to create for such storages.

III Same problem with OData, that is pushing by Microsoft.
With OData users can query data by passing parameters in URI, for example,$top=2&$orderby=name

The idea is great, but, but, but
"Users like cheating only if it is a focus!"

If you take Sharepoint services with OData you will find the same problem. The service just cannot provide all possible ways of using it. Users will be restricted to the platform in their heads, instead of be lucky with interface features.

P.S. Better use what users expect to get from your services (IEnumerable<T> in most cases).

IQueryable is Tight Coupling by Mark Seemann

No comments:

Post a Comment