Home > .NET Domain Model, Developing Using CodeFluent Entities > Messages in CodeFluent Entities

Messages in CodeFluent Entities

CodeFluent Entities supports localization thanks to the message concept. Even though its use is quite simple, the message concept has multiple facets which we’ll describe in this section.

Local and Global Messages

Messages represent text resources in the .NET sense. It means that declaring a message in your model will generate an entry in the generated resource file (.resx file). Therefore, it’s common practice in CodeFluent Entities projects to see model parts (e.g. MyApplication.Messages.xml) which contain all localizable strings of an application. This way all the text that needs to be localized by your translation team is centralized into one file, from which the actual .NET resource files (one per handled locale) will be generated.

Defining global messages is done as so:

<cf:project xmlns:cf="http://www.softfluent.com/codefluent/2005/1" defaultNamespace="MyApplication">
   <cf:message name="FatalError" cultureName="fr-FR">Erreur fatale</cf:message>
   <cf:message name="FatalError" cultureName="en-US">Fatal Error</cf:message>

   <cf:message name="FileNotFoundError" cultureName="fr-FR">Erreur: Le fichier '{0}' est introuvable</cf:message>
   <cf:message name="FileNotFoundError" cultureName="en-US">Error: File '{0}' not found</cf:message>


In the example above we defined global messages. Since the name attribute is specified in the message declaration, the generated resource manager class will contain corresponding properties (FatalError, and FileNotFoundError) which depending on the current thread’s culture will return one localized message version (English) or the other (e.g. French).

Messages can also be local, that is to say linked with another concept such as an entity, an enumeration, a property, a method, etc. It’s very interesting because in such cases, UI producers will be able to use localized strings representing the object rather than the object name (by default).

    <cf:message cultureName="en-US">Mrs</cf:message>
    <cf:message cultureName="fr-FR">Mme</cf:message>

In the example above, we declared two local messages, that are linked to the Mrs enumeration value. This way, whenever the value will be used in produced UI layers, the localized string will be used, instead of the enumeration value.

Validation Messages

CodeFluent Entities supports Validation Rules, therefore validation messages are needed to warn an end-user that typed-in data is invalid. By default, all validation rules have their default messages, however it’s possible to override those messages. Technically speaking, validation messages are global messages, that is to say messages declared at the project level. They can be overridden if necessary by declaring them explicitly in your model.

Those messages are contained in the following namespace: CFV.[ValidatorName].[ValidationRule]. For instance, overriding the validation message used by a MaxLength validation rule would be done as so:

<cf:message name="CFV.StringValidator.MaxLength"  cultureName="en-US" >
    Global English message for CFV.StringValidator.MaxLength

By doing as shown here-before, all MaxLength validation messages raised by the string validator will use the specified message.

In order to modify the validation for a single property, you should declare it in the rule body such as:

<cf:property name="Code2">
      <cf:rule typeName="StringValidate" maxLength="2">
        <cf:message name="MaxLength"  cultureName="en-US" >
          English local message 'Code2' for StringValidate

Supporting Cultures

Cultures supported are the cultures defined in the application concept of your project (Design Concepts). By default, two cultures are supported: English and French in the default application. From the cultures defined in the application concept of your model, one resource file will be generated by culture. Each resource file will contain all messages of the corresponding culture name.

To support, cultures other than English and French, you need to declare handled cultures, ergo you need to define your application. Consequently the first step is to disable the creation of the default application, which can be done by setting the createDefaultApplication attribute to false on the cf:project node. Then in your own custom application, define your desired cultures.

<cf:application xmlns:cf="http://www.softfluent.com/codefluent/2005/1"
    <cf:culture name="en-US"/>
    <cf:culture name="fr-FR"/>
    <cf:culture name="it-IT"/>


Messages have a class attribute which lets developers specify a class to a message. This attribute is made to group messages that can afterwards be used to produce something. For instance, CodeFluent Entities provides the _doc class. Declaring a message in the _doc class will produce a code remark on the parent node in the Business Object Model (BOM).

  <cf:message class="_doc">Code documentation for "Car"</cf:message>

In the example above the Car entity class will be decorated with the given message:

/// <summary>
/// Code documentation for "Car"
/// </summary>
public partial class Car: ...

Furthermore, you can define your own classes which you could use in patterns or with the Template Producer to produce extra outputs such as a RTF file documenting your BOM which was illustrated in the following post: https://codefluententities.wordpress.com/2009/08/13/generate-a-documentation-of-your-codefluent-model-using-codefluent-api/

CodeFluent Entities R&D Team

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s