Improve class BaseNopValidator

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 7 ans
Hello

In the BaseNopValidator class of the "Nop.Web.Framework.Validators" namespace there is a method called "PostInitialize", which can be used to work with independent files using partial classes, without modifying the original NopCommerce code but in my Opinion can be improved if ILocalizationService is passed by injection of dependencies to show the messages according to the language.

for example:}

public abstract class BaseNopValidator<T> : AbstractValidator<T> where T : class
    {
        protected readonly ILocalizationService _localizationService;

        protected BaseNopValidator(ILocalizationService localizationService)
        {
            _localizationService = localizationService;
            PostInitialize();
        }

        /// <summary>
        /// Developers can override this method in custom partial classes
        /// in order to add some custom initialization code to constructors
        /// </summary>
        protected virtual void PostInitialize()
        {
            
        }

        /// <summary>
        /// Sets length validation rule(s) to string properties according to appropriate database model
        /// </summary>
        /// <typeparam name="TObject">Object type</typeparam>
        /// <param name="dbContext">Database context</param>
        /// <param name="filterPropertyNames">Properties to skip</param>
        protected virtual void SetStringPropertiesMaxLength<TObject>(IDbContext dbContext, params string[] filterPropertyNames)
        {
            if(dbContext==null)
                return;

            var dbObjectType = typeof(TObject);

            var names = typeof (T).GetProperties()
                .Where(p => p.PropertyType == typeof(string) && !filterPropertyNames.Contains(p.Name))
                .Select(p => p.Name).ToArray();

            var maxLength = dbContext.GetColumnsMaxLength(dbObjectType.Name, names);
            var expression = maxLength.Keys.ToDictionary(name => name, name => Kendoui.DynamicExpression.ParseLambda<T, string>(name, null));

            foreach (var expr in expression)
            {
                RuleFor(expr.Value).Length(0, maxLength[expr.Key]);
            }
        }
    }

This way you can access "_localizationService" and get the message according to the language.
Il y a 7 ans
Hello,

Can you share us where you ended up. Because we also need to implement seperate partial validator class but could not achieve it yet.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.