2

ErrorMessageResourceNameデータ検証属性とandの詳細については、検索で次の質問/回答を見つけましたErrorMessageResourceType

カスタム リソース ソリューションで DataAnnotations ErrorMessageResourceName を使用する方法

残念ながら、必要な情報がありません。答えでは、推定されるクラスCustomResourceManagerが言及されています:

[Required(
    ErrorMessageResourceType = typeof(CustomResourceManager), 
    ErrorMessageResourceName = "ResourceKey")]
public string Username { get; set; }

しかし、このクラスがどのように見えるか、どのインターフェースを実装するかなどに関するドキュメントが見つかりません。検証の失敗に対してカスタム エラー メッセージを提供する必要がありますが、必要なドキュメントが見つかりません。

4

1 に答える 1

2

エンティティ フレームワークと検証属性を使用して wpf アプリのグローバリゼーションを実現しようとしたため、今日も同じ問題が発生しました。

私はさまざまなソリューションを試しましたが、今のところ、CustomResourceManagerDave Sexton がこの投稿で説明しているサンプルに基づいて作成することになりました: http://www.pcreview.co.uk/forums/load-resources-string-table-database- t2892227.html

基本的に、私は my を実装DatabaseResourceManagerし、それぞれDatabaseResourceSetから派生し、次のように実装しました。ResourceManagerResourceSet

DatabaseResourceManager実装:

public class DatabaseResourceManager : ResourceManager
{
    #region Singleton pattern http://msdn.microsoft.com/en-us/library/ff650316.aspx
    private static volatile DatabaseResourceManager instance;
    private static object syncRoot = new Object();

    private DatabaseResourceManager() : base() { }

    public static DatabaseResourceManager Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new DatabaseResourceManager();
                }
            }

            return instance;
        }
    }
    #endregion

    protected override ResourceSet InternalGetResourceSet(CultureInfo culture, bool createIfNotExists, bool tryParents)
    {
        if (culture == null)
            culture = CultureInfo.InvariantCulture;

        return new DatabaseResourceSet(culture);
    }
}

そしてDatabaseResourceSet実装:

public class DatabaseResourceSet : ResourceSet
{
    private readonly CultureInfo culture;
    private static readonly Dictionary<string, Hashtable> cachedResources = new Dictionary<string, Hashtable>();

    public DatabaseResourceSet(CultureInfo culture)
    {
        if (culture == null)
            throw new ArgumentNullException("culture");

        this.culture = culture;

        ReadResources();
    }

    protected override void ReadResources()
    {
        if (cachedResources.ContainsKey(culture.Name))
        // retrieve cached resource set
        {
            Table = cachedResources[culture.Name];
            return;
        }

        using (MyDatabaseContext db = new MyDatabaseContext())
        {
            var translations = from t in db.Translations
                       where t.CultureIso == culture.Name
                       select t;
            foreach (var translation in translations)
            {
                Table.Add(translation.Chave, translation.Valor);
            }
        }

        cachedResources[culture.Name] = Table;
    }
}

私の DataBaseResourceManager にはシングルトーン パターンが実装されているため、次のように簡単にアクセスしてデータを取得できます。

public class LocalizedRequiredAttribute : RequiredAttribute
{
    public LocalizedRequiredAttribute() : base()
    {
    }

    public override string FormatErrorMessage(string name)
    {
        string localErrorMessage = DatabaseResourceManager.Instance.GetString(this.ErrorMessageResourceName) ?? ErrorMessage ?? "{0} is required"; //probably DataAnnotationsResources.RequiredAttribute_ValidationError would is a better option
        return string.Format(System.Globalization.CultureInfo.CurrentCulture, localErrorMessage, new object[] { name });
    }
}

今の欠点:

pcreview の投稿で述べたように、winforms は this を受け入れませんDataBaseResourceManagerが、私の場合は wpf を使用しているため、この問題はありませんでした (ただし、ResourceManagerと a の重複のため、別の小さなメモリの問題がありましたDictionaryResource)。winformにリソースマネージャーを挿入できるかどうかはわかりませんが、それはあなたの質問を超えています. すでにご存知のもう 1 つの欠点は、がValidationAttribute使用されるようにすべての使用済みを導出する必要があることFormatErrorMessageです。StringLengthAttribute、、、RequiredAttributeなどRegularExpressionAttribute。これはエラーが発生しやすく、不器用です。残念ながら、Microsoft はここで私たちに多くの余地を与えてくれませんでした。

おそらく、将来この回答を確認して、これに関する私の調査結果を共有します。

よろしく

于 2013-08-06T13:25:23.163 に答える