none
Does Returning IQueryable<T> from a static method cause any problems? RRS feed

  • Question

  • Hi All,

    I wonder if Returning IQueryable<T> from a static method cause any potential problems, like memory leak.

    see the code below, thanks.

    internal

     

    static class Orders

    {

     

    internal static IQueryable<Order>

    GetOrders(int customerID)

    {

    NorthwindDataContext dc = new NorthwindDataContext();

    {

     

    return dc.Orders.Where(o =>o.CustomerID == customerID);

    }

    }

    }

    Tuesday, April 5, 2011 7:33 PM

Answers

  • It looks like you have an extra set of { brackets }.  If you are doing using(DataContext dc = new DataContext()){ return dc....   } then I would recommend returning a list, but since you are not desposing of the object yourself it should be fine.  The only possible issue would be the datacontext could eventually be in a disconnected state.  Additionally your limiting future joins and other operations of the IQueryable since you do not have an handle on the DataContext anymore.  The additional joins and other operations are really were you gain the performance.

    You would have to also create a new datacontext and do a disconnected updates / deletes whenever you make changes.

    The better question is why would you want your method to be static?  Personally, I like the BLL instance logic layer classes. 

    • Marked as answer by Jun_1111 Wednesday, April 6, 2011 2:36 PM
    Tuesday, April 5, 2011 8:13 PM

All replies

  • It looks like you have an extra set of { brackets }.  If you are doing using(DataContext dc = new DataContext()){ return dc....   } then I would recommend returning a list, but since you are not desposing of the object yourself it should be fine.  The only possible issue would be the datacontext could eventually be in a disconnected state.  Additionally your limiting future joins and other operations of the IQueryable since you do not have an handle on the DataContext anymore.  The additional joins and other operations are really were you gain the performance.

    You would have to also create a new datacontext and do a disconnected updates / deletes whenever you make changes.

    The better question is why would you want your method to be static?  Personally, I like the BLL instance logic layer classes. 

    • Marked as answer by Jun_1111 Wednesday, April 6, 2011 2:36 PM
    Tuesday, April 5, 2011 8:13 PM
  • Hi LitEnders,

    Thanks for the reply.   My purpose is to reuse the method in many place.  In case I need to save the changes,  I can get back the handle of the datacontext by reflection.

    I always have some troubles in seperating BLL and DAL. :(  For me, it is bit time consuming.  I need to code fast to meet the deadline.

    Thanks again. 

    Wednesday, April 6, 2011 2:35 PM
  • Yeah I know, Linq and the BLL hasn't really been greatly defined anywhere by Microsoft.  I view the DataContext as the DAL, then my BLL classes share a base type that allows them all to have common features.  Once you define your BLL Base class you should be able to reuse it in multiple projects, and hopefuly have a common way of laying out your layer.

    Wednesday, April 6, 2011 2:44 PM