Skip to content

Instantly share code, notes, and snippets.

@mohammad-taheri1
Created October 23, 2025 20:43
Show Gist options
  • Select an option

  • Save mohammad-taheri1/4a8fa81b3b537209f1febe4d86e71f93 to your computer and use it in GitHub Desktop.

Select an option

Save mohammad-taheri1/4a8fa81b3b537209f1febe4d86e71f93 to your computer and use it in GitHub Desktop.
namespace Backend.Application.Common;
public class OperationResult<TResult> : IOperationResult
{
public TResult? Result { get; set; }
public bool IsSuccess { get; set; }
public bool IsNotFound { get; set; }
public List<KeyValuePair<string, string>> ErrorMessages { get; set; } = new();
public static OperationResult<TResult> SuccessResult(TResult result)
{
return new OperationResult<TResult>()
{
Result = result,
IsSuccess = true
};
}
public static OperationResult<TResult> FailureResult(string propertyName, string message)
{
var result = new OperationResult<TResult>
{
Result = default
};
result.ErrorMessages.Add(new KeyValuePair<string, string>(propertyName, message));
return result;
}
public static OperationResult<TResult> FailureResult(List<KeyValuePair<string, string>> errors)
{
return new OperationResult<TResult>
{
Result = default,
ErrorMessages = errors
};
}
public static OperationResult<TResult> DomainFailureResult(string errorMessage)
{
return new OperationResult<TResult>
{
Result = default,
ErrorMessages = new(new List<KeyValuePair<string, string>>() { new("DomainError", errorMessage) })
};
}
public static OperationResult<TResult> NotFoundResult(string propertyName, string message)
{
var result = new OperationResult<TResult>
{
Result = default,
IsNotFound = true
};
result.ErrorMessages.Add(new KeyValuePair<string, string>(propertyName, message));
return result;
}
}
public class OperationResult : IOperationResult
{
public bool IsSuccess { get; set; }
public bool IsNotFound { get; set; }
public List<KeyValuePair<string, string>> ErrorMessages { get; set; } = new();
public static OperationResult SuccessResult()
{
return new OperationResult()
{
IsSuccess = true
};
}
public static OperationResult FailureResult(string propertyName, string message)
{
var result = new OperationResult();
result.ErrorMessages.Add(new KeyValuePair<string, string>(propertyName, message));
return result;
}
public static OperationResult FailureResult(List<KeyValuePair<string, string>> errors)
{
return new OperationResult
{
ErrorMessages = errors
};
}
public static OperationResult DomainFailureResult(string errorMessage)
{
return new OperationResult
{
ErrorMessages = new(new List<KeyValuePair<string, string>>() { new("DomainError", errorMessage) })
};
}
public static OperationResult NotFoundResult(string propertyName, string message)
{
var result = new OperationResult
{
IsNotFound = true
};
result.ErrorMessages.Add(new KeyValuePair<string, string>(propertyName, message));
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment