数据库注入与ASP MVC和温莎城堡验证属性 [英] Database injection into a validation attribute with ASP MVC and Castle Windsor

查看:122
本文介绍了数据库注入与ASP MVC和温莎城堡验证属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助 - 我想在需要进行数据库调用一个ASP.NET MVC的Web项目中使用自定义验证属​​性。

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.

我有温莎顺利工作的控制器和IRepository接口通常注入。当我需要存储库注入到属性类的问题边棱。

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.

属性类具有以下code:

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;
    }

我的问题是,我似乎无法让温莎拦截属性的建构中IArticleRepository通

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

在我的Global.asax文件当前code是如下:

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>();

任何帮助将大大AP preciated。

Any help would be greatly appreciated.

推荐答案

据我所知没有依赖注入容器,可以直接管理的属性,因为它的运行时实例化,有没有办法拦截。

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. 使用静态网关到容器(<一个href=\"http://stackoverflow.com/questions/553330/how-do-i-use-windsor-to-inject-dependencies-into-actionfilterattributes/553405#553405\">example),或

  2. 使用集结功能,无论注入都依赖一个已经构造的对象中找到。这被称为堆积在Unity 在Autofac InjectProperties。

  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.

温莎不支持#2( REF1 ,的 REF2 ),所以你可以:

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


  1. 尝试的黑客之一,使温莎支持#2(<一个href=\"http://www.jeremyskinner.co.uk/2008/11/08/dependency-injection-with-aspnet-mvc-action-filters/\"相对=nofollow> hack1 , hack2

  2. 使用一个<一个href=\"https://rhino-tools.svn.sourceforge.net/svnroot/rhino-tools/trunk/commons/Rhino.Commons/RhinoContainer/IoC.cs\"相对=nofollow>静态网关

  3. 实施自己的<一个href=\"https://svn.castleproject.org/svn/castle/trunk/Components/Validator/Castle.Components.Validator/IValidatorBuilder.cs\"相对=nofollow> IValidatorBuilder ,并使其使用温莎建立验证程序。我敢肯定,这是什么地方实现,但我找不到它现在...

  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...

这篇关于数据库注入与ASP MVC和温莎城堡验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆