---
uid: module-building.stereotypes.how-to-use-stereotypes
description: "How to create Stereotype Definitions, add properties, query values using generated extension methods, and apply them in Intent Architect designers."
---
# How to use Stereotypes

This how-to will explain how to define [Stereotypes](https://docs.intentarchitect.com/docs-md/application-development/modelling/about-stereotypes/about-stereotypes.md) that are contained in your [Modules](https://docs.intentarchitect.com/docs-md/application-development/applications-and-solutions/about-modules/about-modules.md) and how to apply them to your applications.

Stereotypes defined in Modules are useable in the following ways:

- During use of the Designers in Intent Architect: Stereotypes can be manually or automatically applied to Elements by everyday users.
- During module authoring in the IDE: Intent Architect generates "APIs" for Stereotypes for easily retrieving them and their properties for use in Templates and other aspects of module building.

## Create a Stereotype Definition

[Stereotype Definitions](https://docs.intentarchitect.com/docs-md/module-building/stereotypes/about-stereotype-definitions/about-stereotype-definitions.md) are used to creates "types" of Stereotypes which can then be applied to Element(s) (such as a Class in a Domain).

> [!NOTE]
> This how-to requires that the Domain Module's metadata is [installed](https://docs.intentarchitect.com/docs-md/module-building/tutorial-create-a-template/04-create-a-files-per-model-template/create-a-files-per-model-template.md#install-the-domain-metadata). Although this how-to uses the Domain Designer, the same principles can be applied to the use of any Designer.

Create or open a Module Builder application (such as the one created in [Create a Module and Template](https://docs.intentarchitect.com/docs-md/module-building/tutorial-create-a-template/02-create-the-module-and-a-template/create-the-module-and-a-template.md)) in Intent Architect, open the Module Builder Designer, right-click on the package in the designer and select `New Stereotype-Definition`.

In the Properties pane on the right of the screen, ensure that:

- `Name` has a value (eg: `Entity`).
- `Target Mode` is set to `Elements of Type`.
- `Targets` has `Class`.
- `Apply Mode` is set to `Manually`.
- `Icon` is specified to be a Boxed Character "E" on a Black background `E|#0`.
- `Display Icon` is should be `return true;`.

<p><video style="max-width: 100%" muted="true" loop="true" autoplay="true" src="videos/create-basic-stereotype.mp4"></video></p>

> [!NOTE]
> During the Software Factory Execution, it generated a `ClassModelExtensions.cs` which contains an "API" for being able to more easily use the Stereotype during module building, such as for Templates. Use of this will be discussed in more detail [further below](#query-stereotypes-from-templates).

## Configure Module packaging for Stereotype Definitions

Open the Module application in Intent Architect where your Stereotype Definition is located, then open the `Module Builder` Designer and click on the package.

In the Properties pane on the right of the screen:

- Check `Include in Module`.
- For `Reference in Designer` ensure that `Domain` is included.

<p><video style="max-width: 100%" muted="true" loop="true" autoplay="true" src="videos/package-stereotype.mp4"></video></p>

## Add properties to a Stereotype

Properties on a Stereotype are additional "fields" (of metadata) which can be configured on an applied Stereotype.

Open the Module application in Intent Architect containing the Stereotype Definition you created above and then open the `Module Builder` Designer.

Right-click on the `Entity` Stereotype Definition.

- Select `Add Property`.
- Set the Name to `Change Detection`.
- Select the Type `Checkbox`.

<p><video style="max-width: 100%" muted="true" loop="true" autoplay="true" src="videos/stereotype-add-property.mp4"></video></p>

## Query Stereotypes from Templates

Open the Module application in Intent Architect where your [Stereotype Definition](#add-properties-to-a-stereotype) is located, then open the `Module Builder` Designer.

Add a `C# Template` called `EntityClass` with the following properties:
- `Type` set to `File Per Model`
- `Templating Method` set to `T4 Templating`
- `Designer` set to `Domain`
- `Model Type` set to `Class`

Now execute the Software Factory, add the following content to the generated `EntityClassTemplate.tt` file:

```cs
<#@ template language="C#" inherits="CSharpTemplateBase<Intent.Modelers.Domain.Api.ClassModel>" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="Intent.Modules.Common" #>
<#@ import namespace="Intent.Modules.Common.Templates" #>
<#@ import namespace="Intent.Modules.Common.CSharp.Templates" #>
<#@ import namespace="Intent.Templates" #>
<#@ import namespace="Intent.Metadata.Models" #>
<#@ import namespace="NewModule.Api" #>

using System;
using System.Collections.Generic;

[assembly: DefaultIntentManaged(Mode.Fully)]

namespace <#= Namespace #>
{
    public class <#= ClassName #>
    {
<#  foreach(var attribute in Model.Attributes) { #>

        public <#= GetTypeName(attribute) #> <#= attribute.Name.ToPascalCase() #> { get; set; }
<#  } #>
<#  foreach(var associationEnd in Model.AssociatedClasses.Where(x => x.IsNavigable)) { #>

        public <#= GetTypeName(associationEnd) #> <#= associationEnd.Name.ToPascalCase() #> { get; set; }
<#  } #>
    }
}
```

> [!NOTE]
> Your code Solution (the project generated by the Software Factory from the Module application) should contain an `API` folder with a file called `ClassModelExtensions.cs`.
>
> It contains a Class with Extension Methods for performing queries against a Class Element with regards to the `Entity` Stereotype. The Module Builder generates these extension methods for each of thee entries specified in the `Targets` property of the Stereotype Definition.

A Class Element can be queried for the presence of the `Entity` Stereotype. This can be invoked within your Template code:

```cs
bool hasEntityStereotype = Model.HasEntity();
```

The `Change Detection` Property of the `Entity` Stereotype can also be queried by invoking it within your Template code:

```cs
bool hasChangeDetection = Model.GetEntity().ChangeDetection();
```

The example below uses these extension methods in the `EntityClass` Template to determine whether or not it should generate additional properties:

```cs
<#  if (Model.HasEntity() && Model.GetEntity().ChangeDetection()) 
    { #>
        public DateTime CreatedDate { get; set; }
        public DateTime LastUpdatedDate { get; set; }
        public string UserName { get; set; }
<#  } #>
```

> [!IMPORTANT]
> In the code example above there is an `import` directive to reference the namespace of the `ClassModelExtensions` class.
>
> In this example it would be `<#@ import namespace="NewModule.Api" #>`

After applying the example above, the complete Template should be as follows:

```cs
<#@ template language="C#" inherits="CSharpTemplateBase<Intent.Modelers.Domain.Api.ClassModel>" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="Intent.Modules.Common" #>
<#@ import namespace="Intent.Modules.Common.Templates" #>
<#@ import namespace="Intent.Modules.Common.CSharp.Templates" #>
<#@ import namespace="Intent.Templates" #>
<#@ import namespace="Intent.Metadata.Models" #>
<#@ import namespace="NewModule.Api" #>

using System;
using System.Collections.Generic;

[assembly: DefaultIntentManaged(Mode.Fully)]

namespace <#= Namespace #>
{
    public class <#= ClassName #>
    {
<#  if (Model.HasEntity() && Model.GetEntity().ChangeDetection()) 
    { #>
        public DateTime CreatedDate { get; set; }
        public DateTime LastUpdatedDate { get; set; }
        public string UserName { get; set; }
<#  } #>

<#  foreach(var attribute in Model.Attributes) { #>

        public <#= GetTypeName(attribute) #> <#= attribute.Name.ToPascalCase() #> { get; set; }
<#  } #>
<#  foreach(var associationEnd in Model.AssociatedClasses.Where(x => x.IsNavigable)) { #>

        public <#= GetTypeName(associationEnd) #> <#= associationEnd.Name.ToPascalCase() #> { get; set; }
<#  } #>
    }
}
```

## Apply a Stereotype (manually)

Create or open an `Standard ASP.NET Core Web Application` application in Intent Architect.

[Install the module](https://docs.intentarchitect.com/docs-md/module-building/tutorial-create-a-template/03-install-and-run-the-module/install-and-run-the-module.md#install-the-module) that contains the [newly created Stereotype Definition](#create-a-stereotype-definition).

Open the Domain Designer and create or select a Class Element.

Right-click on the Element and select Add Stereotype. A popup dialog appears with a list of Stereotypes Definitions whose `Targets` property contains this `Element` type.
Select your `Entity` Stereotype. Locate the Stereotype in the Properties pane on the right and check the checkbox next to "Change Detection".

<p><video style="max-width: 100%" muted="true" loop="true" autoplay="true" src="videos/apply-stereotype-manually.mp4"></video></p>

Save the Designer and run the Software Factory.

See that with the `Change Detection` checkbox selected, it has generated the additional properties on the C# class.

If you un-selected the checkbox and ran the Software Factory again, you would see that it would no longer generate those additional properties.

![The Diff when the "Change Detection" is checked](images/change-detection-diff-result.png)

## Summary

We have learnt how to create a Stereotype Definition in a module and make that Stereotype available for use in an Application as well as read the Stereotype Definition's value from within a template.
