Exemple #1
0
        private ModelMetadata GetTypeMetadata(Type type)
        {
            if (_entityMetadataProvider.IsEntityType(type))
            {
                return(_entityMetadataProvider.GetMetadata(type));
            }

            if (_clientModelMetadataProvider.IsClientModel(type))
            {
                return(_clientModelMetadataProvider.GetMetadata(type));
            }

            return(null);
        }
        /// <summary>
        /// Builds <see cref="BreezeMetadata"/>.
        /// </summary>
        /// <returns>The breeze metadata.</returns>
        public BreezeMetadata Build()
        {
            var metadata = new BreezeMetadata
            {
                StructuralTypes             = new List <StructuralType>(),
                ResourceEntityTypeMap       = new Dictionary <string, string>(),
                EnumTypes                   = new List <EnumType>(),
                LocalQueryComparisonOptions = _localQueryComparisonOptions ?? "caseInsensitiveSQL"
            };

            _addedTypes.Clear();
            _entityMetadata = _classMetadataProvider.GetAll()
                              .Select(o => o.MappedClass)
                              .Where(o => o != null)
                              .Where(o => _includePredicate?.Invoke(o) ?? true)
                              .Distinct()
                              .Select(o => new { MappedClass = o, Metadata = _entityMetadataProvider.GetMetadata(o) })
                              .ToDictionary(o => o.MappedClass, o => o.Metadata);

            foreach (var entityMetadata in _entityMetadata.Values)
            {
                AddEntityType(entityMetadata, metadata);
            }

            if (_clientModelAssemblies == null)
            {
                return(metadata);
            }

            // Add client models
            _clientModelMetadata = _clientModelAssemblies?.SelectMany(assembly => assembly.GetTypes()
                                                                      .Where(t => t.IsClass && !t.IsAbstract && _clientModelMetadataProvider.IsClientModel(t)))
                                   .Where(t => _includePredicate?.Invoke(t) ?? true)
                                   .Select(o => _clientModelMetadataProvider.GetMetadata(o))
                                   .ToDictionary(o => o.Type, o => o);

            foreach (var modelMetadata in _clientModelMetadata.Values)
            {
                AddClientModelType(modelMetadata, metadata);
            }

            return(metadata);
        }
        public override IEnumerable <Validator> GetValidators(DataProperty dataProperty, Type type)
        {
            Validator validator;
            var       addedValidators = new HashSet <string>();

            if (!dataProperty.IsNullable)
            {
                ModelMetadata modelMetadata = null;
                if (_entityMetadataProvider.IsEntityType(type))
                {
                    modelMetadata = _entityMetadataProvider.GetMetadata(type);
                }
                else if (_clientModelMetadataProvider.IsClientModel(type))
                {
                    modelMetadata = _clientModelMetadataProvider.GetMetadata(type);
                }

                // Do not permit default values for foreign key properties
                if (modelMetadata?.ForeignKeyPropertyNames.Contains(dataProperty.NameOnServer) == true || dataProperty.DataType == DataType.String)
                {
                    addedValidators.Add("fvNotEmpty");
                    validator = new Validator("fvNotEmpty");
                    validator.MergeLeft(FluentValidators.GetParameters(new NotEmptyValidator(null)));
                    yield return(validator);
                }
                else
                {
                    addedValidators.Add("fvNotNull");
                    validator = new Validator("fvNotNull");
                    validator.MergeLeft(FluentValidators.GetParameters(new NotNullValidator()));
                    yield return(validator);
                }
            }

            if (dataProperty.MaxLength.HasValue)
            {
                addedValidators.Add("fvLength");
                validator = new Validator("fvLength");
                validator.MergeLeft(FluentValidators.GetParameters(new LengthValidator(0, dataProperty.MaxLength.Value)));
                yield return(validator);
            }

            if (dataProperty.DataType.HasValue && TryGetDataTypeValidator(dataProperty.DataType.Value, out validator))
            {
                yield return(validator);
            }

            if (!_typePropertyValidators.TryGetValue(type, out var propertyValidators))
            {
                if (!(_validatorFactory.GetValidator(type) is IEnumerable <IValidationRule> validationRules))
                {
                    yield break;
                }

                propertyValidators = validationRules.OfType <PropertyRule>().Where(IsRuleSupported).ToLookup(o => o.PropertyName);
                _typePropertyValidators.Add(type, propertyValidators);
            }

            // Add fluent validations
            foreach (var propertyRule in propertyValidators[dataProperty.NameOnServer])
            {
                var currentValidator = propertyRule.CurrentValidator;
                var name             = FluentValidators.GetName(currentValidator);
                if (string.IsNullOrEmpty(name) || addedValidators.Contains(name))
                {
                    continue;
                }

                validator = new Validator(name);
                validator.MergeLeft(FluentValidators.GetParameters(currentValidator));
                yield return(validator);
            }
        }