How to assign attribute definitions to elements easily

The LLBLGen Pro designer generates source-code and the generated classes and their properties might need additional .NET attributes defined for a given purpose, e.g. validation attributes or data contract attributes or other attributes defined by the .NET framework or your own code. Modifying generated property definitions in generated classes is risky as they get overwritten the next time the code is overwritten.

For this reason the LLBLGen Pro designer offers a way to define the .NET attribute definitions at the model level, so they can be generated into the generated code on properties and classes representing the elements the attributes are defined on. There are two levels you can define the attribute definitions: at the project level and at the element level.

Every attribute defined on the project level for a given element type (e.g. 'Entity'), is inherited by an instance of that type: e.g. the entity definition 'Customer' as an entity inherits the attributes defined for 'Entity' at the project level.

At the element level, you can decide whether the particular element indeed inherits the attribute definition (leave it as-is) or not (uncheck the Inherit checkbox). If just a few elements shouldn't get a particular attribute defined at the project level, this isn't a big problem. However if an attribute should only be assigned to an element with specific aspects (e.g. a StringValidationAttribute should only be assigned to Fields which are of type string), it gets time consuming to uncheck all these checkboxes.

LLBLGen Pro therefore allows you to define rules for each attribute defined on the project level. A rule is an expression which has to resolve to true for the element at hand if the attribute is to be defined on said element. If the rule resolves to true (or there's no rule) the element will inherit the attribute.

By default no rule is present for an attribute definition, which means it's defined on all elements of the given type. This means that for the StringValidationAttribute attribute definition on the element type Field, we can define a rule which states that the FieldType should be String. The rule only resolves to true for fields of type String so only those fields will inherit this attribute at the element level.

Editing attribute rules at the project level

To edit a rule for an attribute, in the tree on the left browse to Project Settings -> Conventions -> Entity Model and click Additional Attributes, and select the element type the attribute is defined for on the right. For the attribute to edit the rule for, click the button in the Has Rule column. This opens the rule editor which is a dynamic dialog. At first, there's only a [+] button, which allows you to add the first expression. Clicking a [+] adds a new expression part to the full expression, and which are combined using logical operators selectable in the dialog.

Clicking OK saves the rule for the attribute in the project settings. When OK is clicked to close the Project Settings Editor, the attributes are assigned immediately. This is dynamic, so changing an aspect of an element, e.g. the type of a field, will immediately have the effect that the attributes are updated.

Defining attributes using macros for generic, flexible definitions

To be able to define attributes which reflect values of the element they're defined on (e.g. the max length of a field in a validation attribute defined on the property representing the field), LLBLGen Pro uses a macro language. It's described in more detail in its own section.