Blogs

Gary's Blog

XAF 9.3 Sneak Peak – The Guard Class

     

XAF has a new guard, and we’re not talking Kobe Bryant either! Nope, we’re talking about the new Guard class coming in XAF 9.3, a class to help enforce code contracts.

So how does it work? Well just look at this piece of code:

public void MyMethod(object myObject, string message)
{
    if (myObject.GetType() != typeof(MyType))
    {
        throw new ArgumentException();
    }
    if (myObject == null)
    {
        throw new ArgumentNullException();
    }
    else
    {
        //...custom logic here
        if (!string.IsNullOrEmpty(message))
        {
            //...custom logic here
        }
    }
}

Checking the arguments throughout the method body like this isn’t best practice, it clutters the method and makes the important business logic less visible. Now, contrast that with what the same method could look like using the forthcoming Guard class:

public void MyMethod(object myObject, string message)
{
    Guard.ArgumentNotNull(myObject, "myObject");
    Guard.ArgumentNotNull(message, "message");
    Guard.ArgumentIs(typeof(MyType), myObject, "myObject");
    //...custom logic here                
}

This version makes the code much more readable and, to a certain extend, self documenting. The following table shows the other methods that will be available in the Guard class:

MethodDescription
ArgumentIs Ensures that an argument has a specific type.
ArgumentNotNull Ensures that a specific argument isn't a null reference.
ArgumentNotNullOrEmpty Ensures that a specific string argument isn't a null reference and isn't an empty string.
CheckObjectFromObjectSpace Ensures that a specific object belongs to a particular Object Space.
CreateArgumentOutOfRangeException Initializes a new ArgumentOutOfRangeException class with the specified argument name and value.
NotDisposed Ensures that a specific object hasn't been disposed.
TypeArgumentIs Ensures that an argument has a specific type.

That’s it for this post, but keep an eye out for the other sneak peak posts from DevEpress’ evangelists.

Published Oct 20 2009, 10:40 AM by Gary Short (DevExpress)
Filed under:
Technorati tags: Sneak Peak
Bookmark and Share

Comments

 

Robert O'Brien said:

Nice. Couldn't we have a utility class like this for those of us who don't use XAF?

October 20, 2009 6:28 AM
 

MESUT KOSUCU said:

Hello,

cool stuff... Also, is there any news regarding reminder support for scheduler  module which is deadly important for such a cool framework.

October 20, 2009 6:45 AM
 

CA Team Member said:

How is it different from Debug.Assert method?

October 20, 2009 8:41 AM
 

Ronnie Murphy said:

Ben, you could try Code Contracts from MS http://tiny.cc/6EliQ

It does run-time checking (and static checking for VSTS)

October 20, 2009 10:27 AM
 

Andrew Miller said:

Nitpick: The verbose sample is broken...the check for null should precede the check for type.

October 20, 2009 10:59 AM
 

Travis Illig said:

Does it come with the associated FxCop rules so you can keep allowing FxCop to verify that you checked arguments for null, etc.?

October 20, 2009 11:11 AM
 

Manel Fernandez said:

Cool!

Is it based on the Microsoft Code Contracts? I've seen some demos of it and it's pretty amazing.

msdn.microsoft.com/.../dd491992.aspx

October 20, 2009 1:54 PM
 

Gary L Cox Jr [DX-Squad] said:

CA Team Member: Debug.Assert is meant for debugging your code, the Guard class will perform the check against your object and throw an exception for you at runtime.

October 20, 2009 1:55 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.