/// <summary>
        /// 验证实体实例的值得类型是否合法
        /// </summary>
        /// <param name="objectToValidate"></param>
        /// <param name="currentObject"></param>
        /// <param name="key"></param>
        /// <param name="validateResults"></param>
        protected override void DoValidate(object objectToValidate, object currentObject, string key, Validation.ValidationResults validateResults)
        {
            DEEntityInstance instanceData = objectToValidate as DEEntityInstance;

            instanceData.Fields.ForEach(f =>
            {
                //字段的值
                object fieldValue = f.GetRealValue();

                if (f.Definition.FieldType == Enums.FieldTypeEnum.Collection)
                {
                    DEEntityInstanceBaseCollection childEntities = f.GetRealValue() as DEEntityInstanceBaseCollection;
                    if (fieldValue is string)
                    {
                        //"实体实例'{0}'(实体的ID:{1})的字段'{2}'长度没有通过验证"
                        RecordValidationResult(validateResults,
                                               string.Format(this.MessageTemplate, f.Definition.ID),
                                               instanceData,
                                               key);
                    }
                    childEntities.ForEach(ce =>
                    {
                        ce.Validate();
                    });
                }
                else
                {
                    //添加验证,调用值转换函数
                    if (!IsOk(f.Definition.FieldType.ToString(), f.StringValue))
                    {
                        RecordValidationResult(validateResults,
                                               string.Format(this.MessageTemplate, f.Definition.ID),
                                               instanceData,
                                               key);
                    }
                    //if (f.StringValue is string && f.Definition.FieldType != Enums.FieldTypeEnum.String)
                    //{
                    //    RecordValidationResult(validateResults,
                    //        string.Format(this.MessageTemplate, instanceData.Name, instanceData.ID, f.Definition.Name),
                    //        instanceData,
                    //        key);
                    //}
                }
            });
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="serializer"></param>
        /// <returns></returns>
        public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            Dictionary <string, object> dictionary = new Dictionary <string, object>();
            EntityFieldValue            data       = (EntityFieldValue)obj;

            //dictionary.AddNonDefaultValue("FieldID", data.Definition.ID);
            dictionary.AddNonDefaultValue("CodeName", data.Definition.CodeName);
            if (!string.IsNullOrEmpty(data.Definition.ReferenceEntityCodeName))
            {
                dictionary.AddNonDefaultValue("EntityCodeName", data.Definition.ReferenceEntityCodeName);
            }
            dictionary.AddNonDefaultValue("FieldName", data.Definition.Name);

            if (data.Definition.FieldType == Enums.FieldTypeEnum.Collection)
            {
                DEEntityInstanceBaseCollection coll = data.GetRealValue() as DEEntityInstanceBaseCollection;
                if (coll != null)
                {
                    coll.ForEach(i =>
                    {
                        i._notSerialize = true;
                    });
                    dictionary.AddNonDefaultValue("StringValue", coll);
                }
                //else
                //{
                //    dictionary.AddNonDefaultValue("StringValue", " ");
                //}
            }
            else
            {
                if (string.IsNullOrEmpty(data.StringValue))
                {
                    //dictionary.AddNonDefaultValue("StringValue", " ");
                }
                else
                {
                    dictionary.AddNonDefaultValue("StringValue", data.StringValue);
                }
            }
            return(dictionary);
        }
        protected override void DoValidate(object objectToValidate, object currentObject, string key, Validation.ValidationResults validateResults)
        {
            DEEntityInstance instanceData = objectToValidate as DEEntityInstance;

            instanceData.Fields.ForEach(f =>
            {
                if (f.Definition.FieldType == Enums.FieldTypeEnum.Collection)
                {
                    DEEntityInstanceBaseCollection childEntities = f.GetRealValue() as DEEntityInstanceBaseCollection;
                    if (childEntities.Count > f.Definition.Length)
                    {
                        //"实体实例'{0}'(实体的ID:{1})的字段'{2}'长度没有通过验证"
                        RecordValidationResult(validateResults,
                                               string.Format(this.MessageTemplate, f.Definition.ID),
                                               instanceData,
                                               key);
                    }
                    childEntities.ForEach(ce =>
                    {
                        ce.Validate();
                    });
                }
                else
                {
                    if ((((f.Definition.FieldType == Enums.FieldTypeEnum.Bool) && (f.Definition.Length != 1)) ||
                         ((f.Definition.FieldType != Enums.FieldTypeEnum.Bool) && (f.StringValue.Length > f.Definition.Length))
                         ) && (f.Definition.FieldType != Enums.FieldTypeEnum.DateTime)
                        )
                    {
                        RecordValidationResult(validateResults,
                                               string.Format(this.MessageTemplate, f.Definition.ID),
                                               instanceData,
                                               key);
                    }
                }
            });
        }
Exemplo n.º 4
0
        public void Update(DEEntityInstanceBaseCollection collection)
        {
            collection.NullCheck <ArgumentNullException>("实例集合collection不能为空");

            collection.ForEach(this.Update);
        }