Skip to content

hylasoft-usa/h-resolution

Repository files navigation

h-resolution

A lightweight error collection framework.

Requirements

h-resolution works with .NET Framework 4.5 and higher

Usage

Have validation, service, and/or domain methods return a Hylasoft.Resolution.Result object.

public Result Validate()
{
	return conditionMet()
		? Result.Info("Validation passed.")
		: Result.Error("Validation did not pass.");
}

Concatenate Results by using the addition operator.

public Result Validate()
{
	var first = ValidateFirst();
	var second = ValidateSecond();

	return first + second;
}

public Result ValidateAlternate()
{
        var validate = ValidateFirst();
	validate += ValidateSecond();
	
	return validate
		? validate
		: validate + Result.SingleInfo("Validation failed.");	
}

Or by using Result.Concat().

public Result Validate()
{
	return Result.Concat(ValidateFirst, ValidateSecond, ValidateThird);
}

public Result ValidateWithParam<T>(T param)
{
	return Result.Concat(param, ValidateFirst, ValidateSecond, ValidateThird);
}

Result.ConcatRestricted() can be used in the same way, but will stop execution on the first failure.

public Result Validate()
{
	return Result.ConcatRestricted(ValidateFirst, ValidateSecond, ValidateThird);
}

Results can be built from exceptions.

public Result Validate()
{
	try
	{
		ValidateFirst();
		ValidateSecond();

		return Result.Success;
	}
	catch (Exception e)
	{
		return Result.Error(e);
	}
}

Results can be implicitly cast to bool.

public Result Validate()
{
	Result first;
	return (first = ValidateFirst())
		? first + ValidateSecond()
		: first.AppendError("The first phase of validation failed.");
}

Failure is defined as a result containing any Result Issue with a level higher than a warning.

	if (Result.Trace("Trace message"); // true

	if (Result.SingleDebug("Debug message"); // true

	if (Result.SingleInfo("Info message"); // true

	if (Result.SingleWarning("Warning message"); // true

	if (Result.SingleError("Error message"); // false

	if (Result.SingleFatal("Fatal message"); // false

Results are treated as IEnumerable

	var result = Validate();
	var errors = result.Where(issue => issue.Level > ResultLevels.Warning).ToList();

Result issues can be compared to each other, and equated against longs, strings, or other result issues.

	var result = Validate();
	var result2 = ValidateSecond();

	var distinctIssues = result.Distinct().ToList();
	var exclusions = result.Except(result2).ToList();
	
	var targetCode = 5;
	var targetMessage = Resources.Warnings.SpecificWarning;

	if (result.Contains(targetCode))
		result = result.AppendTrace("Result contains '{0}'.", targetCode);

	if (result.Contains(targetMessage))
		result = result.AppendTrace("Result contains specific message.");

	if (result.Contains(result2.Max())
		result = result.AppendTrace("Max result2 included in first.");

Results may have their issues filtered by conditions.

  var result = Validate();

  var debugMessages = result.Where(r => r.Level == ResultIssueLevels.Debug);
  var userMessages = result.Where(r => IssueCodes.UserMessages.Contains(r.IssueCode);

Build

You can build the project using Visual Studio or by running the grunt tasks for msbuild

Contribute

This project uses hylasoft/cs-boilerplate to define tasks and stle guides. Please read the readme of the project to learn more about how to contribute.

About

A lightweight error collection framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published