GithubHelp home page GithubHelp logo

andreykarinskiy / system.componentmodel.annotations.validation Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 2.79 MB

System.ComponentModel.Annotations.Validation is a extension of System.ComponentModel validation engine. Supports validation of properties and invariants for object graphs.

License: MIT License

Batchfile 1.51% C# 98.49%
componentmodel dataannotations fluentvalidation domain-driven-design validation validator invariants

system.componentmodel.annotations.validation's Introduction

System.ComponentModel.Annotations.Validation

Overview

System.ComponentModel.Annotations.Validation is a extension of System.ComponentModel validation engine. Supports validation of properties and invariants for object graphs.

This library helps in writing validation logic in OOP and DDD-style, when the data and methods that check them are in one place.

Validation rules can be implemented in three ways:

The library is fully compatible with System.ComponentModel.Annotations, uses common base types.

Features

  • Validation of an individual object (properties and methods).
  • Validation of the object graph, that is, several related links (including cyclical) objects.
  • Asynchronous validation TODO.
  • Reactive validation (IObservable interface implementation) TODO.
  • Invariant methods can process validation logic in various ways:
    • return single ValidationResult,
    • collection of ValidationResult,
    • true/false (error flag),
    • string (error message),
    • may throw an ValidationException exception instead of the return value
    • or arbitrary exception.
  • Adapter for FluentValidation library TODO.

Usage

Domain classes contain properties with validation attributes and invariant methods with more complex validation logic.

public class Engine
{
	[Required]
	[StringLength(15)]
	public string Name { get; set; }

	[Range(1, 24)]
	public int NumOfCylinders { get; set; }

	[Range(1000.0, 5000.0)]
	public double Cost { get; set; }

	[Invariant]
	private ValidationResult NameMustBeStartedWithCapitalize()
	{
		var firstSymbol = Model.First();
		if (!char.IsUpper(firstSymbol))
		{
			return new Fail($"'{firstSymbol}' is not uppercase.");
		}

		return Success();
	}

	[Invariant("The cost depends on the number of cylinders")]
        private IEnumerable<ValidationResult> CostDependsOnNumberOfCylinders()
	{
		if (NumOfCylinders == 4 && Cost < 1500)
		{
			yield return new Fail("4-cylinder engine cannot cost less than 1500!");
		}

		if (NumOfCylinders >= 12)
		{
			yield return new Fail("We do not serve tanks!");
		}
	}
}

Next, the validator checks the domain class instance.

var engine = new Engine();

IValidator validator = new ObjectValidator();

IEnumerable<ValidationResult> result = validator.Validate(engine);

Architecture

The library architecture is a classic OOP. The facade of the library, the ObjectValidator class, gets property accessors and method accessors (which contain class invariants) from the PropertyProvider and MethodProvider components, and then sends them to the PropertyValidator and MethodValidator components for checking.

The DefaultPropertyProvider class first uses TypePropertyProvider to get the property descriptors (PropertyInfo) of the class being validated, and then creates PropertyAccessor wrappers for them for quick access using PropertyAccessorFactory . Next, the resulting PropertyAccessors are checked using the PropertyValidator.

Methods with invariants are handled similarly.

The "Show project dependency diagram" command of the JetBrains ReSharper tool generates the following diagram:

Installation

Just install System.ComponentModel.Annotations.Validation from the package manager console:

PM> Install-Package ComponentModel.Annotations.Validation

If you need to use this library with FluentValidation library, you will also need to install the appropriate package containing the adapter for FluentValidation:

PM> Install-Package ComponentModel.Annotations.Validation.FluentValidation

Requirements

Depends to System.ComponentModel.Annotations.

Versioning

The project uses semantic versioning with GitVersion utility.

Questions and Problems?

If you have significant questions, problems related to this library usage or if you want to discuss new features, please send email to [email protected].

If you've discovered a bug, please report it to the GitHub Issues. Detailed reports with stack traces, actual and expected behaviors are welcome.

Build

Use Buid.cmd in root package for build process or Clean.cmd if you need to delete all binary files before archiving or copying.

After building and testing, script Build.cmd creates a NuGet-package and publishes it to the local NuGet-repository on the developer's machine. The repository path is specified at the beginning of Build.cmd file in the publishDir constant.

License

This project is licensed under the MIT License.

system.componentmodel.annotations.validation's People

Contributors

andreykarinskiy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

xtjatswc

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.