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
|
Hi Guys, I am after some advice :) I have my (LightSpeed) model in its own class project. My MVC app references this model. I can access to update etc the model from the MVC app with no problems, but I wondered what the best way to access a Unit Of Work from within the Model itself was. I have a class like this, which will probably show what I am *trying* to do – this does seem to work (except for the 1st time it’s instantiated)...
class NewUnitOfWork : IDisposable
And then within the model in either extended partial classes or my own new classes would use it like this...
public override bool ValidateUser(string username, string password)
Is there a better way of achieving this? I think there must be something wrong with my code as well, as it doesn’t work on the first hit – but then works from the 2nd onwards.
Thanks for any help! |
|
|
Could you expand on "doesn't work"? Exception, nothing happens, incorrect result, etc.? What happens when you step through the code in the debugger -- can you see where it starts to behave incorrectly? |
|
|
Sorry Ivan - In the ValidateUser code when the line.... var userDetails = uow.Users.Where(a => a.Enabled && a.UserName == cleanUsername).SingleOrDefault(); Is run for the first time, I get this exception... System.ObjectDisposedException was caught But reloading, the page or just trying again, there is no exception - I'm sure it's a problem with my code - but is what I am trying to do the write way of doing it?
|
|
|
Sorry Ivan - In the ValidateUser code when the line.... var userDetails = uow.Users.Where(a => a.Enabled && a.UserName == cleanUsername).SingleOrDefault(); Is run for the first time, I get this exception... System.ObjectDisposedException was caught But reloading, the page or just trying again, there is no exception - I'm sure it's a problem with my code - but is what I am trying to do the write way of doing it?
|
|
|
Sorry Ivan - In the ValidateUser code when the line.... var userDetails = uow.Users.Where(a => a.Enabled && a.UserName == cleanUsername).SingleOrDefault(); Is run for the first time, I get this exception... System.ObjectDisposedException was caught But reloading, the page or just trying again, there is no exception - I'm sure it's a problem with my code - but is what I am trying to do the write way of doing it?
|
|
|
Hmm, I'm not sure why you're seeing a pattern of 'fails first time than succeeds second time,' but there is a bug in your code which could lead to the exception you describe. In this line: using(var uow = new NewUnitOfWork().UoW) you create a NewUnitOfWork which is never again referenced. That makes the NewUnitOfWork eligible for garbage collection and finalisation. If you were very unlucky then the NewUnitOfWork could be finalised before the Users query runs. And as part of finalisation it would dispose the ModelUnitOfWork. It's weird that you seem to be reliably "very unlucky" the first time around and not so unlucky the second time around, but that's all I can think of! The solution is to rework NewUnitOfWork. NewUnitOfWork is really a factory for creating ModelUnitOfWork objects: it's not a container which *owns* the unit of work once it's been created (at least not the way you're using it). So rewrite it to something like this: static class ModelUnitOfWorkFactory { And use it as follows: using (ModelUnitOfWork uow = ModelUnitOfWorkFactory.Create()) { |
|
|
Fantastic - Thanks Ivan - Looks good :) |
|