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
Implementing Storage Policies with LightSpeed Entity Tracking Soft Deletion Timestamps for Entity Tracking and Soft Deletion User Ids for Entity Tracking and Soft Deletion Concurrent Editing Implementing Policies in HandCoded Entities Change Tracking

Timestamps for Entity Tracking and Soft Deletion

By default, the times stored by LightSpeed for the CreatedOn, UpdatedOn and DeletedOn fields are the current local time of the computer where LightSpeed is running.  You may want to override this.  For example, if a database is shared across multiple time zones, you may want to use UTC or some other standard time zone so that the sort order is consistent regardless of which site created or updated a record.  Or you may want to force a ‘fake’ time for unit testing purposes.

To do this, set LightSpeedContext.AutoTimestampMode in code, or the autoTimestampMode attribute in configuration.  The available values are defined by the AutoTimestampMode enumeration.

Setting the timestamp strategy in configuration

<add name="Test"
     autoTimestamps="Utc" />

Built-In Timestamp Strategies

There are two built-in timestamp strategies, Local and Utc.

·         Local: The timestamp is the local time of the computer where LightSpeed is running.

·         Utc: The timestamp is UTC/GMT.

In both cases the time comes from the clock of the machine where LightSpeed is running.  If clocks are not well synchronised then the errors will be reflected in the stored timestamps.

Using a Custom Timestamp Strategy

If you use the Custom mode, you must also implement IAutoTimestampStrategy, and set LightSpeedContext.CustomAutoTimestampStrategy to an instance of that implementation.

Implementing IAutoTimestampStrategy for unit testing

public class FixedAutoTimestampStrategy : IAutoTimestampStrategy
{
  private DateTime _timestamp;
  public FixedAutoTimestampStrategy(DateTime timestamp)
  {
    _timestamp = timestamp;
  }
  public AutoTimestampMode Mode
  {
    // Custom strategies must always return AutoTimestampMode.Custom
    get { return AutoTimestampMode.Custom; }
  }
  public DateTime GetTime()
  {
    return _timestamp;
  }
}

Using the custom strategy

_context.AutoTimestampMode = AutoTimestampMode.Custom;
_context.CustomAutoTimestampStrategy = new FixedAutoTimestampStrategy(Tests.NominalTime);

You can also specify a custom timestamp strategy in configuration using its assembly‑qualified type name.  In this case the strategy class cannot take constructor parameters.

Setting a custom timestamp strategy in configuration

<add name="Test"
     autoTimestamps="MyApp.MyTimestampStrategy, MyApp" />

Database-side Timestamping

LightSpeed 5 also includes support for database-side timestamps. With your custom timestamp strategy implementation, have it also implement the IAutoTimestampExpressionStrategy interface. Your GetTimeExpression() method should return the desired SQL expression for the column, or null to use the client-side timestamp returned from IAutoTimestampStrategy.GetTime(). Here’s one implementation:

Setting a custom timestamp strategy in configuration

public object GetTimeExpression(Type type, TimestampUsage usage, LightSpeedContext context) {
  if (usage == TimestampUsage.DeletedOn)
    return null;
  return "DEFAULT"; // or "GETDATE()" or "GETUTCDATE()" etc.
}

LightSpeed will not read back the timestamp generated by the database. After saving changes, therefore, the entity will contain the timestamp returned from IAutoTimestampStrategy.GetTime(). If your application needs to know the timestamp generated by the database, you will need to reload the entity.

If your application needs to be portable across different database engines, you can use the LightSpeedContext.DataProvider value to determine which database is being targeted.

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