Implement an object verification Library Series (highly criticized ),

Source: Internet
Author: User

Implement an object verification Library Series (highly criticized ),
Reason

The reason for writing this series is that a small library for object verification was implemented before the National Day,

I also hope that the big gods in my blog will criticize and educate me on this library, so that I can grow up.

However, since I didn't introduce the library content and my ideas, I just gave the code,

Great gods are not very interested in this,

So recently I will write a series of simple introductory articles to illustrate the content of this library.

I hope you will be able to be seen and criticized by the gods.

Series directory

0) Introduction to directories and library Structures

1) interface Introduction and Overview

2) validators

3) Fluent and extension method implementation

 

Body of this article: database structure introduction (0) database Introduction

I imagine that the database users can use Fluent to create object verification rules,

In addition, the Library does not involve the special code of different. net device platforms, so I use Portable to write the library.

The usage code of the hypothetical library is as follows:

Container. init (); // Only need init in your app once, with the aim of binding type and interface var builder = Validation. newValidatorBuilder <Student> (); // create the verification creator B. ruleFor (I => I. age ). must (I => I> = 0 & I <= 18 ). overrideName ("student age "). overrideError ("not student "). thenRuleFor (I => I. name ). must (I =>! String. isNullOrWhiteSpace (I )). overrideName ("student name "). overrideError ("no name"); // sets the validation rule var v = builder. build (); // use the Creator to create the validators var student = new Student () {Age = 13, Name = "v"}; // verify the object data var context = Validation. createContext (student); // create a verification context because the context contains the execution-related parameter var result = v. validate (context); // use the validator to verify the student data Assert. isNotNull (result); Assert. true (result. isValid); Assert. true (result. failures. count = 0); // compare and test the verification result
(1) database code structure

The project structure is as follows:

UnitTest: Unit test code (skip this section)

ObjectValidater: The Library only has this project

  • Base:Verification Rules, rule creators, rule selectors, validators, and other actual implementation code
  • Checkers:Implementation of various verification methods
  • Common:It has nothing to do with the business definition or logic of the database, and provides convenient implementation of public extension methods or classes.
  • Entities:Verification context, verification result Definition
  • Interfaces:Definition of various interfaces
  • Container class:Used for decoupling interface and implementation Type Binding to set static classes
  • Syntax class:Provides the extension method definition for Fluent usage.
  • ValidationClass:To decouple interfaces from actual implementations, you can only provide the created static classes instead of new objects.
(2) Project dependency

Currently, this library only relies on Autofac. Due to the decoupling interface and specific implementation:

  <package id="Autofac" version="3.5.2" targetFramework="portable-net40+sl50+win80+wp80+wpa81" />

 

 

NEXT: 1) interface Introduction and Overview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.