-1

I have problems with IQueryable. What I want to do is write library that allows access to database. But first - in my database there are 3 tables - drivers, cars, tracks. And now how to make a class (or classes) that implements IQueryable and allows lazy loading. I don't want to access like get all rows to list and that filter. Should I write 3 model classes and implement IQueryable within each class or write 3 model classes and another class (classes?) implementing IQueryable? I was looking for some tutorials but almost every site is telling about differences between IQueryable and IEnumerable. I am a bit confused right now. Could anyone give me some guide how to implement this interface correctly?

Thank in advance to any answer.

metal_man
  • 580
  • 1
  • 9
  • 22
  • How are you accessing the DB? – Yuval Itzchakov Apr 09 '14 at 18:30
  • IQueryable is a collection class (which extends IEnumerable). Your classes don't implement it, they use that for various collections in your class that you want to be loaded lazily. – Corey Adler Apr 09 '14 at 18:31
  • I think I could access through SqlConnection and SqlCommands. But I don't know the best way of accessing data. – metal_man Apr 09 '14 at 18:32
  • Don't write your own data access layer/ORM. NHibernate and the like are your friend – Jedediah Apr 09 '14 at 18:48
  • @IronMan84 so how I should implement getting data from one table? By LINQ to SQL? I have a little problem of imagine how it should be implemented... – metal_man Apr 09 '14 at 18:49
  • @Jedediah i know that there are EntityFramework or nHibernate. But I want to be able to write something on my own :) – metal_man Apr 09 '14 at 18:50

1 Answers1

1

Writing your own library that provides a IQueryable that does not rely on linq to objects (which is just wrapping a IEnumerable inside IQueryable) is very difficult and not simple at all. You will not find any simple tutorials on this subject as it is too complex of a topic to cover in a simple single tutorial.

If you still want to do it and still want a "tutorial", the first place is to start here: "LINQ: Building an IQueryable Provider - Part I" (there are 17 parts to the series). Once you have the ability to parse out IQueryable requests you then can connect that to whatever database interface you want to use.

I highly recommend that you let someone else do the work for you and just use a pre-made ORM library instead.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • First of all thanks for the link. And second it seems to be more complicated than i thought. But still worth trying to get through :) For my own I still want to get it to work. Anyway than for your answer. – metal_man Apr 09 '14 at 19:34