Identity generation refers to the strategy used by LightSpeed to obtain indentifiers for new entities.

LightSpeed supports four different identity generation strategies.

KeyTable Identity Generation

KeyTable identity generation (Fowler PoEAA) uses a single table in your database that stores the current identity value. This allows LightSpeed to secure a block of identities (the default size being 10 and is configurable) and use them to set the identity value of newly created identities in your system. This works because every identity in the database, even in different tables, is unique.

KeyTable is a great identity method if you need high performance from you database as LightSpeed does not need to flush new entities to obtain identity values. That means less round trips to the database and therefore a more speedy application.

A small setup step is required to use KeyTable, you will need to run the KeyTable.sql schema file for your database to create the necessary table. This script can be found in your LightSpeed install directory.

This method uses an integer under the covers and therefore your entities should be implemented as using an integer:

CopyUsing Integer Identifiers
1public class MyModelClass : Entity<int>

Create a base model class as a layer of abstraction between your model and the LightSpeed Entity<TId> class
 

Sequence Identity Generation

Sequence identities are similar to the KeyTable identity generation but are natively supplied by the database engine. This means using sequence identity generation is limited to databases that support it: Oracle and PostgreSQL.

As with the KeyTable identity generation method, Sequence provides a high performance mechanism for creating identities. And like the KeyTable identity generation, some setup is required. You need to run the Sequence.sql schema file for your database to create the necessary schema changes. This script can be found in your LightSpeed install directory.

You can also use multiple sequences by selecting the MultiSequence identity method. This supports “sequence per table” conventions. In this case, you must implement INamingStrategy, and return the per-table name from INamingStrategy.GetMultiSequenceName. See Configuration or LightSpeedContext.NamingStrategy for how to apply your custom naming strategy.

The Sequence and MultiSequence methods also use an integer and declaring your entities is therefore the same as for the Key Table strategy.

Guid Identity Generation

Guid identities are great for systems that require strong uniqueness (think replication etc.) and are also extremely practical from a generation perspective. However, while not an issue for many systems, GUIDs can have an impact on application aesthetics. For example, you may not want Guids present in your URLs if you are developing a web application.

CopyUsing Guid Identifiers
1public class MyModelClass : Entity<Guid>

The “GUID Comb” identity method trades randomness for improved INSERT performance. See Jimmy Nilsson’s article introducing the concept for more information.

Identity Column Identity Generation

Identity Column identity generation supports auto-incrementing identity columns. This approach, while supported, is not a recommended method for identity generation. Using this identity type means that LightSpeed cannot do optimized batching as well as it can with other identity methods. This is because LightSpeed needs to obtain the new identity value and update any in-memory associated objects before continuing the flush process. Identity Column identity generation is limited to databases that support it: SQL Server, SQL Server Compact, VistaDB, PostgreSQL, MySQL and SQLite.

This method also uses an integer and therefore your entities should be implemented as using an integer:

CopyUsing Integer Identifiers
1public class MyModelClass : Entity<int>

Configuring identity generation

Identity generation is set on the LightSpeed context, either in code or by using the LightSpeed configuration section in the .config file for your application.

CopyConfiguring LightSpeed Identifier Generation
 1LightSpeedContext.IdentityMethod = IdentityMethod.KeyTable;
 2
 3// or
 4
 5LightSpeedContext.IdentityMethod = IdentityMethod.Guid;
 6
 7// or
 8
 9LightSpeedContext.IdentityMethod = IdentityMethod.GuidComb;
10
11// or
12
13LightSpeedContext.IdentityMethod = IdentityMethod.Sequence;
14
15// or
16
17LightSpeedContext.IdentityMethod = IdentityMethod.MultiSequence;
18
19// or
20
21LightSpeedContext.IdentityMethod = IdentityMethod.IdentityColumn;

When using the KeyTable, Sequence or MultiSequence types you can also fine tune the LightSpeedContext.IdentityBlockSize. The default value is 10 and in most applications you will not need to change this default.

CopyConfiguring IdentityBlockSize
1LightSpeedContext.IdentityBlockSize = 20;

Per-Table Identity Generation

In some cases your database may contain tables that do not use the standard identity method. For example, your database may primarily use integer IDs but contain a couple of tables that use GUID IDs. In this case you can use the TableAttribute to specify an override identity method for these tables.

Natural Keys

Some legacy databases use natural keys—that is, the identity is a business-meaningful piece of information rather than an opaque numeric or GUID ID. This pattern is not well supported by LightSpeed, because LightSpeed is designed to allocate its own IDs and relies on the actual ID value being unimportant. However, if you need to use LightSpeed with a database where you must control the IDs, you can override the Entity.GeneratedId method. LightSpeed will call this exactly once in the lifecycle of each entity, when it is first added to a unit of work. It is your responsibility to ensure that you have all the data required to compute the natural key before the entity is added to the unit of work, and to ensure that the generated key is unique across entities of the given type.