Mindscape
  • Register
  • Login
  • YOUR CART IS EMPTY
home
Software
  • Developers Toolbox
  • LightSpeed
  • Raygun
  • WPF Elements
  • Web Workbench
  • Metro Elements
  • Silverlight Elements
  • NHibernate Designer
  • Phone Elements
  • WPF Diagrams
  • Simple DB management
  • Visual Tools for SharePoint
Downloads
Store
  • Buy online
  • Purchase order
  • Volume discounts
  • Reseller
  • Software license
  • Contact sales
Blog
Support
About
  • About Us
  • Contact
  • Testimonials
rss
twitter
facebook
Building Web Applications Building ASPNET Web Forms Applications Building ASPNET MVC Applications New in LightSpeed 5 Running LightSpeed in Medium Trust ASPNET Dynamic Data

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 
  = new LightSpeedContext<MyModelUnitOfWork>("Development");

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
{
  private PerRequestUnitOfWorkScope<ModelUnitOfWork> _unitOfWorkScopeHolder;
  public PageBase()
  {
    _unitOfWorkScopeHolder = new PerRequestUnitOfWorkScope<ModelUnitOfWork>(Global.LightSpeedContext);
  }
}

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
{
  get { return _unitOfWorkScopeHolder.Current; }
}

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)
{
  var scope = new PerRequestUnitOfWorkScope<ModelUnitOfWork>(LightSpeedContext);
  if (scope.HasCurrent)
  {
    scope.Current.Dispose();
  }
}

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
if (!MyEntityInstance.IsValid)
{
  // triggering any additional validation messages would go here
  return;
}

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
{
  get
  {
    if (_member == null)
    {
      _member = new Member();
    }
    return _member;
  }
  set
  {
    _member = value;
  }
}
public ValidationErrorCollection ValidationErrors
{
  get { return Member.IsValid ? null : Member.Errors; }
}
// Example of binding the ValidationErrors collection on the page
<% if (IsPostBack && ValidationErrors != null)
   { %>
<div id="errors">
  <h2>Remaining Details Required:</h2>
  <ul>
    <asp:Repeater runat="Server" ID="ErrorsRepeater">
      <ItemTemplate>
        <li><%# System.Web.UI.DataBinder.Eval(Container.DataItem, "ErrorMessage") %></li>
       </ItemTemplate>
    </asp:Repeater>
  </ul>
</div>
<% } %>

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">
 
    <lightspeed:EntityDataBindingItem runat="Server" BindingMode="TwoWay" BindingSource="Member" BindingSourceMember="Username" TargetControl="SignUpUsername" TargetControlProperty="Text" />
    <lightspeed:EntityDataBindingItem runat="Server" BindingMode="TwoWay" BindingSource="Member" BindingSourceMember="FirstName" TargetControl="SignUpFirstName" TargetControlProperty="Text" />
 
</lightspeed:EntityDataBinder>

An example which would bind validation errors to an errors repeater

<lightspeed:EntityDataBinder runat="Server" ID="DataBinder">
 
    <lightspeed:EntityDataBindingItem runat="Server" BindingMode="OneWay"
        BindingSource="this" BindingSourceMember="ValidationErrors" TargetControl="ErrorsRepeater" TargetControlProperty="DataSource" />
 
</lightspeed:EntityDataBinder>

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)
{
  if (!IsPostBack)
  {
    DataBinder.DataBind();
  }
}
protected void SignUpButton_Click(object sender, EventArgs e)
{
  DataBinder.Unbind();
  if (!Member.IsValid)
  {
    DataBinder.DataBind();
    return;
  }
}

Samples

For example code you can review the Aptitude Test sample which has been installed alongside LightSpeed.

Data Products

  • LightSpeed ORM
  • NHibernate Designer
  • SimpleDB Tools
  • SharePoint Tools

DevOp Tools

  • Raygun

Visual Controls

  • WPF Elements
  • WPF Diagrams
  • Silverlight Elements
  • Phone Elements

Popular Products

  • Web Workbench

    Modern web development for Visual Studio!

  • Mindscape Megapack

    All Mindscape products for one awesome price!

Quick Links

  • Forums
  • Blog
  • Register
  • Login
  • Contact us
  • Twitter
  • Facebook
  • Google+
  • YouTube
  • Linkedin
  • Rss

© Mindscape 2025. Mindscape is a registered trademark of Mindscape Limited.

  • Terms
  • Privacy