3

I am looking into implementing a form a Data Validation on at the Data Layer of my application. I am looking to accept a specific string input into my data model, but am struggling to know how best to implement this.

I am currently looking into whether Fluent API (currently testing EF Core flavor) will facilitate this functionality, or whether I need to consider bringing in Data Annotations into my Data Layer model to ensure the validation can be implemented on all Object Relational Modeling services.

From reading around online, Fluent API is the best course of action as it removes workload from the Data Layer and lets the Data Access layer control and specify the specific rules around how the data will be modeled.

The downside is that I would need to re-implement this for other ORM's (i.e.: NPoco) to meet the rules of this platform.

A bit more on the problem

I have several classes that will have foreign cultured names and strings, and they can be identified by a Language Culture String, using the ISO-639 and ISO-3166 combination. (en-GB, zh-CN)

This is represented by a String in the Data Model.

public abstract class Alias
{
    public string Culture { get; set; }
}

Under EF Core, I set up the tables using the OnModelCreating function.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    var authorAlias = modelBuilder.Entity<AuthorAlias>();
    authorAlias.Property("Given").IsRequired().IsUnicode();
    authorAlias.Property("Family").IsRequired().IsUnicode();
    authorAlias.Property("Middle").IsUnicode();

    base.OnModelCreating(modelBuilder);
}

I know .NET Core has the ability to pull down an array of CultureInfo which use the Localization String system I want to implement, but I do not intend the Data Model to check this but to let the Data Access layer validate the data.

How can I ensure that my intended logic can be implemented into the system? And would I need to change to Data Annotations to ensure this logic?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • 1
    Are you looking for something like this? [Validation in EF Core](http://www.bricelam.net/2016/12/13/validation-in-efcore.html) – bricelam Apr 05 '18 at 15:58
  • Kind of, but it requires the use of Data Annotiation for the IValidateObject to be used. Data Annotations are something I would like to avoid using, so I might have to take another approach. Maybe using a custom Interface that will do validation on the model directly, before it hits the database. As I am planning to allow various ORMs to be utilized, it may have to be safer no to use validation on the Access layer... –  Apr 06 '18 at 05:29

1 Answers1

1

This can be a late answer but can you make use of 'Fluent Validation' - A popular .NET library for building strongly-typed validation rules.

  1. Install the Nuget package "dotnet add package FluentValidation.AspNetCore"

  2. Create a model validator as below which extends 'AbstractValidator' and create Rules as needed.

    public class Employee
    {
        public string Name { get; set; }
        public string EmployeeId { get; set; }
    
    }
    
    public class EmployeeValidator:AbstractValidator<Employee>
    {
        public EmployeeValidator()
        {
            RuleFor(x => x.Name).NotEmpty().WithMessage("Employee Name can not be 
                    empty");
    
            RuleFor(x => x.EmployeeId).NotEmpty();  
        }
    }
    
  3. FluentValidation supports integration with ASP.NET Core 2.1 or 3.1 (3.1 recommended). Once enabled, MVC will use FluentValidation to validate objects that are passed in to controller actions by the model binding infrastructure.

More information on how this can be used for ASP.NET CORE can be found here - https://docs.fluentvalidation.net/en/latest/aspnet.html

RashmiMs
  • 129
  • 14