This is a migrated thread and some comments may be shown as answers.

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

5 Answers 167 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jassey Nguyen
Top achievements
Rank 2
Jassey Nguyen asked on 03 Dec 2009, 09:35 PM
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?

5 Answers, 1 is accepted

Sort by
0
Zoran
Telerik team
answered on 04 Dec 2009, 08:01 AM
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.
0
Jassey Nguyen
Top achievements
Rank 2
answered on 04 Dec 2009, 02:19 PM
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
0
Zoran
Telerik team
answered on 04 Dec 2009, 05:21 PM
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.
0
Jassey Nguyen
Top achievements
Rank 2
answered on 04 Dec 2009, 06:12 PM
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!
0
Jordan
Telerik team
answered on 07 Dec 2009, 12:01 PM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Jassey Nguyen
Top achievements
Rank 2
Answers by
Zoran
Telerik team
Jassey Nguyen
Top achievements
Rank 2
Jordan
Telerik team
Share this question
or