This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm having problems returning Lightspeed objects in methods. More specifically, when trying to access properties on the returned object, I'm presented with ObjectDisposedExceptions. For example, my underlying class is:
And I would use as follows:
The trouble is, the BLLContext object (and therefor the underlying UnitofWork) is disposed immediately after the return statement. When trying to access any of the returned User object's properties I'm presented with an ObjectDisposedException exception. I can get around this by adding a call to .ToList() - return controller.LoadAll().ToList(). This effectively creates a copy of the original User collection - thus removing the scoping issue - but it feels like a hacky way to work around the problem. What should I be doing here? Am I using Lightspeed in a way it was not designed to be used? |
|
|
This is fine, what you need to do is explicitely call UnitOfWork.Detach for each User object you are loading to detach them from the UnitOfWork. The exception comes from trying to lazily load association data which requires access to a UnitOfWork. Because the instances have not been detached they try and access the UnitOfWork they were associated with (the one they were loaded in) but since it has been disposed the exception is triggered.
|
|
|
Hi Jeremy. Thanks for the prompt response! I wasn't familiar with the Detach method, so that really helps (and feels much cleaner that the .ToList() approach). I fully understand the issue with lazy loading, and use eager loading where necessary. |
|