forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUUIDStringGenerator.cs
51 lines (45 loc) · 1.5 KB
/
UUIDStringGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Text;
using NHibernate.Engine;
namespace NHibernate.Id
{
/// <summary>
/// An <see cref="IIdentifierGenerator" /> that returns a string of length
/// 16.
/// </summary>
/// <remarks>
/// <p>
/// This id generation strategy is specified in the mapping file as
/// <code><generator class="uuid.string" /></code>
/// </p>
/// <para>
/// The identifier string will NOT consist of only alphanumeric characters. Use
/// this only if you don't mind unreadable identifiers.
/// </para>
/// <para>
/// This impelementation was known to be incompatible with Postgres.
/// </para>
/// </remarks>
public partial class UUIDStringGenerator : IIdentifierGenerator
{
#region IIdentifierGenerator Members
/// <summary>
/// Generate a new <see cref="String"/> for the identifier using the "uuid.string" algorithm.
/// </summary>
/// <param name="session">The <see cref="ISessionImplementor"/> this id is being generated in.</param>
/// <param name="obj">The entity for which the id is being generated.</param>
/// <returns>The new identifier as a <see cref="String"/>.</returns>
public object Generate(ISessionImplementor session, object obj)
{
StringBuilder guidBuilder = new StringBuilder(16, 16);
byte[] guidInBytes = Guid.NewGuid().ToByteArray();
// add each item in Byte[] to the string builder
for (int i = 0; i < guidInBytes.Length; i++)
{
guidBuilder.Append((char) guidInBytes[i]);
}
return guidBuilder.ToString();
}
#endregion
}
}