Error using IQueryable<T> from IObjectScope.Extent<T> multiple times

6 posts, 0 answers
  1. Jassey Nguyen
    Jassey Nguyen avatar
    6 posts
    Member since:
    May 2008

    Posted 03 Dec 2009 Link to this post

    I have a class that in the contructor I initialize an instance of IQuerable<T> from the IObjectScope.Extent<T>() and I use the IQueryable<T> for my LINQ queries.

    public ConcreteRepository() 
            { 
                _context = DBFactory.GetContext<T>(); 
            } 
    (_db is my instance of IObjectScope)
    public static IQueryable<T> GetContext<T>() 
            { 
                if (instance == null
                { 
                    throw new ArgumentNullException("The DBFactory has not been initialized."); 
                } 
                return _db.Extent<T>(); 
            } 


    So during the lifetime of this class I was hoping to use the same IQueryable<T>, but that returns an error. " The last accepted query piece is not from the this query." the second time that the _context is used.

    public virtual IQueryable<T> GetAll() 
            { 
                var query = from data in _context//DBFactory.GetContext<T>() 
                            select data; 
                return query; 
            } 
    Is this expected?


    This leads to my second question which is my DBFactory class has a static IObjectScope variable that is used during the
    entire scope of my application. Is this ok to do or a bad idea?
  2. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 04 Dec 2009 Link to this post

    Hi Jassey Nguyen,

    Using a static scope is one of the practices we suggest our clients to avoid in their OpenAccess applications. That is a probable cause for your problems as well. I would suggest you having a look at the following two articles that discuss the best practices for managing the IObjectScope in Windows Forms and Web Site scenarios.

    All the best,
    Zoran
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Jassey Nguyen
    Jassey Nguyen avatar
    6 posts
    Member since:
    May 2008

    Posted 04 Dec 2009 Link to this post

    Hi Zoran,

    Thanks for your response.  Why is having a static IObjectScope not a good idea.  Is it holding connections open or what is the reason?  Just curious.

    The only question I have about your examples for handling the IObectScope is that I'm using the repository pattern and so have a Data class that I want to keep the IObjectScope contained within.  I don't want to expose it to my individual pages.  Is the best solution for that scenario then to simply implement IDisposable in my repository class and make sure that I dispose of the IObjectScope after each usage.

    Thanks,

    Jassey
  5. Zoran
    Admin
    Zoran avatar
    534 posts

    Posted 04 Dec 2009 Link to this post

    Hi Jassey Nguyen,

    The biggest issue for having a static object scope is that it is not thread safe. Regarding your second concern, yes you should dispose the object scope after each request, implementing IDisposable at repository level is one of the options for achieving this.

    Greetings,
    Zoran
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Jassey Nguyen
    Jassey Nguyen avatar
    6 posts
    Member since:
    May 2008

    Posted 04 Dec 2009 Link to this post

    So I was looking at the MVC examples using OpenAccess.  It looks ok at first, but the problem I see is that the several of the Helper classes (e.g.) Forum Facade take IObjectScope as an argument.  Do you have an example of how you would mock the IObjectScope.  I have the MockObjectScope project, but that doesn't seem to fit this scenario because it doesn't actually implement IObjectScope.

    This is why I was trying to use IQueryable at the beggining so that I could inject an IQueryable easily during testing of my Service layer.

    Thanks for your help again!
  7. Jordan
    Admin
    Jordan avatar
    547 posts

    Posted 07 Dec 2009 Link to this post

    Hi Jassey Nguyen,

    Unfortunately, mocking / implementing the whole IObjectScope interface will is not easy at all currently. So,  the way I see it you have two options:

    1. Implement a IObjectScope mock that implements just the methods that you are using. You could use the code from the MockObjectScope project.
    2. Get back to your previous idea about using IQueryable<T> instead of IObjectScope, but change it a bit like for example get a fresh IQueryable every time when you need to do a query.

    We will be glad to help if you have more questions.

    Sincerely yours,
    Jordan
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
DevCraft banner