When performing validation, Validator have multiple instances

Oct 4, 2013 at 12:31 PM
Edited Oct 4, 2013 at 1:47 PM
asp.net mvc 4 ,When performing validation,The same Validator have multiple instances,why ?
Coordinator
Oct 4, 2013 at 2:56 PM
Hello,

I'm afraid I don't understand your question. Perhaps you could provide some more details?
Oct 6, 2013 at 6:27 AM
Edited Oct 6, 2013 at 6:28 AM
抱歉,我英文不好。我意思是:我在ASP.NET MVC 4 网站项目里定义了一个AbstractValidator<ReportViewModel>类,当用户提交数据的时候,创建了多个AbstractValidator<ReportViewModel>的实例。我不知道是不是你的框架有问题,我新建了其他的ASP.NET MVC 4项目再次进行测试,也是会创建多个AbstractValidator<ReportViewModel>的实例。我是在DEBUG这个类的时候才发现的。
Coordinator
Oct 6, 2013 at 1:37 PM
Please post in English. Thanks.
Oct 7, 2013 at 8:25 AM
from google translate

Sorry, my English is not good. I mean: I am in ASP.NET MVC 4 Web site project in the definition of a AbstractValidator <ReportViewModel> class, when the user submits the data, create multiple AbstractValidator <ReportViewModel> instance. I do not know if your frame a problem, I created a new ASP.NET MVC 4 project other test again, will also create multiple AbstractValidator <ReportViewModel> instance. I was in this class only when DEBUG discovered.
Coordinator
Oct 7, 2013 at 1:45 PM
The ModelValidatorProvider will instantiate the validator once for each instance of the model type being validated, so if there's only 1 instance of ReportViewModel being validated, then the validator will only be created once. If this isn't the behaviour you're seeing, then I'm going to need more information to troubleshoot - perhaps you could put together a small code sample to reproduce the issue?
Oct 8, 2013 at 3:34 AM
Edited Oct 8, 2013 at 3:37 AM
the code run inside orchard project
ModelValidatorProviders.Providers.Clear();
            FluentValidationModelValidatorProvider.Configure(provider =>
            {
                provider.ValidatorFactory = new FluentValidatorFactory();
            });
 public class FluentValidatorFactory : AttributedValidatorFactory
    {
        //private readonly InstanceCache _cache = new InstanceCache();
        public override IValidator GetValidator(Type type)
        {
            if (type != null)
            {
                var attribute = (ValidatorAttribute)Attribute.GetCustomAttribute(type, typeof(ValidatorAttribute));
                if ((attribute != null) && (attribute.ValidatorType != null))
                {

                    var instance = Activator.CreateInstance(attribute.ValidatorType);
                    return instance as IValidator;
                }
            }
            return null;

        }
    }
 public class ReportViewModelValidator : AbstractValidator<ReportViewModel>
    {
        public ReportViewModelValidator()
        {
            RuleFor(m => m.Summary).NotEmpty().WithMessage("今日总结不能为空");
            RuleFor(m => m.NextPlan).NotEmpty().WithMessage("明日计划不能为空");
        }
    }
Coordinator
Oct 9, 2013 at 12:53 PM
I can't reproduce the problem - I used this code and it worked correctly. Perhaps you could put together a small sample project that reproduces the problem?

Jeremy