Micro Project Practice (4): Common Layer Code Analysis

Source: Internet
Author: User
Tags assert

In the previous article, we generated the entity code, joined the business and common, the project suddenly became big, now we have a little analysis.

Let's start with this system chart. Let's look at common this DLL, which contains classes, as shown in the following figure.

1, verify the related class (ivalidatable,validater,validateresult,validatefailexception)

These four classes are used primarily at the business level to validate Entity data, and each entity (Entity) validates its own data when it saves data to ensure that illegal data is never saved to the database, and that these four classes serve this feature. The validator (Validater) is the core of these four classes and is responsible for the implementation of the validation. The following test code (located in Dongblog.test\common\validate\validatetest.cs) shows the typical use of this class, that is, through the addcondition Add the validation result (an instance of the Validateresult Class) using the Validate method, adding the condition to be validated (usually an expression) and the error message that is displayed when the validation fails:

1: [TestMethod (), Description ("Validation failed test")]
2:public void Validatefailtest ()
3: {
4:validater Validater = new Validater ()
5:. Addcondition (True, "Error1")
6:. Addcondition (False, "Error2")
7:.  Addcondition (False, "Error3");
8:
9:validateresult validateresult = validater. Validate ();
10:assert.isfalse (validateresult.isvalidated);
One:
//validate error message
13:assert.areequal<int> (2, validateResult.ErrorMessage.Length); 
   14:assert.areequal<string> ("Error2", validateresult.errormessage[0));
15:assert.areequal<string> ("Error3", validateresult.errormessage[1]);
16:}

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.