Class CommandResult
Represents the result of a command execution.
public class CommandResult
- Inheritance
-
CommandResult
- Inherited Members
Properties
CommandId
Gets or sets the ID of the command that produced this result.
public Guid CommandId { get; set; }
Property Value
Data
Gets or sets any data returned by the command.
public object? Data { get; set; }
Property Value
ErrorMessage
Gets or sets error details if the command failed.
public string? ErrorMessage { get; set; }
Property Value
Exception
Gets or sets the exception that occurred during command execution, if any.
public Exception? Exception { get; set; }
Property Value
ExecutedAt
Gets or sets the date and time when the command was executed.
public DateTime ExecutedAt { get; set; }
Property Value
ExecutionTime
Gets or sets the duration of the command execution.
public TimeSpan? ExecutionTime { get; set; }
Property Value
Message
Gets or sets a message describing the result of the command.
public string Message { get; set; }
Property Value
Success
Gets or sets a value indicating whether the command executed successfully.
public bool Success { get; set; }
Property Value
ValidationErrors
Gets or sets the collection of validation errors produced during command execution.
public List<string> ValidationErrors { get; set; }
Property Value
Warnings
Gets or sets the collection of warnings generated during command execution.
public List<string> Warnings { get; set; }
Property Value
Methods
AddWarning(string)
Adds a warning message to the result.
public CommandResult AddWarning(string warning)
Parameters
warningstringThe warning message to add.
Returns
- CommandResult
The current CommandResult instance for method chaining.
Fail(string, Exception?)
Creates a failed CommandResult instance.
public static CommandResult Fail(string errorMessage, Exception? exception = null)
Parameters
errorMessagestringA message describing the cause of the failure.
exceptionExceptionThe exception that triggered the failure, if available.
Returns
- CommandResult
A configured CommandResult indicating failure.
GetData<T>()
Gets the primary data payload of the result, converted to the specified type.
public T? GetData<T>()
Returns
- T
The data converted to
T, or the default value ofTif not set or not convertible.
Type Parameters
TThe expected type of the primary data.
GetData<T>(string)
Gets a named data value from the result.
public T? GetData<T>(string key)
Parameters
keystringThe key associated with the stored value.
Returns
- T
The value converted to
T, or the default value ofTif not found or not convertible.
Type Parameters
TThe expected type of the stored value.
GetDisplayMessage()
Gets a user-friendly message summarizing the outcome of the command execution.
public string GetDisplayMessage()
Returns
- string
A human-readable message that describes the result.
Ok(string, object?)
Creates a successful CommandResult instance.
public static CommandResult Ok(string message = "Command executed successfully.", object? data = null)
Parameters
messagestringAn optional message describing the success.
dataobjectOptional data returned by the command.
Returns
- CommandResult
A configured CommandResult indicating success.
SetData(string, object?)
Sets a named data value in the result.
public CommandResult SetData(string key, object? value)
Parameters
Returns
- CommandResult
The current CommandResult instance for method chaining.
ToString()
Returns a string that represents the current CommandResult.
public override string ToString()
Returns
- string
A formatted string containing the success status and primary message.
Unauthorized(string)
Creates a CommandResult representing an authorization failure.
public static CommandResult Unauthorized(string message = "Unauthorized to execute this command.")
Parameters
messagestringAn optional message describing the authorization issue.
Returns
- CommandResult
A configured CommandResult indicating unauthorized access.
ValidationFailed(List<string>)
Creates a CommandResult representing a validation failure.
public static CommandResult ValidationFailed(List<string> errors)
Parameters
Returns
- CommandResult
A configured CommandResult indicating validation failure.