Skip to content

Good Practices

Simon Mourier edited this page Feb 19, 2020 · 1 revision

The following article is a compilation of advice from common mistakes that we see on SoftFluent CodeModeler projects.

Keep the model as platform independent as possible

A common mistake is to add platform specific code in models. Although, it definitely can happen that for some reason there are no other way than to actually write a SQL query for instance, this should be limited as much as possible since the more platform specific code there is in the model, the more your model is coupled with technology which results in less flexible applications.

Therefore, use as much platform independent concepts as possible: for instance, always prefer writing standard CMQL methods to unchecked or raw methods.

Use entities rather than entity keys in CMQL

Bad Practice:

delete(int RoleId) where Role.Id=@RoleId

Of course, this method is valid, however writing so implies that the identifier of the Role entity is of the integer type, so if you change its type, or change its key into a composite key, you’ll have to write this method over. However, using entities directly instead of their keys solves this issue:

Good Practice:

delete(Role) where Role=@Role

Both CMQL queries will produce the same final SQL statement, however the second version is way more flexible.

Avoid repeating type in methods or properties

Do not repeat declaring entity type name in method name. For instance, in the SecurityItemRights entity below, replace:

Avoid repeating type in methods or properties - Picture 288

by:

Avoid repeating type in methods or properties - Picture 290

"IS NOT NULL" is "Exists" in CMQL

There is no need to write raw SQL to test if a value exists in a method. This can be done in CMQL thanks to the Exists operator. Check-out the CMQL Operators article for more information.

"TOP" exists in CMQL

A "TOP" statement can be added to final generated stored procedures by setting the “Maximum Count” attribute on source CMQL methods. There's no need to write raw SQL to do so:

"TOP" exists in CMQL - Picture 298

You can access entity properties on Many-To-Many relations in CMQL

A common mistake is to write raw SQL to access a value contained in a Many-To-Many relation property, when in fact it can be done without any extra work in CMQL.

For instance, in the following example a User can have several Roles, and one Role can be assigned to several Users. Nevertheless, if you want to load all users with a role name containing a specific token, instead of writing a raw SQL query, you could do it in a single line CMQL query, such as:

You can access entity properties on Many-To-Many relations in CMQL - Picture 313

Clone this wiki locally