Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
            }
        }
    }
}



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; }
    }


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();
    }
}



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.
Posted
Updated 25-Apr-18 4:14am

1 solution

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

[Validator(typeof(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))]


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

using Spicy.Entities.Validators;


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.
 
Share this answer
 
Comments
Kim O 25-Apr-18 12:46pm    
Thank you. I have made the changes you suggested. For clarity: the Validators folder and CategoryValidator.cs is in the Entities project. I already have a project reference in the web project referencing the entities project. I have added the using statement. I am still having the issue that all references in the CategoryValidator class to the RecipeContext the error is "The type or namespace name 'RecipeContext' could not be found...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900