誰でも、エラーメッセージのカスタマイズを可能にするカスタムIMessageInterpolatorを にすることができました。このウェブページの 手順に従ってみましたが、役に立たないです。 http://codelog.climens.net/2009/03/04/nhibernate-validator-custom-messages/nHibernate ValidatorカスタムIMessageInterpolator
DefaultMessageInterpolatorを見ると、 はフレームワークに焼き付けられているので、紛失しているものがあります。
私はユニットテストをどのように実装しているかを示しました。
namespace TestValidator
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestValidator()
{
var customer = new Customer();
ClassValidator classValidator = new ClassValidator(customer.GetType());
InvalidValue[] validationMessages = classValidator.GetInvalidValues(customer);
Assert.IsTrue(validationMessages.Length == 1);
Assert.IsTrue(validationMessages[0].Message == "may not be null or empty");
}
[TestMethod]
public void TestCustomInterpolator()
{
ValidatorEngine ve = new ValidatorEngine();
NHVConfiguration nhvc = new NHVConfiguration();
nhvc.Properties[Environment.MessageInterpolatorClass] = typeof(CustomMessageInterpolator).AssemblyQualifiedName;
ve.Configure(nhvc);
var customer = new Customer();
ClassValidator classValidator = new ClassValidator(customer.GetType());
InvalidValue[] validationMessages = classValidator.GetInvalidValues(customer);
Assert.IsTrue(validationMessages.Length == 1);
// THIS TEST FAILS!!
Assert.IsTrue(validationMessages[0].Message == "CustomMessageInterpolator");
}
}
public class Customer
{
public virtual int CustomerId { get; set; }
[NotNullNotEmpty]
public string CustomerName { get; set; }
}
public class CustomMessageInterpolator : IMessageInterpolator
{
public CustomMessageInterpolator()
{
}
public string Interpolate(string message, IValidator validator, IMessageInterpolator defaultInterpolator)
{
return "CustomMessageInterpolator";
}
}
}
NHibernateとNHibernate.Validatorの設定も追加できますか? – zihotki
これは完全な設定です。もうない。 – Craig