Configuring a Domain Model

Last modified: April 18, 2024

1 Introduction

This document explains how you can configure a domain model for your application with Mendix. Each application can have multiple modules, and each module has its own domain model. All the domain models together define the data layer of the application.

A domain model consists of entities and associations. An entity is the blueprint for an object in your application, like the Customer and Order entities mentioned in the following sections.

Associations define the relation between two entities. As soon as you deploy the application, Mendix takes care of the underlying database for you. This means that you do not have to create tables and write queries yourself.

This document teaches you how to do the following:

  • Create entities and attributes
  • Add enumerations
  • Create associations
  • Delete association behavior

This document also presents an example for defining the domain model for an online shopping shop.

2 Creating Entities and Attributes

To create entities and their attributes, follow these steps:

  1. Open your domain model.

  2. Go to Toolbox and drag Entity into your domain model.

    By default, Studio Pro creates a persistable entity, which means that the app’s database is able to store objects of this type of entity.

  3. Start typing directly to change the name of the entity to Customer, or you can go to Properties and enter Customer for the Name:

  4. Double-click the Customer entity and go to Attributes.

  5. Click New to add an attribute to the Customer entity.

  6. Enter Name for the Name of the new attribute, and select String as the data Type:

  7. Refer to steps 4-6 to create a Customer entity that looks like this:

  8. Refer to steps 1-6 to create a second entity Order that looks like this:

3 Adding Enumerations

An enumeration is a predefined list of values that can be used as an attribute type. This allows end-users to select any of the predefined values for this attribute. A good example of an enumeration is order status (with values open, processing, and complete).

To extend the Order entity with an enumeration value-based attribute, follow these steps:

  1. Right-click the module and select Add other > Enumeration.

  2. Enter OrderStatus for the Name and click OK.

  3. Click New to add an enumeration value.

  4. Enter Open for the Caption and click OK.

  5. Refer to steps 3 and 4 to add the Processing and Complete values. You should then have the following configured values:

    Click OK to save the enumeration. Now you need to add an enumeration value-based attribute to the Order entity and select the OrderStatus enumeration there.

  6. Double-click the Order entity and go to Attributes.

  7. Click New to add an attribute to the Order entity.

  8. Enter OrderStatus for the Name and select Enumeration for the Type.

  9. Select the OrderStatus enumeration and click Select.

  10. Select Open for Default value:

  11. Click OK to save the new attribute. Now the Order entity should look like this:

4 Creating Associations

After you created the entities, you can create associations for the entities. For more information, see Associations.

To create an association, draw a line from the border of one entity to the border of the other entity. Always start with the entity that can have more instances in the system than the other one. In this case, draw an association from Order to Customer, because one customer can have multiple orders.

5 Multiplicity

This section explains how to change the multiplicity of associations.

By default, the domain model editor creates an association with a one-to-many multiplicity. In the above case, a customer can have multiple orders, and an order can only have one customer.

If you want to change the multiplicity, double-click the Order_Customer association to open its Properties dialog box:

  • To change the association to a one-to-one multiplicity, select the [1 – 1] option in the Multiplicity section; this means that a customer can only have one order and vice versa:

  • To change the association to a many-to-many multiplicity, select the [] option in the Multiplicity section; this means that a customer can have multiple orders, and an order can have multiple customers:

    When the association is set to a many-to-many multiplicity, the Navigability property is also activated. For further details, see the Navigability section in Association Properties.

6 Delete Behavior

You can configure the on delete behavior for both sides of an association.

To configure the delete behavior, double-click the Order_Customer association to open its Properties dialog box:

  • To configure cascading delete, select the Delete ‘Order’ object(s) as well option in the On delete of ‘Customer’ object section; this means that all the orders of a customer are removed if the customer is deleted:

  • To configure prevention of delete, select the Delete ‘Customer’ object only if it is not associated with ‘Order’ object(s) in the On delete of ‘Customer’ object section; this means that a customer can only be deleted if no orders refer to this customer, and the Error message is shown to the end-user who tries to delete a customer that has orders:

7 An Example of Defining a Domain Model

In the above sections, you learn the basics of how to configure a domain model. In this section, we present an example for how you can define the domain model for an online shopping app.

7.1 Defining What Data to Include

Understanding the typical process helps you define what data to include to your domain model. The workflow for new customers of the online shopping app looks the following way:

  1. A customer registers in the online shopping app and enters the following details:
    1. Full name
    2. Address
    3. Email
    4. Date of birth
  2. When registration is complete, a unique ID is assigned to the customer.
  3. The customer browses through products and the following product details are displayed:
    1. Product image
    2. Name
    3. Description
    4. Availability
    5. Price
    6. Vendor
    7. Unique product ID
  4. The customer adds products to a shopping cart.
  5. In the shopping cart, every item is presented as a separate line showing quantity and price per line. The customer checks the order, pays it, and gets a confirmation with the order details and the date the order is purchased on.

Based on the description above, you can divide your data into the following elements:

  • Customer
  • Product
  • Order

The following sections show how to represent the data you want to include in your app with entities and their attributes in the domain model and how you should set up the associations between the entities.

An example of the domain model for the online shopping app is shown below:

Domain Model online shopping app

7.2 Defining Entities

The following shows what entities you should create in your domain model for the online shopping app:

  • The customer information should be presented by the following entity:

    • Customer – general information about the customer, such as their name, address, and email address
  • The product information should be divided into the following entities:

    • Product – general information about the product, such as its name, description, and price
    • Product_Image – each product has an image, but you do not create it as an attribute. You need to create an entity that is a specialization of the System.Image entity to allow you to store images. For an example on how to use the specialization of the System.Image entity to display images on a page, see Image Uploader
  • The order information should be divided into the following entities:

    • Order – general information about the order, such as its status, order number, the name of the customer and their address
    • Order_line – items ordered, their quantity and price
    • Order_confirmation – confirmation that is sent to the customer that the order is placed

7.3 Defining Associations

The following explains how each entity is associated for the online shopping app:

  • Product_Image and Product have a one-to-one association – One product image is connected to only one product.
  • Order and Customer have a one-to-many association – An order is placed by a customer. Several orders can be connected to one customer.
  • Product and Order_Line have a one-to-many association – The Order_Line uses information about the product. One product can be associated with several order lines.
  • Order and Order_Line have a one-to-many association – One order can contain multiple items (order lines) in it.
  • Order and Order_Confirmation have a one-to-one association – One order confirmation is issued per order.

8 Read More