NHibernate GUID Alternative

A good identifier for an entity is considered to be a number. A unique identifier for an entity, such as identifier that can be synchronized across multiple databases, is considered to be GUID. The only issue with a GUID is that it's generated on the DB side, and therefor has  a certain performance hit (an extra roundtrip to the DB to generate the GUID and let NHibernate know about it on insert of a new record). The alternative is to use a generator strategy guid.comb - a guid generated on the client side and as a result of that eliminate an extra roundtrip to the DB. The other benefit is that the value generated by this strategy is sequential, and therefore the GUID values are somewhat more traceable in terms of their order of creation.

A sample mapping file would look like this:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
                   auto-import="true" 
                   assembly="Example.Domain" 
                   namespace="Example.Domain.Mappings">
  <class name="Message" table="Messages">
    <id name="Id" column="Id">
      <generator class="guid.comb" />
    </id>
    <property name="Text" />
  </class>
</hibernate-mapping>

Quick run generated these values:

2636fb13-a23d-4d2c-b5b5-9b2d016cb9b2    1st
81514f25-15c5-4a86-802d-9b2d016cb9b7    2nd
544746af-8749-4e4c-9b65-9b2d016cb9b7    3rd

Ideas on having a better strategy when dealing with IDs of GUID type?

No Comments