views:

30

answers:

1

Given i have a class like so in my Data Layer

public class GenericRepository<TEntity> where TEntity : class 
{

   public MyDataContext DataContext {get;set;}

   [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)]
   public IQueryable<TEntity> SelectAll()
   {
      return DataContext.GetTable<TEntity>();
   }
}

I would be able to query a table in my database like so from a higher layer

using (GenericRepositry<MyTable> mytable = new GenericRepositry<MyTable>())
{
   var myresult = from m in mytable.SelectAll()
                  where m.IsActive
                  select m;
}

is this considerably slower than using the usual code in my Data Layer

using (MyDataContext ctx = new MyDataContext())
{
   var myresult = from m in ctx.MyTable
                  where m.IsActive
                  select m;
}

Eliminating the need to write simple single table selects in the Data layer saves a lot of time, but will i regret it?

Edit: @ Skeet I have actually implemented this approach in a fairly large WCF/Silverlight LOB project, and it seems our servers CPU's are struggling to keep up. The extra work of creating/destroying extra objects couldn't possibly be attributed to the rise in cpu usage over projects using the usual way?

+1  A: 

You haven't shown where your "generic repository" is getting its context from - I assume it's creating a new one, and proxying the dispose call?

If so, it should basically be the same - it's been a while since I looked into the difference between GetTable<T>() and using the property, but I wouldn't be surprised if the property just called GetTable<T> itself. Other than that, there's no real difference.

The important point is that you're still using IQueryable<T> in both cases, so the query will still be translated into SQL - if your SelectAll method returned IEnumerable<T> instead, it would be disastrous.

Jon Skeet
@Jon Skeet: can you please throw lights on the last line?
Veer
@Veer: If the method returned `IEnumerable<T>`, the query expression would be using LINQ to Objects, doing filtering etc in process instead of in SQL.
Jon Skeet