다음 코드는 NHibernate.Id.GuidCombGenerator 클래스에서 찾을 수 있습니다. 이 알고리즘은 "임의"guid와 DateTime을 결합하여 순차적 (빗) guids를 만듭니다. 내가 표시 한 선에 관한 몇 가지 질문을 가지고 * 1) * 2) 아래 : 모든의NHibernate의 GuidCombGenerator에 대한 몇 가지 질문
private Guid GenerateComb()
{
byte[] guidArray = Guid.NewGuid().ToByteArray();
// *1)
DateTime baseDate = new DateTime(1900, 1, 1);
DateTime now = DateTime.Now;
// Get the days and milliseconds which will be used to build the byte string
TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
TimeSpan msecs = now.TimeOfDay;
// *2)
// Convert to a byte array
// Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333
byte[] daysArray = BitConverter.GetBytes(days.Days);
byte[] msecsArray = BitConverter.GetBytes((long) (msecs.TotalMilliseconds/3.333333));
// Reverse the bytes to match SQL Servers ordering
Array.Reverse(daysArray);
Array.Reverse(msecsArray);
// Copy the bytes into the guid
Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
return new Guid(guidArray);
}
첫째, * 1), 더 가지고 더 나은되지 않을 것 baseDate로 최근 일자, 예 앞으로 더 많은 가치를위한 자리를 마련하기 위해 2000-01-01?
* 2)에 대해서는 어쨌든 datetime의 바이트에만 관심이 있고 SQL Server datetime 필드에 값을 저장하지 않으려 고 할 때 SQL Server의 DateTimes에 대한 정확성에 대해 왜 신경을 씁니까? DateTime에서 제공되는 모든 정확성을 사용하는 것이 더 좋지 않습니까? 지금?
DateTime.UtcNow 대신 DateTime.Now를 사용하는 이유가 궁금합니다. 다른 시간대의 서버에서 guid를 작성하면 오프셋이 달라지며 guid가 순차적이지 않습니다. – MartinF