Exemple #1
0
        internal Dictionary <string, dynamic> Autonumber(Type type, object item)
        {
            Dictionary <string, dynamic> autonumbers = new Dictionary <string, dynamic>();
            List <string> autonumberedProps          = new List <string>();
            var           autonumberProps            = this.propertyService.Properties(type.FullName).Where(s => s.IsAutonumber);

            foreach (var autonumberProp in autonumberProps)
            {
                if (!ValueHelper.IsNumeric(autonumberProp.PropertyType) && !autonumberProp.PropertyType.Equals(typeof(Guid)))
                {
                    exceptionService.Throw(new AutonumberDataTypeException());
                }

                if (ValueHelper.DefaultOf(autonumberProp.PropertyType).Equals(autonumberProp.Property.GetValue(item)))
                {
                    dynamic next = null;
                    if (!autonumberProp.PropertyType.Equals(typeof(Guid)))
                    {
                        //lock() is implemeneted in NextNumber()
                        next = NextNumber(type, autonumberProp);
                        autonumberProp.Property.SetValue(item, next);
                        autonumberedProps.Add(autonumberProp.Property.Name);
                    }
                    else
                    {
                        next = Guid.NewGuid();
                        autonumberProp.Property.SetValue(item, next);
                    }
                    autonumbers.Add(autonumberProp.PropertyName, next);
                }
            }

            return(autonumbers);
        }
Exemple #2
0
        private dynamic NextNumber(Type type, PropertyInfoItem prop)
        {
            dynamic defValue = ValueHelper.DefaultOf(prop.PropertyType);

            if (!prop.IsAutonumber)
            {
                return(defValue);
            }

            dynamic value = defValue;

            lock (GetLock())
            {
                if (this.Contains(type, prop.PropertyName))
                {
                    value = this.GetNext(type, prop.PropertyName, prop.IdentityIncrement);
                    if (value < prop.IdentitySeed)
                    {
                        value = prop.IdentitySeed;
                        this.Update(type, prop.PropertyName, value);
                    }
                }
                else if (this.findLastDelegate != null)
                {
                    MethodInfo numOpsSumGenericMethod = typeof(NumericOperations)
                                                        .GetMethod("Sum")
                                                        .MakeGenericMethod(new Type[] { prop.PropertyType });

                    var last = this.findLastDelegate(type);
                    if (last == null)
                    {
                        value = prop.IdentitySeed;
                    }
                    else
                    {
                        value = numOpsSumGenericMethod.Invoke(
                            null,
                            new object[] {
                            new object[]
                            {
                                prop.Property.GetValue(last),
                                prop.IdentityIncrement
                            }
                        });
                    }
                    this.Set(type, prop.PropertyName, numOpsSumGenericMethod, value);
                }
            }
            return(value);
        }