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 What's the recommended way to declare a LightSpeedContext and unit of work globally in a web app? So for example I could then use... using (var uow = _context.CreateUnitOfWork())
... in any code-behind page of the app. I'm new to LightSpeed and have seen some stuff in other threads about issues with disposing UOW's and making use of PerRequestUnitOfWorkScope - what's the recommended approach? Thanks, Danzig |
|
|
Hi Danzig, The above (a tightly scoped UnitOfWork) would work, however generally for web applications particularly when data binding you will find that you need to keep the UnitOfWork active for the duration of the web request so the ideal approach is to instantiate a UnitOfWork instance as required on a per request basis and then re-use that during the request and ensure it is disposed at the end of the request so the associated database connection can be closed etc. The PerRequestUnitOfWorkScope class pretty much encapsulated that behavior, in that it creates a UnitOfWork instance as required and then stores that in HttpContext.Items for later re-use in that request. What it does not do however is dispose of the UnitOfWork, and you will need to do this yourself, either by explicitely disposing the PerRequestUnitOfWorkScope, or the UnitOfWork. I would recommend you set up a static global LightSpeedContext object in Global.asax which you can use in conjunction with PerRequestUnitOfWorkScope (it needs it to create the UnitOfWork instances by calling .CreateUnitOfWork()). e.g. public class Global : System.Web.HttpApplication [...] On a per page basis you can then use PerRequestUnitOfWorkScope to get access to the current UnitOfWork. e.g. public class BasePage : System.Web.UI.Page and then have your pages derive from BasePage and use the UnitOfWork accordingly. To clean up I would suggest you look at hooking the EndRequest event on the HttpApplication (Global.asax) and getting the event to fetch the current requests UnitOfWork and if it exists dispose it. e.g. private PerRequestUnitOfWorkScope<ModelUnitOfWork> _currentRequestScopeHolder; public Global() void Global_EndRequest(object sender, EventArgs e)
Note: ModelUnitOfWork is a typed UnitOfWork generated by the designer. Generally you will want to deal with typed UnitOfWork instances due to the IQueryable (LINQ) properties on these.
Hope that helps, Jeremy |
|
|
Thanks for your reply Jeremy. A question: When you say "and then have your pages derive from BasePage and use the UnitOfWork accordingly", is BasePage in your example just a container for instantiating PerRequestUnitOfWorkScope and then all other web pages would have "using BasePage;" to access UnitOfWork? Cheers, Danzig |
|
|
In all your other pages, inherit from the BasePage class instead of System.Web.UI.Page. So if you have a page called MyPage.aspx, in MyPage.aspx.cs change public partial class MyPage : Page to public partial class MyPage : BasePage Then you can just access UnitOfWork in the same way as any other class variable Thanks, Chris |
|
|
Cool, thanks! That's working nicely. |
|
|
Another question about this: How can I use the global unit of work from within a partial class? eg. In my model I have entity Artist. I have also created my own partial class Artist so I can do things like Artist.Authenticate(username, password). But I can't inherit from basepage in my partial class because I get the build error: "Partial declarations of 'Artist' must not specify different base classes.". And without inheriting from basepage I can't use "uow" in my partial class. Many thanks. |
|
|
Once an entity such as an Artist has been assigned to a UOW, you can access that as this.UnitOfWork. If you need access to the request UOW regardless of whether the entity is part of that UOW, and if you're using PerRequestUnitOfWorkScope in your BasePage as suggested, then you can just create another scope object. This sounds a bit weird because Artist will be creating a different instance of PerRequestUnitOfWorkScope, but as long as it's part of the same request it will actually resolve to the same UOW, the one stored in the current HttpContext. So... partial class Artist { Again, I realise this looks a bit odd. But remember that the PerRequestUnitOfWorkScope object is not a UOW scope or holder in itself, but rather an accessor object for a "real" scope which is independent of which instance of the accessor class you go through -- that "real" scope in this case being the HTTP request. Hope that makes sense! |
|