When using Linq-To-SQL or Entity Framework, the DataContext and generated entities provide IQueryable interfaces for deferred execution. It lets me write code like this:
public class RPO
{
DataContext dc;
public RPO(){ dc = new DataContext(); }
public IQueryable<Data> ReadData()
{
return dc.Data;
}
}
public class Svc
{
RPO repository;
public Svc() { repository = new RPO(): }
public IQueryable<Data> ReadActiveData()
{
return repository.ReadData().Where(d => d.IsActive.Equals(true));
}
public IQueryable<Data> ReadArchiveData()
{
return repository.ReadData().Where(d => d.IsArchived.Equals(true));
}
}
This model falls appart if in the class Svc I return DataModel instead of Data — how can I keep IQueryable<T> as far down the chain as possible?
Your SVC layer should never expose IQueryable. What happens then is, it is actually your service consumer who gets to execute your query which is a bad pattern. So service should always expose
data which is sufficient for the service-user to work ( display) with.
Preferably a IList or a IEnumarable.