Building ASPNET Web Forms Applications
Unit of Work Scoping
To scope your UnitOfWork instances on a per request basis you will want to make use of the PerRequestUnitOfWorkScope<TUnitOfWork> helper class which is included as part of the Mindscape.LightSpeed assembly.
First you will want to declare your LightSpeedContext so it is accessible for creating UnitOfWork instances. We recommend that this is done as part of your Global.asax.cs so it is always available.
Example declaration of a static LightSpeedContext in Global.asax.cs |
public static LightSpeedContext<MyModelUnitOfWork> LightSpeedContext |
We would then recommend that you set up a base class for your pages (or if you are using a controller/presenter approach then in your base class for your controllers/presenters) which can leverage the PerRequestUnitOfWorkScope<TUnitOfWork> to provide you with access to your UnitOfWork.
Example use of PerRequestUnitOfWorkScope<TUnitOfWork> in your base class |
public class PageBase : System.Web.UI.Page |
The PerRequestUnitOfWorkScope class holds instances of your typed UnitOfWork instances in the HttpContext.Items collection so they will be available for the duration of the request.
The UnitOfWork will only be instantiated once you call the .Current property on the PerRequestUnitOfWorkScope instance so it is safe to instantiate the holder class early and set up a property accessor to pass through to the .Current property on the holder.
Example declaration of a UnitOfWork property on your base class |
public ModelUnitOfWork UnitOfWork |
Finally you will need to dispose the UnitOfWork instance at the end of the request to ensure the database connection is released. We recommend that this is done as part of the EndRequest event and that you hook this in your Global.asax.cs
Example of PerRequestUnitOfWorkScope<TUnitOfWork> disposal code |
protected void Application_EndRequest(object sender, EventArgs e) |
Validation
LightSpeed provides a rich, extensible object-level validation framework. For more details about the validation framework in LightSpeed please review the Validation section in the Creating Domain Models chapter.
When you approach validation with ASP.NET you will be interested in handling both client and server side validation concerns. You will implement your client side validation concerns manually and then you can use the .IsValid property in your postback events to determine if your updated LightSpeed entity is valid.
Example of testing a LightSpeed entity for validity on postback |
// your manual assignment code would go here |
You may also wish to surface the validation error messages for your entity and bind those to a summary on the page. The pattern we would generally recommend for this is to expose an Errors property on your page which returns a LightSpeed ValidationErrorsCollection instance. This can then be returned from any of your entities which are invalid.
Example of testing a LightSpeed entity for validity on postback |
public Member Member |
Data Binding using EntityDataBinder
The Mindscape.LightSpeed.Web assembly contains a simple two way data-binder which can be used to help perform binding to and from your LightSpeed entities. To use the data binder you need to describe the binding behavior between form fields and data accessible to your page instance.
An example describing a two way data-binding between two fields on a form and a Member entity which has been exposed on the page instance |
<lightspeed:EntityDataBinder runat="Server" ID="DataBinder"> |
An example which would bind validation errors to an errors repeater |
<lightspeed:EntityDataBinder runat="Server" ID="DataBinder"> |
Like other data-bound controls in ASP.NET you need to explicitly call .DataBind() on the control to ask it to perform a data-binding operation. Because the source data is specified as part of the declarative mark-up for the control there is no DataSource property which needs to be assigned.
For unbinding data back to your entities you will need to call .Unbind() in your postback events after which it is sensible to validate your objects and then deal with any resulting errors.
An example of calling DataBind and Unbind |
protected void Page_Load(object sender, EventArgs e) |
Samples
For example code you can review the Aptitude Test sample which has been installed alongside LightSpeed.