Skip to content

ApplicationGuard

La clase ApplicationGuard proporciona métodos de extensión diseñados para implementar cláusulas de guardia en la capa de aplicación. Estos métodos permiten validar argumentos, manejar excepciones de manera centralizada y reforzar la robustez del código al evitar estados inválidos.

ApplicationGuard está alineada con los principios de la arquitectura hexagonal, permitiendo a los desarrolladores realizar validaciones específicas de negocio según los requerimientos de cada caso de uso. Esto garantiza que las reglas de negocio se mantengan coherentes y desacopladas de la lógica de infraestructura.

namespace CodeDesignPlus.Net.Exceptions.Guards;
/// <summary>
/// Provides guard clauses for application layer validations.
/// </summary>
public static class ApplicationGuard
{
/// <summary>
/// Throws an exception if the specified value is null.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="error">The error message to include in the exception if the value is null.</param>
public static void IsNull(object value, string error) => Guard.IsNull(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is not null.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="error">The error message to include in the exception if the value is not null.</param>
public static void IsNotNull(object value, string error) => Guard.IsNotNull(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified string is null or empty.
/// </summary>
/// <param name="value">The string to check.</param>
/// <param name="error">The error message to include in the exception if the string is null or empty.</param>
public static void IsNullOrEmpty(string value, string error) => Guard.IsNullOrEmpty(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified string is not null or empty.
/// </summary>
/// <param name="value">The string to check.</param>
/// <param name="error">The error message to include in the exception if the string is not null or empty.</param>
public static void IsNotNullOrEmpty(string value, string error) => Guard.IsNotNullOrEmpty(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified string is null or whitespace.
/// </summary>
/// <param name="value">The string to check.</param>
/// <param name="error">The error message to include in the exception if the string is null or whitespace.</param>
public static void IsNullOrWhiteSpace(string value, string error) => Guard.IsNullOrWhiteSpace(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified string is not null or whitespace.
/// </summary>
/// <param name="value">The string to check.</param>
/// <param name="error">The error message to include in the exception if the string is not null or whitespace.</param>
public static void IsNotNullOrWhiteSpace(string value, string error) => Guard.IsNotNullOrWhiteSpace(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is true.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="error">The error message to include in the exception if the value is true.</param>
public static void IsTrue(bool value, string error) => Guard.IsTrue(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is false.
/// </summary>
/// <param name="value">The value to check.</param>
/// <param name="error">The error message to include in the exception if the value is false.</param>
public static void IsFalse(bool value, string error) => Guard.IsFalse(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is not greater than the comparison value.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="compare">The comparison value.</param>
/// <param name="error">The error message to include in the exception if the value is not greater than the comparison value.</param>
public static void IsGreaterThan<T>(T value, T compare, string error) where T : IComparable<T> => Guard.IsGreaterThan(value, compare, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is not greater than or equal to the comparison value.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="compare">The comparison value.</param>
/// <param name="error">The error message to include in the exception if the value is not greater than or equal to the comparison value.</param>
public static void IsGreaterThanOrEqual<T>(T value, T compare, string error) where T : IComparable<T> => Guard.IsGreaterThanOrEqual(value, compare, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is not less than the comparison value.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="compare">The comparison value.</param>
/// <param name="error">The error message to include in the exception if the value is not less than the comparison value.</param>
public static void IsLessThan<T>(T value, T compare, string error) where T : IComparable<T> => Guard.IsLessThan(value, compare, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is not less than or equal to the comparison value.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="compare">The comparison value.</param>
/// <param name="error">The error message to include in the exception if the value is not less than or equal to the comparison value.</param>
public static void IsLessThanOrEqual<T>(T value, T compare, string error) where T : IComparable<T> => Guard.IsLessThanOrEqual(value, compare, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified values are not equal.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="compare">The comparison value.</param>
/// <param name="error">The error message to include in the exception if the values are not equal.</param>
public static void AreEquals<T>(T value, T compare, string error) where T : IComparable<T> => Guard.AreEquals(value, compare, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified values are equal.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="compare">The comparison value.</param>
/// <param name="error">The error message to include in the exception if the values are equal.</param>
public static void AreNotEquals<T>(T value, T compare, string error) where T : IComparable<T> => Guard.AreNotEquals(value, compare, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is not within the specified range.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="min">The minimum value of the range.</param>
/// <param name="max">The maximum value of the range.</param>
/// <param name="error">The error message to include in the exception if the value is not within the range.</param>
public static void IsInRange<T>(T value, T min, T max, string error) where T : IComparable<T> => Guard.IsInRange(value, min, max, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified value is within the specified range.
/// </summary>
/// <typeparam name="T">The type of the values to compare.</typeparam>
/// <param name="value">The value to check.</param>
/// <param name="min">The minimum value of the range.</param>
/// <param name="max">The maximum value of the range.</param>
/// <param name="error">The error message to include in the exception if the value is within the range.</param>
public static void IsNotInRange<T>(T value, T min, T max, string error) where T : IComparable<T> => Guard.IsNotInRange(value, min, max, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified collection is empty.
/// </summary>
/// <typeparam name="T">The type of the elements in the collection.</typeparam>
/// <param name="value">The collection to check.</param>
/// <param name="error">The error message to include in the exception if the collection is empty.</param>
public static void IsEmpty<T>(IEnumerable<T> value, string error) => Guard.IsEmpty(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified collection is not empty.
/// </summary>
/// <typeparam name="T">The type of the elements in the collection.</typeparam>
/// <param name="value">The collection to check.</param>
/// <param name="error">The error message to include in the exception if the collection is not empty.</param>
public static void IsNotEmpty<T>(IEnumerable<T> value, string error) => Guard.IsNotEmpty(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified GUID is empty.
/// </summary>
/// <param name="value">The GUID to check.</param>
/// <param name="error">The error message to include in the exception if the GUID is empty.</param>
public static void GuidIsEmpty(Guid value, string error) => Guard.GuidIsEmpty(value, Layer.Application, error);
/// <summary>
/// Throws an exception if the specified GUID is not empty.
/// </summary>
/// <param name="value">The GUID to check.</param>
/// <param name="error">The error message to include in the exception if the GUID is not empty.</param>
public static void GuidIsNotEmpty(Guid value, string error) => Guard.GuidIsNotEmpty(value, Layer.Application, error);
}

Métodos de Extensión

La clase ApplicationGuard proporciona los siguientes métodos de extensión para realizar validaciones en la capa de aplicación, utilizando códigos de error estructurados.

IsNull

Type: public static void IsNull(object value, string error)

Lanza una excepción si el valor especificado es nulo.

public void ProcesarUsuario(Usuario usuario)
{
ApplicationGuard.IsNull(usuario, "201: El usuario no puede ser nulo.");
// ... lógica para procesar el usuario ...
}

IsNotNull

Type: public static void IsNotNull(object value, string error)

Lanza una excepción si el valor especificado no es nulo.

public void AsignarId(Usuario usuario)
{
ApplicationGuard.IsNotNull(usuario.Id, "202: El Id del usuario no puede ser nulo.");
// ... lógica para asignar el ID ...
}

IsNullOrEmpty

Type: public static void IsNullOrEmpty(string value, string error)

Lanza una excepción si la cadena especificada es nula o vacía.

public void EnviarMensaje(string mensaje)
{
ApplicationGuard.IsNullOrEmpty(mensaje, "203: El mensaje no puede estar vacío.");
// ... lógica para enviar el mensaje ...
}

IsNotNullOrEmpty

Type: public static void IsNotNullOrEmpty(string value, string error)

Lanza una excepción si la cadena especificada no es nula o vacía.

public void EstablecerNombre(string nombre)
{
ApplicationGuard.IsNotNullOrEmpty(nombre, "204: El nombre no puede ser nulo o vacío.");
// ... lógica para establecer el nombre ...
}

IsNullOrWhiteSpace

Type: public static void IsNullOrWhiteSpace(string value, string error)

Lanza una excepción si la cadena especificada es nula o contiene solo espacios en blanco.

public void ValidarDireccion(string direccion)
{
ApplicationGuard.IsNullOrWhiteSpace(direccion, "205: La dirección no puede estar vacía o contener solo espacios en blanco.");
// ... lógica para validar la dirección ...
}

IsNotNullOrWhiteSpace

Type: public static void IsNotNullOrWhiteSpace(string value, string error)

Lanza una excepción si la cadena especificada no es nula o no contiene solo espacios en blanco.

public void ProcesarCodigo(string codigo)
{
ApplicationGuard.IsNotNullOrWhiteSpace(codigo, "206: El código no puede ser nulo o solo espacios en blanco.");
// ... lógica para procesar el código ...
}

IsTrue

Type: public static void IsTrue(bool value, string error)

Lanza una excepción si el valor especificado es verdadero.

public void ValidarCompra(bool isValido)
{
ApplicationGuard.IsTrue(isValido == false , "207: La compra no es valida.");
// ... lógica para validar la compra ...
}

IsFalse

Type: public static void IsFalse(bool value, string error)

Lanza una excepción si el valor especificado es falso.

public void ProcesarPago(bool pagoRealizado)
{
ApplicationGuard.IsFalse(pagoRealizado == true, "208: El pago no se ha realizado correctamente.");
// ... lógica para procesar el pago ...
}

IsGreaterThan

Type: public static void IsGreaterThan<T>(T value, T compare, string error) where T : IComparable<T>

Lanza una excepción si el valor especificado no es mayor que el valor de comparación.

public void ValidarEdad(int edad)
{
ApplicationGuard.IsGreaterThan(edad, 18, "209: La edad debe ser mayor que 18.");
// ... lógica para validar la edad ...
}

IsGreaterThanOrEqual

Type: public static void IsGreaterThanOrEqual<T>(T value, T compare, string error) where T : IComparable<T>

Lanza una excepción si el valor especificado no es mayor o igual que el valor de comparación.

public void ValidarDescuento(decimal descuento)
{
ApplicationGuard.IsGreaterThanOrEqual(descuento, 0, "210: El descuento no puede ser menor a 0.");
// ... lógica para validar el descuento ...
}

IsLessThan

Type: public static void IsLessThan<T>(T value, T compare, string error) where T : IComparable<T>

Lanza una excepción si el valor especificado no es menor que el valor de comparación.

public void ValidarCantidad(int cantidad)
{
ApplicationGuard.IsLessThan(cantidad, 100, "211: La cantidad no puede ser mayor a 100.");
// ... lógica para validar la cantidad ...
}

IsLessThanOrEqual

Type: public static void IsLessThanOrEqual<T>(T value, T compare, string error) where T : IComparable<T>

Lanza una excepción si el valor especificado no es menor o igual que el valor de comparación.

public void ValidarPeso(decimal peso)
{
ApplicationGuard.IsLessThanOrEqual(peso, 100, "212: El peso no puede ser mayor a 100.");
// ... lógica para validar el peso ...
}

AreEquals

Type: public static void AreEquals<T>(T value, T compare, string error) where T : IComparable<T>

Lanza una excepción si los valores especificados no son iguales.

public void ValidarClave(string clave, string claveConfirmada)
{
ApplicationGuard.AreEquals(clave, claveConfirmada, "213: Las claves no coinciden.");
// ... lógica para validar la clave ...
}

AreNotEquals

Type: public static void AreNotEquals<T>(T value, T compare, string error) where T : IComparable<T>

Lanza una excepción si los valores especificados son iguales.

public void ValidarId(Guid id, Guid newId)
{
ApplicationGuard.AreNotEquals(id, newId, "214: Los ID son iguales");
// ... logica para validar el id
}

IsInRange

Type: public static void IsInRange<T>(T value, T min, T max, string error) where T : IComparable<T>

Lanza una excepción si el valor especificado no está dentro del rango especificado.

public void ValidarNota(int nota)
{
ApplicationGuard.IsInRange(nota, 0, 10, "215: La nota debe estar entre 0 y 10.");
// ... lógica para validar la nota ...
}

IsNotInRange

Type: public static void IsNotInRange<T>(T value, T min, T max, string error) where T : IComparable<T>

Lanza una excepción si el valor especificado está dentro del rango especificado.

public void ValidarTemperatura(int temperatura)
{
ApplicationGuard.IsNotInRange(temperatura, 10, 30, "216: La temperatura no debe estar entre 10 y 30 grados.");
// ... lógica para validar la temperatura ...
}

IsEmpty

Type: public static void IsEmpty<T>(IEnumerable<T> value, string error)

Lanza una excepción si la colección especificada está vacía.

public void ProcesarPedidos(List<Pedido> pedidos)
{
ApplicationGuard.IsEmpty(pedidos, "217: La lista de pedidos no puede estar vacía.");
// ... lógica para procesar los pedidos ...
}

IsNotEmpty

Type: public static void IsNotEmpty<T>(IEnumerable<T> value, string error)

Lanza una excepción si la colección especificada no está vacía.

public void MostrarProductos(List<Producto> productos)
{
ApplicationGuard.IsNotEmpty(productos, "218: La lista de productos no puede estar vacía");
// ... lógica para mostrar los productos ...
}

GuidIsEmpty

Type: public static void GuidIsEmpty(Guid value, string error)

Lanza una excepción si el GUID especificado está vacío.

public void ValidarProductoId(Guid productoId)
{
ApplicationGuard.GuidIsEmpty(productoId, "219: El ID del producto no puede ser vacío.");
// ... lógica para validar el ID del producto ...
}

GuidIsNotEmpty

Type: public static void GuidIsNotEmpty(Guid value, string error)

Lanza una excepción si el GUID especificado no está vacío.

public void ActualizarProducto(Guid productoId)
{
ApplicationGuard.GuidIsNotEmpty(productoId, "220: El Id no puede ser vacio");
// ... lógica para actualizar el producto ...
}