Monday, August 22, 2011

What should come back from the Repositories? What shape?

Repository methods should return IQueryable and not IList as the former is, well, queryable. One may reach into it and filter results on the other side of the method call before a trip to the database is every made.

2 comments:

  1. I am wondering how this affects a view model? Would you define a IQueryable property on your view model or define a IList ?

    ReplyDelete
  2. Hi Rippo.
    We are getting data back from interfaces in IQueryable form and then after "molding the clay" we are casting IQueryable to IList. However, a list of a domain object is NOT what is being passed to a view as a model. We are populating the Models folder of our MVC project with models that only have to do with UI concerns and do not go back and forth to the database. Typically Foo would be wrapped in FooModel (as a list) which would have some other properties on it to aid in building a screen, for example as of the potential values in a dropdown list and not just the value that sits at a parameter on Foo.

    (Note: http://tom-jaeschke.blogspot.com/2011/08/chain-together-filters-ontop-of.html touches on what we are doing with IQueryable and http://tom-jaeschke.blogspot.com/2011/08/base-class-for-repositories.html gives some other code that the first post leaves out by oversite)

    ReplyDelete