/// <summary>
        /// Generates an id for the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="idMap">The id map.</param>
        /// <returns></returns>
        /// <remarks>
        /// This code was taken from NHibernate.
        /// </remarks>
        public object Generate(object entity, IdMap idMap)
        {
            var guidArray = Guid.NewGuid().ToByteArray();

            var baseDate = new DateTime(1900, 1, 1);
            var now = DateTime.Now;

            // Get the days and milliseconds which will be used to build the byte string 
            var days = new TimeSpan(now.Ticks - baseDate.Ticks);
            var msecs = now.TimeOfDay;

            // Convert to a byte array 
            // Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333 
            var daysArray = BitConverter.GetBytes(days.Days);
            var 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);
        }
예제 #2
0
        /// <summary>
        ///   Gets the id of the specified entitiy.
        /// </summary>
        /// <param name = "entity">The entity.</param>
        /// <returns></returns>
        public object GetId(object entity)
        {
            if (!HasId)
            {
                throw new InvalidCastException(string.Format("{0} does not have a mapped id.", ClassType));
            }

            return(IdMap.GetValue(entity));
        }
        /// <summary>
        /// Generates an id for the specified entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="idMap">The id map.</param>
        /// <returns></returns>
        public object Generate(object entity, IdMap idMap)
        {
            var id = idMap.GetValue(entity);

            if (Equals(id, idMap.UnsavedValue))
                throw new IdGenerationException(string.Format("Ids for {0} must be manually assigned before saving.", entity.GetType()));

            return id;
        }
예제 #4
0
 /// <summary>
 /// Generates an id for the specified entity.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <param name="idMap">The id map.</param>
 /// <returns></returns>
 public object Generate(object entity, IdMap idMap)
 {
     return Oid.NewOid();
 }