Skip to content

A proxy that allows dynamic validation of services method calls.

License

Notifications You must be signed in to change notification settings

nickntg/ValidationProxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ValidationProxy

ValidationProxy is a library that allows injection of dynamic validation rules in methods. It is designed with Web API services in mind and focuses on simplicity.

Example usage

Having the following service:

public interface ICalculatorService
{
	public int CalculateFactorial(int number);
}

public class CalculatorService : ICalculatorService
{
	public int CalculateFactorial(int number)
	{
		if (number == 1)
		{
			return 1;
		}

		return number * CalculateFactorial(number - 1);
	}
}

Assume we want to prevent CalculateFactorial from being called with numbers equal or less than zero and numbers above 10. Using ValidationProxy, this is achieved as follows:

  • Annotate the CalculateFactorial method.
public const string FactorialTooSmall = "factorial-too-small";
public const string FactorialTooLarge = "factorial-too-large";

[Validator(0, ValidationActions.FactorialTooLarge)]
[Validator(1, ValidationActions.FactorialTooSmall)]
public int CalculateFactorial(int number)
{
	if (number == 1)
	{
		return 1;
	}

	return number * CalculateFactorial(number - 1);
}
  • Add the validation interceptor in the Web API configuration.
services.AddValidatorInterceptor();
  • Configure ICalculatorService for interception.
services.AddProxiedScoped<ICalculatorService, CalculatorService>();
  • Add validation actions
ActionRepository.AddValidationAction(ValidationActions.FactorialTooSmall, info =>
{
	var argument = info.ArgumentByPosition(0).AsInt();
	if (argument<=0)
	{
		throw new ArgumentException($"Cannot calculate factorial for {argument}");
	}
});

ActionRepository.AddValidationAction(ValidationActions.FactorialTooLarge, info =>
{
	var argument = info.ArgumentByName("number").AsInt();
	if (argument > 10)
	{
		throw new ArgumentException($"Will not calculate factorial for {argument}");
	}
});

Validation actions can be removed or added dynamically during the lifetime of the application.

About

A proxy that allows dynamic validation of services method calls.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages