/// <summary>
		/// Creates a new instance of the LocalizedErrorMessageSource class using the specified resource name and resource type.
		/// </summary>
		/// <param name="resourceType">The resource type</param>
		/// <param name="resourceName">The resource name</param>
		/// <param name="resourceAccessorBuilder">Strategy used to construct the resource accessor</param>
		public LocalizedStringSource(Type resourceType, string resourceName, IResourceAccessorBuilder resourceAccessorBuilder) {
		    var resourceAccessor = resourceAccessorBuilder.GetResourceAccessor(resourceType, resourceName);

            this.resourceType = resourceAccessor.ResourceType;
			this.resourceName = resourceAccessor.ResourceName;
			this.accessor = resourceAccessor.Accessor;
		}
        /// <summary>
        /// Creates a new instance of the LocalizedErrorMessageSource class using the specified resource name and resource type.
        /// </summary>
        /// <param name="resourceType">The resource type</param>
        /// <param name="resourceName">The resource name</param>
        /// <param name="resourceAccessorBuilder">Strategy used to construct the resource accessor</param>
        public LocalizedStringSource(Type resourceType, string resourceName, IResourceAccessorBuilder resourceAccessorBuilder)
        {
            var resourceAccessor = resourceAccessorBuilder.GetResourceAccessor(resourceType, resourceName);

            this.resourceType = resourceAccessor.ResourceType;
            this.resourceName = resourceAccessor.ResourceName;
            this.accessor     = resourceAccessor.Accessor;
        }
        /// <summary>
        /// Specifies a localized name for the error message.
        /// </summary>
        /// <param name="rule">The current rule</param>
        /// <param name="resourceSelector">The resource to use as an expression, eg () => Messages.MyResource</param>
        /// <param name="resourceAccessorBuilder">Resource accessor builder to use</param>
        public static IRuleBuilderOptions <T, TProperty> WithLocalizedName <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, Expression <Func <string> > resourceSelector, IResourceAccessorBuilder resourceAccessorBuilder = null)
        {
            resourceSelector.Guard("A resource selector must be specified.");
            // default to the static resource accessor builder - explicit resources configured with WithLocalizedName should take precedence over ResourceProviderType.
            resourceAccessorBuilder = resourceAccessorBuilder ?? new StaticResourceAccessorBuilder();

            return(rule.Configure(config => {
                config.DisplayName = LocalizedStringSource.CreateFromExpression(resourceSelector, resourceAccessorBuilder);
            }));
        }
        /// <summary>
        /// Specifies a custom error message resource to use when validation fails.
        /// </summary>
        /// <param name="rule">The current rule</param>
        /// <param name="resourceSelector">The resource to use as an expression, eg () => Messages.MyResource</param>
        /// <param name="resourceAccessorBuilder">The resource accessor builder to use. </param>
        /// <returns></returns>
        public static IRuleBuilderOptions <T, TProperty> WithLocalizedMessage <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, Expression <Func <string> > resourceSelector, IResourceAccessorBuilder resourceAccessorBuilder)
        {
            resourceSelector.Guard("An expression must be specified when calling WithLocalizedMessage, eg .WithLocalizedMessage(() => Messages.MyResource)");

            return(rule.Configure(config => {
                config.CurrentValidator.ErrorMessageSource = LocalizedStringSource.CreateFromExpression(resourceSelector, resourceAccessorBuilder);
            }));
        }
Esempio n. 5
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);
        }