0

In a domain model, when you have a model class that implements Validate method and in this method you are adding BrokenRules as BusinessRule objects and all of them have a property and a rule message, what is the best way to localize these messages?

elector
  • 1,327
  • 4
  • 26
  • 43

4 Answers4

2

In my opinion localization belongs to UI. The fact that you even have this question may indicate that you overgeneralized your model and introduced concepts like Validate and BusinessRule. And now Presentation concerns 'bleed' into your domain code. If you used Ubiquitous Language your code would look more like

bool isDelinquent = order.IsDelinquent();

Where domain is clearly not responsible for UI concerns like user friendly and localized messages. Instead you probably have something along these lines:

List<BusinessRule> brokenRules = order.Validate(){ 
    ...
    brokenRule = new BusinessRule("Sorry this is order is delinquent");
    // what if I want this message in Italian?
    // would this even fit into error text box?
    // should delinquency unit test rely on 'magic string' error message?
    ...
}
Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70
  • Yes, this is exactly what I have. I am surprised that this approach is suggested in many books that refer to this as DDD. I don't have the time now to read Mr. Evans book, but I will for sure. Do you know of any example project in .NET that implements these concepts properly? Thank you – elector May 28 '13 at 20:57
  • I will accept this as an answer if you could describe a very, very simple solution... Thank you. – elector May 29 '13 at 09:42
  • 1
    Solution is to keep all presentation concerns (including messages that you display to a user) in UI layer. My main point was that if you don't let your domain objects to get into 'invalid' state the problem may just go away. The problem is that the word 'invalid' is very general and context dependent. I know that it sounds confusing but you should try thinking in terms of domain invariants and Ubiquitous Language. http://codebetter.com/gregyoung/2009/05/22/always-valid/ – Dmitry May 29 '13 at 16:14
  • I just wanted a quick demonstration of "don't let the domain object get into invalid state" without reading DDD blue book. I guess you somehow need to pass the details of what is wrong in the domain to the UI and I didn't want to have another mapping from domain messages to UI messages. – elector May 30 '13 at 06:33
2

Broken rules /Validation of an entity

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var validationResults = new List<ValidationResult>();

        //-->Check first name property
        if (String.IsNullOrWhiteSpace(this.FirstName))
        {
            validationResults.Add(new ValidationResult(Messages.validation_CustomerFirstNameCannotBeNull, 
                                                       new string[] { "FirstName" }));
        }

        //-->Check last name property
        if (String.IsNullOrWhiteSpace(this.LastName))
        {
            validationResults.Add(new ValidationResult(Messages.validation_CustomerLastNameCannotBeBull,
                                                       new string[] { "LastName" }));
        }


        return validationResults;
    }

You can let your entity implement IValidatableObject. That is part of System.ComponentModel.DataAnnotations. Still you can see that resx files can be used this way. Or you just make your own static wrapper around an xml file you read in application startup.

Magnus Backeus
  • 2,242
  • 17
  • 24
0

The problem you have with this is that your domain code may not be running near the UI. That being said, if the code is running on a server (message bus endpoint for example) then any exception becomes part of the business process.

For anything that does run with knowledge of the front-end language I would suggest resource files. They are really going to be the simplest.

Other than that specific exceptions or some coding/lookup system would probably be required,

Eben Roux
  • 12,983
  • 2
  • 27
  • 48
0

You can absolutely have messages in Domain. But they should be domain specific and tell clients whats wrong or what's invalid (usually are errors and validation messages that are responded back to client). Look at one of the best DDD examples out there http://msdn.microsoft.com/es-es/architecture/gg189193

they use messages in domain like:

 throw new InvalidOperationException(Messages.exception_BankAccountCannotDeposit);

or

originAccount.WithdrawMoney(amount, string.Format(Messages.messages_TransactionFromMessage, destinationAccount.Id));

As you can see you can use resx files within Domain project and use them to store messages like exceptions and other business messages back to client

Hope this helps you. Cheers

Magnus Backeus
  • 2,242
  • 17
  • 24