GithubHelp home page GithubHelp logo

karenpayneoregon / teaching-simple-validation-1 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 955 KB

data annotations for windows forms with error provider using net8/C#

C# 100.00%
csharp-core csharp-library dataannotations dotnet8

teaching-simple-validation-1's Introduction

Validating application data with Data Annotations

Validation is the first and most important step in securing an application. It prevents the application from processing unwanted inputs that may produce unpredictable results. Couple validation with properly transmitting data to a data source.

When validating data should there be instant feedback? This is subjective, instant feedback will be better when there are many inputs so this would be better than waiting to submit their input. In the case of instant feedback there needs to be events triggered to perform validation while on submit there is a central generic method to perform validation.

Let's take another view of validating on submit, with all types of application which can be created with Visual Studio there are methods which permit displaying messages to assist the user to correct mistakes. So back to subjective, it may come down to if code is written in a team or individual in regards to, are there team rules or it's the Wild West.

Data Annotations

Data Annotations are nothing but a set of attributes which can be used to configure your model classes to validate the input data entered by users. It provides a set of .NET attributes that can be applied to data object class properties. These attributes offer a very declarative way to apply validation rules directly to a model. Data Annotations are a general purpose mechanism which can be used to feed metadata to the framework. Framework drives validation from the metadata, and uses the metadata while building the HTML to display and edit models. Well, you can manually validate the view model is ASP.NET Core and Windows Forms too, but using data annotation makes you validation logic reusable and also saves time. In addition, it also minimizes the complexity in the action methods. The idea is to add constraints metadata to the properties in the view model (or in Windows Forms), which can be later picked up by the default binder in the model-binding process.

Project Description
ValidatorLibrary Base methods needed for validating a model with data annotations which matches code in BaseDataValidationLibrary without rules and extras past performing a model validaton.
ValidatorLibraryTestProject unit test for ValidatorLibrary project
RulesLibrary This project holds any data annotation rules which are common to a team or individual developer's work.
RulesUnitTestProject Unit test for RulesLibrary
BaseDataValidatorLibrary For teaching, for real work use ValidatorLibrary/RulesLibrary
EntityFrameworkCoreLibrary For use with data annotation validations in CustomerEntityFrameworkTestProject.
CustomerEntityFrameworkTestProject Unit testfor EntityFrameworkCoreLibrary
DirectoryHelpersLibrary Provides methods to walk backwards in a directory structure.
AnnotationValidationLibrary For teaching
ValidatingFormProject Windows form project to try out validating a model

Besides provided annotations developers can create their own like shown below for validating the year value of a DateTime property in a model.

[YearRange(maximumYear: 2022, MinimumYear = 1932)]

Year Range

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class YearRangeAttribute : ValidationAttribute
{
    public YearRangeAttribute(int maximumYear)
    {
        MaximumYear = maximumYear;
    }
    /// <summary>
    /// Maximum permitted year
    /// </summary>
    public int MaximumYear { get; }
    /// <summary>
    /// Minimum permitted year
    /// </summary>
    public int MinimumYear { get; set; }
    public override string FormatErrorMessage(string name)
    {
        if (ErrorMessage is null && ErrorMessageResourceName is null)
        {
            ErrorMessage = "'Year {0}' and/or year '{1}' are invalid";
        }

        return $"{name} year must be between {MinimumYear} and {MaximumYear}";

    }

    public override bool IsValid(object sender) 
        => ((DateTime)sender).Year.Between(MinimumYear, MaximumYear);
}

teaching-simple-validation-1's People

Contributors

karenpayneoregon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.