Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database injection into a validation attribute with ASP MVC and Castle Windsor

I need some help - I am trying to use a custom validation attribute in an ASP.NET MVC web project that needs to make a database call.

I have windsor successfully working for the controllers and the IRepository interface is injected normally. The problem arrises when I need to inject the repository into the attribute class.

The attribute class has the following code:

public class ValidateUniqueUrlNodeAttribute : AbstractValidationAttribute
{
    private readonly string message;
    private readonly IArticleRepository articleRepository;

    public ValidateUniqueUrlNodeAttribute(string message)
    {
        this.message = message;
    }

    public ValidateUniqueUrlNodeAttribute(string message, IArticleRepository articleRepository):this(message)
    {
        this.articleRepository = articleRepository;
    }
    public override IValidator Build()
    {
        var validator = new UniqueUrlNodeValidator(articleRepository) { ErrorMessage = message };

        ConfigureValidatorMessage(validator);

        return validator;
    }

My problem is that I cannot seem to make Windsor intercept the contruction of the attribute to pass in the IArticleRepository

The current code in my global.asax file is as follows:

container = new WindsorContainer();
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(Container));
   container
     .RegisterControllers(Assembly.GetExecutingAssembly())
     .AddComponent<IArticleRepository, ArticleRepository>()
     .AddComponent<ValidateUniqueUrlNodeAttribute>();

Any help would be greatly appreciated.

like image 959
David E Avatar asked Jun 12 '09 10:06

David E


People also ask

Does attributes can be used for data validation in MVC?

These attributes are used to define metadata for ASP.NET MVC and ASP.NET data controls. You can apply these attributes to the properties of the model class to display appropriate validation messages to the users. The following table lists all the data annotation attributes which can be used for validation.

Which attribute is used for validation in MVC?

ASP.NET MVC provides a unique feature in which we can validate the models using the Data Annotation attribute. Import the following namespace to use data annotations in the application.

How is validation implemented in ASP.NET MVC?

In ASP.NET MVC model validations are done using Data Annotation, its inherited System. ComponentModel. DataAnnotations assembly. In ASP.NET MVC 4 provides a different way to build custom validation.


1 Answers

AFAIK no dependency injection container can directly manage an attribute, since it's instantiated by the runtime and there's no way to intercept that.

However, they can cheat by either:

  1. Using a static gateway to the container (example), or
  2. Using a "BuildUp" feature that injects whatever dependencies are found within an already-constructed object. This is called BuildUp in Unity or InjectProperties in Autofac.

Windsor doesn't support #2 (ref1, ref2), so you can either:

  1. Try one of the hacks to make Windsor support #2 (hack1, hack2)
  2. Use a static gateway
  3. Implement your own IValidatorBuilder and make it use Windsor to create validators. I'm sure this is implemented somewhere but I can't find it right now...
like image 59
Mauricio Scheffer Avatar answered Sep 20 '22 20:09

Mauricio Scheffer