In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to implement entity verification in C#. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Environment:
Windows 2008, VS 2008 SP1, Asp.Net Mvc 1.0
one。 Probably the most original verification code.
To keep the problem as simple as possible, let's start with a User class that is as simple as possible.
Now we want to add a method to the User class to verify the validity of the User instance. I believe many people have written code similar to the following:
The Check () method simply applies various rules to the properties of the User class and then returns an illegal set of prompt information.
To verify, simply call the entity's Check () method:
Well, I admit, in general, this method seems to be enough, but a lot of people may start to freak out when they see a series of if. Yeah, I don't like it either, so come up with the most even way to eliminate them.
two。 An attempt based on the New Features of Craft 3.0
To eliminate if, consider chaining those validation rules together with a line, so:
The validation extension method for writing entity classes is as follows:
Then the Check () method of the entity is changed to:
In this way, we eliminate if by extending the method, but we chain up the verification rules and throw entity illegal information in an abnormal way, so we can only get one piece of verification information per validation, which is obviously not in line with the actual situation. Think about; think about.
three。 Continue to evolve
In order to get a collection of validation error messages, improve:
To make the code look more like that, add a base class to the entity, which is called EntityBase.
Then write a validation class:
Then take a look at the modified Check () method, and the purpose of this validation class is clear at a glance:
You don't have to explain, right? In fact, it is also chained together and returns a collection of validation error messages to see the result:
Thank you for reading! This is the end of this article on "how to achieve entity verification in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Use the Description property to get a short error description
© 2024 shulou.com SLNews company. All rights reserved.