Util application Framework Common operations Class (VI): Validation extension

Source: Internet
Author: User

The basic operation of warehousing is described earlier, the following is ready to begin to expand the query, before the expansion of the query, the first to add two public operation classes, one is the most commonly used authentication method, and the other is the lambda expression operation class.

Many times, we will determine whether an object is NULL, because NULL is an unacceptable value, it causes a "object reference not set to an instance of an object" critical error, so when a null value is detected, the ArgumentNullException exception is generally thrown directly.

 Public void string name) {    ifnull  )          Throw New " name "  );     // Other Actions }    

Because it is a frequent operation to judge Null, an extension method can be used to encapsulate it, called as follows.

 Public void string name) {    "name"  );     // Other Actions }

I extend the Checknull directly to object objects, because most objects require this operation. Note that extending object is very discreet and can cause large areas of pollution, because all objects will see this extension method, and if this action has side effects on some objects, it will cause more confusion.

Another common method is to determine if it is empty, such as the string "", or the GUID of the Guid.Empty.

For string, we generally pass string. Isnullorwhitespace to make judgments.

 Public void string name) {    ifstring. Isnullorwhitespace (name))        return;     // Other Actions }

After encapsulation using the extension method, simplify to the following code.

 Public void string name) {    if  (name). IsEmpty ())        return;     // Other Actions }

IsEmpty extension method, I define the string, GUID, GUID, and other specific types, cannot be extended to object, because each type of implementation is different, of course, can make a variety of judgments, but the execution efficiency may be very low, after all, this is a common method.

Add the extensions.validate file to the Util project, which is a partial class of Extensions , as shown in the code below.

usingSystem;namespaceUtil {/// <summary>    ///Validating extensions/// </summary>     Public Static Partial classExtensions {/// <summary>        ///null value is detected, ArgumentNullException exception is thrown/// </summary>        /// <param name= "obj" >Object</param>        /// <param name= "ParameterName" >Name of parameter</param>         Public Static voidChecknull ( This ObjectObjstringparametername) {            if(obj = =NULL )                Throw NewArgumentNullException (parametername); }        /// <summary>        ///is empty/// </summary>        /// <param name= "value" >value</param>         Public Static BOOLIsEmpty ( This stringvalue) {            return string.        Isnullorwhitespace (value); }        /// <summary>        ///is empty/// </summary>        /// <param name= "value" >value</param>         Public Static BOOLIsEmpty ( ThisGuid?value) {            if(Value = =NULL )                return true; returnIsEmpty (value.        Value); }        /// <summary>        ///is empty/// </summary>        /// <param name= "value" >value</param>         Public Static BOOLIsEmpty ( ThisGuid value) {            if(Value = =guid.empty)return true; return false; }    }}

  The unit Test code is as follows.

usingSystem;usingMicrosoft.VisualStudio.TestTools.UnitTesting;namespaceUtil.Tests.Extensions {/// <summary>    ///Verifying extension tests/// </summary>[TestClass] Public classValidateextensiontest {/// <summary>        ///check for null, not empty, normal execution/// </summary>[TestMethod] Public voidTestchecknull () {varTest =New Object(); Test. Checknull ("Test" ); }        /// <summary>        ///checks for null, and a value of NULL throws an exception/// </summary>[TestMethod] [ExpectedException (typeof(ArgumentNullException))]  Public voidTestchecknull_null_throw () {Try {                ObjectTest =NULL; Test. Checknull ("Test" ); }            Catch(ArgumentNullException ex) {Assert.istrue (ex). Message.contains ("Test"), ex.                Message); Throw; }        }        /// <summary>        ///test whether null value/// </summary>[TestMethod] Public voidtestisempty_string () {stringValue =NULL; Assert.istrue (value.            IsEmpty ()); Assert.istrue ("".            IsEmpty ()); Assert.istrue (" ".            IsEmpty ()); Assert.isfalse ("a".        IsEmpty ()); }        /// <summary>        ///test whether null value/// </summary>[TestMethod] Public voidTestisempty_guid () {Guid value=Guid.Empty; Assert.istrue (value.            IsEmpty ()); Value=Guid.NewGuid (); Assert.isfalse (value.        IsEmpty ()); }        /// <summary>        ///test whether null value/// </summary>[TestMethod] Public voidtestisempty_guid_nullable () {Guid? Value =NULL; Assert.istrue (value.            IsEmpty ()); Value=Guid.Empty; Assert.istrue (value.            IsEmpty ()); Value=Guid.NewGuid (); Assert.isfalse (value.        IsEmpty ()); }    }}

This article briefly introduces the extension of two verification methods, and the next article encapsulates the operation of a lambda expression, which is the basis for the IQueryable core where method extension.

I have added the following two code files in the downloaded code, which is interesting to download.

. NET Application Framework Exchange QQ Group: 386092459, welcome interested friends to join the discussion.

Thank you for your continued attention, my blog address: http://www.cnblogs.com/xiadao521/

: Http://files.cnblogs.com/xiadao521/Util.2014.12.22.1.rar

Util application Framework Common operations Class (VI): Validation extension

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.