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
|
Can someone please give me a simple example of you to use the PerRequestUnitOfWorkScope from: a. Visual Studio 2005 b. without MVC patterns. I just want to see how to use a unit of work for each web request. I'm having troubles with UnitOfWork being disposed...even though it isn't. Thanks,
|
|
|
Hi Craig, All you really need to do is setup a global static instance of the PerRequestUnitOfWorkScope on your Global.asax (or in a service locator, wherever you want really) and access it from around you web app. The PerRequestUnitOfWorkScope just takes care of holding onto a unit of work in the http context for the duration of the request and then disposes of it once it the scope gets cleaned up. We will look at documenting the scopes further as it can be a little confusing. We're also planning to add much improved samples in LightSpeed 3.0 to help out futher. Let me know if you want any further advice on this. Kind regards, John-Daniel Trask |
|
|
Hi there, I've been trying to find out the best way to use the PerRequestUnitOfScope: The PerRequestUnitOfWorkScope just takes care of holding onto a unit of work in the http context for the duration of the request and then disposes of it once it the scope gets cleaned up. Are you saying that we need to dispose of the UoWScope at the end of the request in order to dispose of the UoW? I have tried setting up a global static application variable to hold the UoWScope, populated in Application_Start and disposed in Application_End, and half expected it to automatically dispose of the current request's UoW at the end of the request. Unfortunately I can see it is opening a new database connection on each request and not closing them. Should I be thinking along the lines of creating a UoWScope object per request, in Application_BeginRequest, and disposing of it in Application_EndRequest? Thanks for your help Ed |
|
|
Hi Ed, We're currently evaluating the usefulness of the various Scope classes because they seem to be more confusing than helpful and also often don't operate as they may initially seem. You would still need to call and dispose of your Scope in the application's end request method. This is obviously not ideal but it should get you started faster (and it's what you suggested near the end so you might already be on the right track!) Let me know if you still have issues after that, John-Daniel Trask |
|
|
Hi John-Daniel, Thanks for replying. Could you clarify whether what I have written below makes sense, or if there is a simpler way to use the unit of work class? I have set up a static lazy-loaded property in Global.asax.cs that creates and returns a PerRequestUnitOfWorkScope, and stores it for subsequent use during the same request in HttpContext.Current.Items. Then Application_EndRequest disposes of it, using the reference to it in HttpContext.Current.Items. So I can now access the scope from within a page using Global.UoWScope. For example we have a HelpRepository; so in our Help page we create a new HelpRepository(Global.UoWScope) and call GetHelpContentById(id). I can see that this works; the scope object is being created on demand, and a database connection is established upon querying the repository for data. The connection is closed when the request ends. That's great, however I was hoping that the PerRequestUnitOfWorkScope would allow me to avoid having to manage the lifecycle of the unit of work. I no longer have to handle the unit of work's lifecycle, but now have to manage the scope's lifecycle in HttpContext.Items. Is there a better way of doing this? Thanks Ed |
|