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've been using LightSpeed as recommended in the instruction manual(with a UnitOfWork property in a base page), but now I have an .ashx file that requires info from a unit of work but can't access it as the .ashx file's ProcessRequest method is sometimes called before a page exists. Is there a nice way of handling this? |
|
|
Assuming you want your ASHX and ASPX to share the same unit of work, you can use a PerRequestUnitOfWorkScope. This will provide you with a single unit of work for the entire duration of the HTTP request. So you can write: class BasePage : Page { class MyAshx : IHttpHandler { The important thing to understand is that even though you have two separate Scope objects in play, they will both return the same unit of work for the same request. So the ASHX and the page will share the same unit of work even though they get it through two different conduits. Does that address your requirement? More info at http://www.mindscapehq.com/documentation/lightspeed/Building-Web-Applications-/Building-ASPNET-Web-Forms-Applications if you need it. |
|
|
Do I do the same type of thing for Master Pages and my partial classes?
In some legacy code we have a public static UnitOfWorkHolder with a .current property that returns a UnitOfWork created in Application_BeginRequest and disposed of in Application_EndRequest. While this makes it easy to use, it seems like a terrible design and I am worried that we aren't using LS properly. Are there any downsides to doing things this way? And do you recommend the above method over this one? |
|
|
And yes that does answer my first question :). Is there anywhere I can read up more about the PerRequestUnitOfWorkScope? I've read the whole instruction manual and made notes on it, and the ASP.Net forms section was very useful but it has me questioning our currently practices. |
|
|
The UnitOfWorkHolder is really just a way for you to get at the UnitOfWork which is being stashed in HttpContext.Current.Items, so it doesnt need to be retained, nor does having it as a global present any particular problems since each time it will be accessing the current requests instance. Generally we recommend having a base class for your page instances and wrapping access to the UnitOfWork through the UnitOfWorkHolder there. Here is an example:
public class PageBase : System.Web.UI.Page { private PerRequestUnitOfWorkScope<ModelUnitOfWork> _unitOfWorkScopeHolder;
public PageBase() { _unitOfWorkScopeHolder = new PerRequestUnitOfWorkScope<ModelUnitOfWork>(Global.LightSpeedContext); }
public ModelUnitOfWork UnitOfWork { get { return _unitOfWorkScopeHolder.Current; } } }
You will still want to dispose the UnitOfWork as part of the EndRequest, here is an example:
protected void Application_EndRequest(object sender, EventArgs e) { var scope = new PerRequestUnitOfWorkScope<ModelUnitOfWork>(LightSpeedContext); if (scope.HasCurrent) { scope.Current.Dispose(); } }
Hope that helps! Jeremy |
|
|
Hello, I have that exact code (from the instruction manual) written up. I was just wondering where I get my UnitOfWork from for Partial classes that extend the LS model and for Master Pages, as some controls on Master Pages are filled with entries from the database, but a Master page doesn't seem to know which page is using it. |
|
|
If I use this PageBase method, will I have to create the PerRequestUnitOfWorkScope for every single partial class and master page? |
|
|
Your model partial classes have access to the UnitOfWork property (this is defined on the Entity base class). Your master pages would need a similar declaration as on PageBase in the code behind to get access to the UnitOfWork.
Jeremy |
|
|
Cool, thanks to you and Ivan for all your help :). |
|
|
Am I going to have to cast that UnitOfWork to ModelUnitOfWork everytime I want to access collections of entities. Eg: UnitOfWork.ColourSchemes.Where(cs => cs.Id == ColourSchemeId)).FirstOrDefault(); Is going to need a cast to ModelUnitOfWork? I take it there is no way around this? |
|
|
You can do that, or you can instead use the Query extension method: this.UnitOfWork.Query<ColourScheme>().Where(...) (In fact this is exactly how the ColourSchemes property is implemented in the ModelUnitOfWork generated code.) The advantage of using the Query extension method over the cast is that it works even if (for some reason) you get a base UnitOfWork instead of a ModelUnitOfWork. The small downside is that you need remember to put using Mindscape.LightSpeed.Linq; in your file to bring the Query extension method into scope. |
|