Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Unity Framework & Validation Application Block to Validate Method Parameters

Question

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?


Background:

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;

      // ...
    }

    // ...
  }
}

Problem

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.


Question

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?

like image 309
Paul Redman Avatar asked Apr 01 '10 19:04

Paul Redman


People also ask

What is Unity framework?

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.

What is Unity framework in MVC?

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.

What is the use of Unity container in C#?

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.

Is Microsoft Unity deprecated?

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.


1 Answers

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.

like image 120
Steven Avatar answered Nov 03 '22 09:11

Steven