With data annotations, it is now easy to localize error messages using Resource.resx files, for example, such as:
public class Student
{
. . .
[Required(ErrorMessageResourceName ="Required",
ErrorMessageResourceType = typeof(StudentResources))]
[StringLength(16)]
[Display(Name = "FirstName", ResourceType = typeof(StudentResources))]
public string FirstName { get; set; }
. . .
}
Now, let's say I want to check if the Student has already paid for this month and year:
public bool CheckIfAlreadyPaid(Payment payment)
{
return repository.GetPayments().Any(p => p.StudentId == payment.StudentId &&
p.Month == payment.Month &&
p.Year == payment.Year);
}
If he has already made the payment, I do the following at my service level:
if (CheckIfAlreadyPaid(payment))
{
modelState.AddModelError("AlreadyPaid",
Resources.Views.Payment.PaymentCreateResources.AlreadyPaid);
}
This works, but I'm not sure referring to a service level resource file.
Is there a standard or better way to localize error messages that are not tied to model properties (Data Annotation) - errors that arise from business logic rules? Should I add these errors to ModelStateDictionary?