コード例 #1
0
        private static List <string> ConvertToFluentValidators(DataProperty dataProp, StructuralType structuralType)
        {
            var toReplace = new HashSet <string> {
                "required", "maxLength"
            };
            var convertedVals = new List <string>();
            var entityType    = structuralType as EntityType;

            foreach (var validator in dataProp.Validators.Where(o => toReplace.Contains(o.Name)).ToList())
            {
                dataProp.Validators.Remove(validator);
                Validator newValidator = null;
                var       name         = validator.Name;

                if (name == "required" && NullableProperties.Contains(dataProp.NameOnServer))
                {
                    convertedVals.Add("fvNotNull");
                    convertedVals.Add("fvNotEmpty");
                    dataProp.IsNullable = true;
                    continue;
                }

                switch (name)
                {
                case "required":
                    //Check if the property is a foreignKey if it is then default type value is not valid
                    if (entityType != null && !string.IsNullOrEmpty(dataProp.NameOnServer) &&
                        entityType.NavigationProperties
                        .Any(o => o.ForeignKeyNamesOnServer != null && o.ForeignKeyNamesOnServer
                             .Any(fk => fk == dataProp.NameOnServer)))
                    {
                        newValidator = new Validator {
                            Name = "fvNotEmpty"
                        };
                        var defVal = dataProp.PropertyInfo?.PropertyType.GetDefaultValue();
                        newValidator.MergeLeft(FluentValidators.GetParamaters(new NotEmptyValidator(defVal)));
                        convertedVals.Add("fvNotEmpty");
                    }
                    else
                    {
                        newValidator = new Validator {
                            Name = "fvNotNull"
                        };
                        newValidator.MergeLeft(FluentValidators.GetParamaters(new NotNullValidator()));
                        convertedVals.Add("fvNotNull");
                    }
                    break;

                case "maxLength":
                    newValidator = new Validator {
                        Name = "fvLength"
                    };
                    newValidator.MergeLeft(FluentValidators.GetParamaters(new LengthValidator(0, dataProp.MaxLength)));
                    convertedVals.Add("fvLength");
                    break;
                }
                dataProp.Validators.Add(newValidator);
            }
            return(convertedVals);
        }
コード例 #2
0
        private MetadataSchema SetupValidators(BreezeMetadataBuiltEvent asyncEvent)
        {
            var metadata = asyncEvent.Metadata;

            //Setup entity models (persisted)
            var entityClass      = typeof(IEntity);
            var clientModelClass = typeof(IClientModel);
            var entityModelTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes()
                                                                                      .Where(type => type.IsClass /*&& !type.IsAbstract*/ && (entityClass.IsAssignableFrom(type) || clientModelClass.IsAssignableFrom(type)))).ToList();

            foreach (var structuralType in metadata.StructuralTypes.Where(x => x is EntityType))
            {
                var entityModelType = entityModelTypes.FirstOrDefault(e => e.Name == structuralType.ShortName);
                if (entityModelType != null)
                {
                    var validationRules = _validatorFactory.GetValidator(entityModelType) as IEnumerable <IValidationRule>;
                    if (validationRules == null)
                    {
                        continue;
                    }

                    var allPropertyRules = validationRules.OfType <PropertyRule>()
                                           .Where(x => !string.IsNullOrEmpty(x.PropertyName))
                                           .ToLookup(o => o.PropertyName, o => o);

                    foreach (var dataProperty in structuralType.DataProperties)
                    {
                        dataProperty.Validators = dataProperty.Validators ?? new Validators();

                        var convertedVals = ConvertToFluentValidators(dataProperty, structuralType);

                        var propertyRules = allPropertyRules[dataProperty.NameOnServer]
                                            .Where(pr => pr.RuleSets == null || ValidationRuleSet.AttributeInsertUpdateDefault.Intersect(pr.RuleSets).Any());

                        foreach (var propertyRule in propertyRules)
                        {
                            var currentValidator = propertyRule.CurrentValidator;
                            var name             = FluentValidators.GetName(currentValidator);
                            if (string.IsNullOrEmpty(name) || convertedVals.Contains(name))
                            {
                                continue;
                            }

                            var validator = new Validator()
                            {
                                Name = name
                            };
                            validator.MergeLeft(FluentValidators.GetParamaters(currentValidator));
                            dataProperty.Validators.AddOrUpdate(validator);
                        }
                    }
                }
            }

            //Setup codelists
            //var codeListType = typeof(ICodeList);
            //var codeListTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes()
            //        .Where(type => type.IsClass && !type.IsAbstract && codeListClass.IsAssignableFrom(type))).ToList();
            foreach (var structuralType in metadata.StructuralTypes.OfType <EntityType>())
            {
                if (structuralType.Namespace.EndsWith(".CodeLists"))
                {
                    structuralType.AutoGeneratedKeyType = Metadata.AutoGeneratedKeyType.KeyGenerator;
                }
            }

            var versionedEntityType       = typeof(IVersionedEntity);
            var versionedEntityProperties = new[]
            { "CreatedDate", "CreatedBy", "CreatedById", "ModifiedByDate", "ModifiedBy", "ModifiedById" };

            foreach (var structuralType in metadata.StructuralTypes.OfType <EntityType>())
            {
                var type = Type.GetType($"{structuralType.Namespace}.{structuralType.ShortName}");

                if (type != null && type.IsAssignableFrom(versionedEntityType))
                {
                    foreach (var property in structuralType.DataProperties)
                    {
                        if (versionedEntityProperties.Contains(property.NameOnServer))
                        {
                            property.IsNullable = true;
                            if (property.Validators != null)
                            {
                                var requiredValidator = property.Validators.FirstOrDefault(x => x.Name == "required");
                                if (requiredValidator != null)
                                {
                                    property.Validators.Remove(requiredValidator);
                                }
                            }
                        }
                    }
                }
            }

            return(metadata);
        }