在两个项目解决方案中放置fluentvalidation验证器类的位置 [英] Where to place fluentvalidation validator class in a two project solution

查看:87
本文介绍了在两个项目解决方案中放置fluentvalidation验证器类的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案中有一个Web项目和一个类库项目。类库项目包含所有非标识数据的模型。我创建了一个验证器类来验证表中的唯一性(类别)。当我将Unique属性放入模型时,我得到无法找到类型或命名空间名称'CategoryValidator'...'。



这里是CategoryValidator类



I have a web project and a class library project in one solution. The class library project contains the models for all non-identity data. I have created a validator class to validate uniqueness in a table (Category). When I place the Unique attribute into the model, I get "The Type or namespace name 'CategoryValidator' could not be found...'.

Here is CategoryValidator class

namespace Spicy.Entities.Validators
{
    public class CategoryValidator : AbstractValidator<Category>
    {
        public CategoryValidator()
        {
            RuleFor(x => x.Name).NotNull().WithMessage("Category Name is required.").Must(UniqueName).WithMessage("This category name already exists.");
        }

        private bool UniqueName(Category category, string name)
        {
            using (RecipeContext db = new RecipeContext())
            {
                var dbCategory = db.Categories
                                .Where(x => x.Name.ToLower() == name.ToLower())
                                .SingleOrDefault();

                if (dbCategory == null)
                    return true;

                return dbCategory.ID == category.ID;
            }
        }
    }
}







这是使用System.Collections.Generic的类别模型






Here is the Category Model

using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Spicy.Entities
{
    [Validator(typeof(CategoryValidator))]
    public class Category
    {
        public int ID { get; set; }

        [StringLength(20, ErrorMessage = "Category cannot be longer than 20 characters")]
        [Required]

        public string Name { get; set; }

        [Required]
        [DisplayName("User Name")]
        [StringLength(20, ErrorMessage = "User Name cannot be longer than 20 characters")]
        public string UserName { get; set; }

        public virtual ICollection<Recipe> Recipes { get; set; }
    }





这是global.asax代码





Here is the global.asax code

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        FluentValidationModelValidatorProvider.Configure();
    }
}







我应该在哪里放置验证器类以及如何我能解决我遇到的问题吗?



帮助。



我是什么尝试过:



当我将验证器类放入类库项目时,它无法从Web项目访问上下文(RecipeContext)。如果我尝试将验证器类放入Web应用程序中,则会收到无法找到类型或命名空间名称'CategoryValidator'的错误...。我也尝试在两个项目中安装Fluent验证包。




Where should I put the validator class and how do I get around the issues I'm having?

help.

What I have tried:

When I place the validator class into the class library project, it cannot access the context (RecipeContext) from the web project. If I try placing the validator class into the web app, I get an error that 'The Type or namespace name ' CategoryValidator' could not be found...'. I have also tried installing Fluent Validation package in both projects.

推荐答案

Web项目需要对类库项目的项目引用。一旦完成,请查看您的代码;



The web project needs a project reference to the class library project. Once that's done look at your code;

[Validator(typeof(CategoryValidator))]





您要求它使用名为CategoryValidator的类型,但这是类名,而不是类型,类型实际上是





You're asking it to use a type called CategoryValidator but that is the class name, not the type, the type is actually

Spicy.Entities.Validators.CategoryValidator





类型是命名空间和类名。因此,您可以将代码更改为





The type is the namespace and the class name. So you could change your code to

[Validator(typeof(Spicy.Entities.Validators.CategoryValidator))]





或保留该行,并将以下using语句添加到类文件的顶部





Or leave that line as it is and add the following using statement to the top of the class file

using Spicy.Entities.Validators;





如果右键单击CategoryValidator文本(它它下面应该有一条波浪线)上下文菜单中有一个重构部分,可以为你做这些动作。



If you right-click on the CategoryValidator text (it should have a wavy line under it) there is a "Refactor" section of the context menu that will do either of these actions for you.


这篇关于在两个项目解决方案中放置fluentvalidation验证器类的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆