数据注释构造函数注入 [英] Data Annotations Constructor Injection

查看:80
本文介绍了数据注释构造函数注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET MVC,是否可以将构造函数注入与数据注释属性一起使用(具体来说,我正在使用验证属性)?

With ASP.NET MVC, is it possible to use constructor injection with the data annotation attributes (specifically, I'm using validation attributes)?

我会做什么希望能够做到的是:

What I would like to be able to do is:

public class NoEmailTakenAttribute : ValidationAttribute
{
    public NoEmailTakenAttribute(IService service) { .. }
}

可能吗?

谢谢。

推荐答案

您不能使用我在Reflector中看到的控制器注入,但似乎可以使用属性注入。通过创建一个从 DataAnnotationsModelValidatorProvider 继承的类,并覆盖方法 GetValidators ,似乎可以合理地使用这些属性。在验证发生之前注入的属性...这是根据初步分析而尚未完全确定的。

You can't use Controller injection from what I see using Reflector, but it does appear possible to use property injection. By creating a class that inherits from DataAnnotationsModelValidatorProvider, and by overriding the method GetValidators, it seems plausible that the attributes can be property injected into before the validation happens... this is from an initial analysis, yet to be fully determined.

这篇关于数据注释构造函数注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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