Is it posible to have the Validation.Validate()
method of the Validation Application Block see each parameter of the IParameterCollection
supplied by Unity as my custom UserModel
?
I am attempting to use both Unity and the Validation Application Block to validate method parameters.
I would like to be able to denote a method as [RequiresValidation()]
and the parameters of that method with the appropriate validation attributes.
So, something like this:
[RequiresValidation()]
public void SaveUser(UserModel user)
{
// ...
}
public class UserModel
{
[StringLengthValidator(3, 255)]
[RegexValidator(@"^[a-zA-Z0-9]${3,255}")]
public string Name { get; set; }
[StringLengthValidator(0, 255)]
[RegexValidator(@"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b")]
public string EMail { get; set; }
}
I have created a custom HandlerAttribute that will initiate the call to the validation handler as shown.
public class RequiresValidationAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new ValidationCallHandler();
}
}
Now, the Validation Handler will attempt to validate each parameter of the method:
public class ValidationCallHandler : ICallHandler
{
public int Order { get; set; }
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
// Validate the method parameters
ValidateArguments(input.Arguments);
// Call the method that was intercepted
return getNext()(input, getNext);
}
private static void ValidateArguments(IParameterCollection parameterCollection)
{
foreach (var parameter in parameterCollection)
{
var results = Validation.Validate(parameter);
if (results.IsValid) continue;
// ...
}
// ...
}
}
The Validation.Validate()
method will always return true. Based on my observations, it seems the validation logic is treating the parameter
as an object
and not my UserModel
.
Is it posible to have the Validation.Validate()
method of the Validation Application Block see each parameter of the IParameterCollection
supplied by Unity as my custom UserModel
?
Unity is a real-time 3D development platform for building 2D and 3D application, like games and simulations, using . NET and the C# programming language. Unity can target 25+ platforms across mobile, desktop, console, TV, VR, AR, and the web. Unity is FREE to start with and is available for both Windows and macOS.
The Dependency Injection Design Pattern allows us to inject the dependency objects into a class that depends on them. Unity is a dependency injection container that can be used for creating and injecting the dependency object using either constructor, method, or property injections.
The Unity Container (Unity) is a full featured, extensible dependency injection container. It facilitates building loosely coupled applications and provides developers with the following advantages: Simplified object creation, especially for hierarchical object structures and dependencies.
This package has been deprecated as it is legacy and is no longer maintained. Due to a complete lack of interest from the community to support this project, it has been archived and will no longer be maintained.
You will need to use the ValidationFactory
instead of the Validation
class. When you replace this line:
var results = Validation.Validate(parameter);
With the following line, you can get it to work.
var results =
ValidationFactory.CreatValidator(parameter.GetType())
.Validate(parameter);
Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With