关于Microsoft的CompositeUI应用程序块中的Guard类的问题

时间:2010-09-28 08:02:52

标签: .net

是否有人在其开源项目CompositeUI Application Block中使用Microsoft提供的Guard类? 我注意到,在Guard类中,只有一些方法可以进行参数验证。但是我们都知道,在我们的自我代码中,可能有许多情况需要验证其他变量(不是参数参数)或成员,并且如果验证失败则抛出异常。对于这种情况,为什么Guard类没有提供帮助这样做的辅助类呢?

以下是Guard类的代码:

//===============================================================================
// Microsoft patterns & practices
// CompositeUI Application Block
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Microsoft.Practices.CompositeUI.Utility
{
 /// <summary>
 /// Common guard clauses
 /// </summary>
 public static class Guard
 {
  /// <summary>
  /// Checks a string argument to ensure it isn't null or empty
  /// </summary>
  /// <param name="argumentValue">The argument value to check.</param>
  /// <param name="argumentName">The name of the argument.</param>
  public static void ArgumentNotNullOrEmptyString(string argumentValue, string argumentName)
  {
   ArgumentNotNull(argumentValue, argumentName);

   if (argumentValue.Length == 0)
    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.StringCannotBeEmpty, argumentName));
  }

  /// <summary>
  /// Checks an argument to ensure it isn't null
  /// </summary>
  /// <param name="argumentValue">The argument value to check.</param>
  /// <param name="argumentName">The name of the argument.</param>
  public static void ArgumentNotNull(object argumentValue, string argumentName)
  {
   if (argumentValue == null)
    throw new ArgumentNullException(argumentName);
  }

  /// <summary>
  /// Checks an Enum argument to ensure that its value is defined by the specified Enum type.
  /// </summary>
  /// <param name="enumType">The Enum type the value should correspond to.</param>
  /// <param name="value">The value to check for.</param>
  /// <param name="argumentName">The name of the argument holding the value.</param>
  public static void EnumValueIsDefined(Type enumType, object value, string argumentName)
  {
   if (Enum.IsDefined(enumType, value) == false)
    throw new ArgumentException(String.Format(CultureInfo.CurrentCulture,
     Properties.Resources.InvalidEnumValue, 
     argumentName, enumType.ToString()));
  }

  /// <summary>
  /// Verifies that an argument type is assignable from the provided type (meaning
  /// interfaces are implemented, or classes exist in the base class hierarchy).
  /// </summary>
  /// <param name="assignee">The argument type.</param>
  /// <param name="providedType">The type it must be assignable from.</param>
  /// <param name="argumentName">The argument name.</param>
  public static void TypeIsAssignableFromType(Type assignee, Type providedType, string argumentName)
  {
   if (!providedType.IsAssignableFrom(assignee))
    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
     Properties.Resources.TypeNotCompatible, assignee, providedType), argumentName);
  }
 }
}

1 个答案:

答案 0 :(得分:0)

Guard类的目的是在使用它们之前测试它们。

例如:

public void Store(string ImageName, Image Image)
{
     Guard.ArgumentNotNullOrEmptyString(ImageName, Resources.ImageName);
     Guard.ArgumentNotNull(Image, Resources.Image);

     LocalStore(ImageName, Image);

     if (NextStore != null)
     {
         NextStore.Store(ImageName, Image);          
     }
}

因此,保护​​声明确保两个参数都符合方法体的假设。

注意:如果您不想使用Guard模式,您应该提供您尝试实现的示例(代码或描述),而不是发布Guard类代码。