Пример #1
0
        /// <summary>
        /// Creates an IErrorMessageSource from an expression: () => MyResources.SomeResourceName
        /// </summary>
        /// <param name="expression">The expression </param>
        /// <param name="resourceProviderSelectionStrategy">Strategy used to construct the resource accessor</param>
        /// <returns>Error message source</returns>
        public static IStringSource CreateFromExpression(Expression<Func<string>> expression, IResourceAccessorBuilder resourceProviderSelectionStrategy) {
            var constant = expression.Body as ConstantExpression;

            if (constant != null) {
                return new StaticStringSource((string)constant.Value);
            }

            var member = expression.GetMember();

            if (member == null) {
                throw new InvalidOperationException("Only MemberExpressions an be passed to BuildResourceAccessor, eg () => Messages.MyResource");
            }

            var resourceType = member.DeclaringType;
            var resourceName = member.Name;
            return new LocalizedStringSource(resourceType, resourceName, resourceProviderSelectionStrategy);
        }
Пример #2
0
        public static ResourceMetaData BuildResourceAccessor(Expression<Func<string>> expression)
        {
            var constant = expression.Body as ConstantExpression;

            if(constant != null) {
                return new ResourceMetaData(null,null, () => (string)constant.Value);
            }

            var member = expression.GetMember();

            if(member == null) {
                throw new InvalidOperationException("Only MemberExpressions an be passed to BuildResourceAccessor, eg () => Messages.MyResource");
            }

            var resourceType = member.DeclaringType;
            var resourceName = member.Name;

            return BuildResourceAccessor(resourceName, resourceType);
        }
        private IEnumerable<ModelClientValidationRule> GetClientRules(Expression<Func<TestModel, object>> expression )
        {
            var propertyName = expression.GetMember().Name;
            var metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(TestModel), propertyName);

            var factory = new Mock<IValidatorFactory>();
            factory.Setup(x => x.GetValidator(typeof(TestModel))).Returns(validator);

            var provider = new FluentValidationModelValidatorProvider(factory.Object);
            var propertyValidators = provider.GetValidators(metadata, controllerContext);

            return (propertyValidators.SelectMany(x => x.GetClientValidationRules())).ToList();
        }
        private ModelClientValidationRule GetClientRule(Expression<Func<TestModel, object>> expression)
        {
            var propertyName = expression.GetMember().Name;
            var metadata = new DataAnnotationsModelMetadataProvider().GetMetadataForProperty(null, typeof(TestModel), propertyName);

            var factory = new Mock<IValidatorFactory>();
            factory.Setup(x => x.GetValidator(typeof(TestModel))).Returns(validator);

            var provider = new FluentValidationModelValidatorProvider(factory.Object);
            var propertyValidator = provider.GetValidators(metadata, controllerContext).Single();

            var clientRule = propertyValidator.GetClientValidationRules().Single();
            return clientRule;
        }