FluentValidation – build strongly-typed validation rules in .NET


Install the FluentValidation .NET library for validation.

Below is our sample Contact class and its corresponding validator class.

The validator class needs to inherit AbstractValidator<Contact> and define the validation rules in the constructor using the RuleFor method.

Instantiate the validator class, and pass the object to be validated.

Here we have passed incorrect values for all the properties of the contact.

The ValidationResult holds all the validation error details.

Give it a try – https://docs.fluentvalidation.net/en/latest/index.html

Hope it helps..

using FluentValidation;
using FluentValidation.Results;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FluentValidator
{
    class Program
    {
        static void Main(string[] args)
        {
            var myContact = new Contact();
            var contactValidator = new ContactValidator();

            myContact.FirstName = "";
            myContact.LastName = "Lei";
            myContact.Email = "aa.gcom";
            myContact.Age = 10;
            myContact.FamilyStatusCode = (FamilyStatusCode)Enum.Parse(typeof(FamilyStatusCode),"5");

            ValidationResult result = contactValidator.Validate(myContact);
        }
    }

    class Contact
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public int Age { get; set; }
        public FamilyStatusCode FamilyStatusCode { get; set; }
    }

    enum FamilyStatusCode
    {
        Single = 1,
        Married = 2,
        Divorced = 3,
        Widowed = 4
    }

    class ContactValidator : AbstractValidator<Contact>
    {
       public ContactValidator()
        {
            // First Name should not be null
            RuleFor(contact => contact.FirstName).NotEmpty();
            // Last Name should not be null and minimum length should be 4
            RuleFor(contact => contact.LastName).NotNull().MinimumLength(4);
            // Email Address Validation
            RuleFor(contact => contact.Email).EmailAddress();
            // Age should be between 18 to 60
            RuleFor(contact => contact.Age).ExclusiveBetween(18, 60);
            // Family Status Code Enum should be a valid value
            RuleFor(contact => contact.FamilyStatusCode).IsInEnum();

        }
    }
}
Advertisements

Fixed – “The application requires one of the following versions of the .NET Framework” or “.NET Framework 4.7.1 or later is already installed this computer” error while running application


While trying to run an application we were getting the below message

It was taking us to Microsoft Download site for downloading the framework.

We downloaded the 4.7.1 Framework but while trying to install it we got the below message that said the framework or a later update is already installed on this computer.

Tried uninstalling all the .NET Framework version installed including 4.7.

That also didn’t work.

Few of the forums talk about making changes in the registry, that also didn’t help.

Eventually we commented the supportedRuntime element in the configuration file of the application to get through this issue.

https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/startup/supportedruntime-element

https://stackoverflow.com/questions/21566528/what-happens-if-i-remove-the-auto-added-supportedruntime-element

Hope it helps..

Advertisements

Literal Improvement in C# 7.0


C# 7.0 now allows using underscore _ to be used a digit separator.

This adds more readability.

Similarly, we now have Binary Literals, so instead of specifying hexadecimal pattern we can specify bit patterns.

Hope it helps..

Using Out Variables (C# 7.0) instead of Out Parameters


Out Parameters must need to declared before they are used in the method which doesn’t seem intuitive.

Now with C# 7.0 we can now make use of Out variable.

Here in our second call to the method we have used out variable named outVariableString which we didn’t have to declare like output parameter.

Hope it helps..

Tuple Types and Tuple Literals in C# 7.0


Tuple are used when we need to get more than one value returned from the method.

C# 7.0 introduces Tuple Types and Tuple Literals to Tuples

We need to add System.ValueType Nuget Package to add the required assemblies to Visual Studio.

Below sample code shows usage of Tuple type and literal. In our second method, we have named the part of the tuple i.e. name and number.

Now to access the first method we use Item1 and Item2 field, however for our second method as we have named the part we can use those names to access them i.e. name and number.

Output in both the case

Hope it helps..

New Feature – Local Functions in C# 7.0


C# 7.0 adds a new feature called local functions.

Local functions allow us to define function within a function. It can be used to write helper methods that we need to use inside that function’s scope.

Here in the below sample code we have defined one main function (MainFunction) that has one local function(MultiplyBy10LocalFunction) defined inside it. And that local function has one more local function(Add10LocalFunction) defined inside it.

Hope it helps..

%d bloggers like this: