Атрибуты MetadataType игнорируются в пользовательском валидаторе - PullRequest
2 голосов
/ 01 июня 2011

(.NET 4.0 / WebForms / EF 4.1 POCO)

Привет,

Я использую пользовательский валидатор для использования аннотаций данных с веб-формами (исходный код ниже).

Все идет хорошо, когда я использую DataAnnotations непосредственно в сгенерированных классах.Но когда я использую DataAnnotations в классе Metadata с частичным классом, атрибуты DataAnnotations кажутся обойденными при проверке.Я знаю, что метаданные были правильно распознаны, потому что, когда я сохраняю данные в DbContext, они проверяются, и EntityValidationErrors возвращает проверенные ошибки.

Я провел несколько поисков и обнаружил следующее: (/1930622/atribut-net-4-rtm-metadatatype-ignoriruetsya-pri-ispolzovanii-validator#1930623). К сожалению, моя реализацияне сработало. Может быть, я не знаю, где это назвать. Я пытался вызвать его в конструкторе класса Metadata, но это не сработало.

public static class MetadataTypesRegister
{
    static bool installed = false;
    static object installedLock = new object();

    public static void Install()
    {
        if (installed)
        {
            return;
        }

        lock (installedLock)
        {
            if (installed)
            {
                return;
            }

            // TODO: for debug purposes only (please remove in production)
            Assembly assembly = Assembly.GetExecutingAssembly();
            Type[] types = assembly.GetTypes();
            //------

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                foreach (MetadataTypeAttribute attrib in type.GetCustomAttributes(typeof(MetadataTypeAttribute), true))
                {
                    TypeDescriptor.AddProviderTransparent(
                        new AssociatedMetadataTypeTypeDescriptionProvider(type, attrib.MetadataClassType), type);
                }
            }

            installed = true;
        }
    }
}

Модель, которая будетvalidated находится в DataLayer.dll, а класс DataAnnotationsValidator находится в Common.dll.

Это мой класс DataAnnotationsValidator:

[ToolboxData("<{0}:DataAnnotationsValidator runat=server></{0}:DataAnnotationsValidator>")]
public class DataAnnotationsValidator : BaseValidator
{

    private string _propertyName = string.Empty;
    public string PropertyName
    {
        get { return _propertyName; }
        set { _propertyName = value; }
    }

    public string _sourceType = string.Empty;
    public string SourceType
    {
        get { return _sourceType; }
        set { _sourceType = value; }
    }

    public ValidationDataType _type = ValidationDataType.String;
    public ValidationDataType Type
    {
        get { return _type; }
        set { _type = value; }
    }

    public string _cssError = string.Empty;
    public string CssError
    {
        get { return _cssError; }
        set { _cssError = value; }
    }

    protected override bool EvaluateIsValid()
    {   
        // get specified type for reflection
        Type objectType = System.Type.GetType(_sourceType, true, true);

        // get a property to validate
        PropertyInfo prop = objectType.GetProperty(_propertyName);

        // get the control to validate
        TextBox control = this.FindControl(this.ControlToValidate) as TextBox;

        object valueToValidate = null;
        if (control.Text != String.Empty)
        {
            if (Type == ValidationDataType.Double)
                valueToValidate = double.Parse(control.Text);
            else if (Type == ValidationDataType.Integer)
                valueToValidate = int.Parse(control.Text);
            else if (Type == ValidationDataType.Date)
                valueToValidate = DateTime.Parse(control.Text);
            else if (Type == ValidationDataType.Currency)
                valueToValidate = decimal.Parse(control.Text);
            else
                valueToValidate = control.Text;
        }

        CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
        bool result = true;
        try
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

            // The custom validator can return only one error message. Because the field model being validated can have more than 
            // one DataAnnotation validation (Required, Range, RegularExpression, etc.) the DataAnnotationsValidator will return only the first 
            // error message that it evaluates.
            foreach (ValidationAttribute attr in prop.GetCustomAttributes(typeof(ValidationAttribute), true).OfType<ValidationAttribute>())
            {
                Thread.CurrentThread.CurrentCulture = currentCulture;

                if (!attr.IsValid(valueToValidate))
                {
                    result = false;
                    var displayNameAttr = prop.GetCustomAttributes(typeof(DisplayNameAttribute), true).OfType<DisplayNameAttribute>().FirstOrDefault();
                    string displayName = displayNameAttr == null ? prop.Name : displayNameAttr.DisplayName;

                    ErrorMessage = attr.FormatErrorMessage(displayName);
                    break;
                }
            }
        }
        finally
        {
            Thread.CurrentThread.CurrentCulture = currentCulture;

            if (result)
            {
                if (!string.IsNullOrEmpty(CssError))
                    control.RemoveCssClass(CssError);
            }
            else
            {
                if (!string.IsNullOrEmpty(CssError))
                    control.AddCssClass(CssError);
            }
        }
        return result;
    }
}

Спасибо !!

1 Ответ

1 голос
/ 01 июня 2011

Я нашел решение здесь (/4114069/kak-vy-provodite-validatsiy-modeli-veb-form).

Я изменяю код метода EvaluateIsValid, чтобы включить приведенный ниже код для поиска атрибутов метаданных:

    // get specified type for reflection
    Type objectType = System.Type.GetType(_sourceType, true, true);

    // check for the types that have MetadataType attribute because 
    // it is they who have the DataAnnotations attributes
    IEnumerable<MetadataTypeAttribute> mt = objectType.GetCustomAttributes(typeof(MetadataTypeAttribute), false).OfType<MetadataTypeAttribute>();
    if (mt.Count() > 0)
    {
        objectType = mt.First().MetadataClassType;
    }

И все идет хорошо!

...