You are here: Platform Specific Issues > Disconnected Objects > Comparison Of Different IDs > Example Guid

Example Guid

This example demonstrates how you can use Guids to identify objects across objects containers. Take a look advantages and disadvantages of Guids: See "Comparison Of Different IDs"

This example assumes that all object have a common super-class, IDHolder, which holds the Guid in a field.

private readonly Guid guid = Guid.NewGuid();

public Guid ObjectId
{
    get { return guid; }
}
IDHolder.cs: generate the id
Private ReadOnly guid As Guid = Guid.NewGuid()

Public ReadOnly Property ObjectId() As Guid
    Get
        Return guid
    End Get
End Property
IDHolder.vb: generate the id

It's important to index the id-field, otherwise looking up for object by id will be slow.

configuration.Common.ObjectClass(typeof (IDHolder)).ObjectField("guid").Indexed(true);
UuidOnObject.cs: index the uuid-field
configuration.Common.ObjectClass(GetType(IDHolder)).ObjectField("guid").Indexed(True)
UuidOnObject.vb: index the uuid-field

The id is hold by the object itself, so you can get it directly.

IDHolder uuidHolder = (IDHolder) obj;
Guid uuid = uuidHolder.ObjectId;
UuidOnObject.cs: get the uuid
Dim uuidHolder As IDHolder = DirectCast(obj, IDHolder)
Dim uuid As Guid = uuidHolder.ObjectId
UuidOnObject.vb: get the uuid

You can get the object you can by a regular query.

IDHolder instance = container.Query(delegate(IDHolder o) { return o.ObjectId.Equals(idForObject); })[0];
UuidOnObject.cs: get an object its UUID
Dim instance As IDHolder = container.Query(Function(o As IDHolder) o.ObjectId.Equals(idForObject))(0)
UuidOnObject.vb: get an object its UUID