Adding custom meta data

Adding validation rules

Now you've got a simple model in place and know how to use platform-specific types, let's add a bit of domain knownledge to our model. In the end, it's very likely that there will be some sort of user input: in our sample application, users would enter new issues through a user interface, and these issues would be saved in a database.

In this part of the tutorial, you will learn how to enrich your model with custom constraints. By defining constraints in the model, you will have a single place from which you can generate validation rules and, for example, database constraints (should a field be nullable? And what should be the max length?).

Creating a custom profile

We will define constraints using a custom profile. As you've already seen in the previous part of this tutorial, profiles are a powerful extension mechanism. You will see that it is easy to create one yourself, and that Yellicode makes it easy to use your custom profile from inside a code generation template.

Go back to the IssueManager model in Yellicode Modeler, and follow these steps:

  1. In the Project Explorer, expand the Profiles panel and click Create a new profile.
  2. For the profile name, enter IssueManagerProfile (you can ignore the Visibility input).
  3. The new profile will be opened. Feel free to enter your own description.

Adding a custom stereotype to the profile

For defining our validation rules, we are going to create a so-called Stereotype. A stereotype describes how elements in a model (such as classes and attributes) can be extended with extra values. These extra values are called 'tagged values'.

  1. In the Project Explorer, right-click the IssueManagerProfile profile and choose New Element => Stereotype.
  2. For the stereotype name, enter CustomConstraints (again, ignore the Visibility input).
  3. The new stereotype will be opened. In the Extends section, check Attribute. This indicates that the stereotype can only be applied to attributes in the model.

Defining the 'Required' tag definition

Let's define two types of validation: required field validation and string length validation:

  1. In the Tag Definitions section, click the plus-icon to create a new definition.
  2. For the name, enter IsRequired.
  3. When the attribute editor panel opens, find the Type selector and choose boolean.
  4. Repeat steps 1-3, but now create a tag definition MaxStringLength with type integer.

Applying the validation rules to your model

Now that you have defined your first profile with a stereotype, it's time to put it to use by applying them.

  1. In the Project Explorer, right-click the IssueManager model and choose Apply Profile('s). In addition to the .NET profile from the previous part of the tutorial, check the IssueManagerProfile and confirm.
  2. Open the Issue class and click the Title attribute.
  3. Then, in the Title property editor, find the Stereotypes selection and check CustomConstraints.
  4. A new section named Tagged Values appears in the property editor, showing the tag definitions that you created in the previous section.
  5. As the final step, set IsRequired to true and fill in a reasonable MaxStringLength for a title.

Now that you've set constraints on the first attribute, feel free to repeat steps 3-5 for other attributes in the model. In the next part of this tutorial, you will learn how to use these constraints to generate validation code. But before you continue, make sure that you have saved the model (you will soon find out why).

Continue to Part V - Using custom meta data »