Also talk about entity verification (Entity Validation)

Source: Internet
Author: User
Keywords We user then the rules
Tags .net asp asp.net beginning check class code environment

Environment:

Windows 2008, VS 2008 SP1, asp.net MVC 1.0

------------------------------------------------------------------------------

Tea watching tonight, the mind suddenly flashed a thought, so open vs simple to do a realization, so with this article ... It's about entity validation, well, start from the beginning.

One. Probably the most original validation code

To make the problem as simple as possible, let's take a simple user class.

Now we're going to add a method to the user class to verify the legality of the user instance, and I believe a lot of people wrote the following code like this:

The Check () method simply applies a variety of rules to the properties of the user class, and then returns an illegal set of hints.

Validation is just a simple call to the entity check () method:

Well, I admit, it seems to be enough in general, but many people see a series of if may start to freak out, yes, I don't like it, then think of the most even way to eliminate them.

Two. An attempt based on the new characteristics of c#3.0

To eliminate if, consider linking those validation rules to a single line, so:

The validation extension methods for writing entity classes are as follows:

Then the check () method for the entity is changed to:

In this way, we eliminate the If by extending the method, but we link the validation rules and throw out the illegal information in an unusual way, so each validation can only get a single validation message, which is often not in line with the actual situation. Think about it.

Three. Continue to evolve

Then, to get a collection of validation error messages, improve:

To make the code look more like that, add a base class to the entity, called EntityBase.

Then write a validation class:

Then look at the modified check () method, and the validation class works at a glance:

No need to explain? In fact, it's also a chain, return the validation error message collection and see the results:

See here, you may say: "What, do not eliminate an If, what a big deal."

Uh ... Yes.. It's true ... Tricks, if there is a similarity, is purely coincidental.

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.