Skip to content

Naming Conventions

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

Since SoftFluent Modeler infers a meta-model from your model, before any producer is called to generate a single line of code, a full representation of your application is in-memory. Thanks to this inference step and the resulting meta-model, developers can apply application-wide changes.

One of the possible application wide changes is to change the way all database objects are named through a naming convention. By default, a set of naming conventions are provided by CodeModeler, but you can also implement your own naming convention to fit your needs.

Built-In Naming Conventions

By default, if a persistenceName attribute isn't explicitly defined on an element, CodeModeler will deduce from entity, property and method names the database object name (table, column, constraint, stored procedure, etc.) using the configured naming convention. A naming convention is a class implementing the CodeModeler.Runtime.Utilities.INamingConvention interface.

By default, the CodeModeler.Model.Naming.BaseNamingConvention is used, nevertheless, several other naming conventions are provided out-of-the-box (all in the CodeModeler.Model.Naming namespace):

  • FormatNamingConvention

  • LowerCaseNamingConvention

  • UpperCaseNamingConvention

  • DecamelizeNamingConvention

  • DecamelizeLowerCaseNamingConvention

  • DecamelizeUpperCaseNamingConvention

Note: FormatNamingConvention derives from the BaseNamingConvention class and adds format capabilities and is used as the base class to all other out-of-the-box naming conventions.

DecamilizeNamingConvention adds “decamelizing” capabilities and is used as a base class for “decamelizing” naming conventions (DecamelizeUpperCaseNamingConvention, DecamelizeLowerCaseNamingConvention).

For example, given this model:

Built-In Naming Conventions - Picture 22

The Default naming convention will generate the following (note we can use the in-memory Inferred Model Tool to check that before anything is actually generated):

Built-In Naming Conventions - Picture 23

The Decamelized will decamelize names using underscores:

Built-In Naming Conventions - Picture 24

The UpperCase will capitalize names:

Built-In Naming Conventions - Picture 27

The LowerCase does the opposite:

Built-In Naming Conventions - Picture 28

The DecamelizedUpperCase combines the Decamelized and the UpperCase ones:

Built-In Naming Conventions - Picture 29

The DecamelizedLowerCase combines the Decamelized and the LowerCase ones:

Built-In Naming Conventions - Picture 30

Implementing a Custom Naming Convention

Like the built-in FormatNamingConvention and its derived conventions, you can implement your own custom naming convention by implementing the CodeModeler.Runtime.Utilities.INamingConvention interface or deriving from the existing conventions to extend and modify them.

Do develop a custom naming convention, all you must do is implement the INamingConvention interface:

public interface INamingConvention
{
    string GetName(INamedObject obj, IDictionary context);
    string GetShortName(IShortNamedObject obj, IDictionary context);
    string GetFullName(IFullNamedObject obj, IDictionary context);
    string GetDisplayName(IDisplayNamedObject obj, IDictionary context);
    string GetClrFullTypeName(IClrFullTypeNamedObject obj, IDictionary context);
    string GetPersistenceName(IPersistenceNamedObject obj, IDictionary context);
}

So, you must create a .NET Class Library, reference at least CodeModeler.Runtime.dll, CodeModeler.Model.dll, and CodeModeler.Producers.dll assemblies, and add a class that implements INamingConvention to the class library. Usually, you will derive from CodeModeler.Model.Naming .BaseNamingConvention instead because it already implements CodeModeler default naming logic. For example, this code:

public class MyCustomConvention : BaseNamingConvention
{
    public override string GetName(INamedObject obj, IDictionary context)
    {
        // get proposed name
        var baseName = base.GetName(obj, context);
 
        // implement custom logic
        if (obj is Column)
            return "COL_" + baseName;
 
        if (obj is Table)
            return "TBL_" + baseName;
 
        return baseName;
    }
}

Will change all prefix all columns names with “COL_” and all tables names with “TBL_”.

Once the library has been compiled as an assembly (.dll), this assembly must be copied to Visual Studio, for example in this directory (adapt to your Visual Studio version):

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\IDE\PublicAssemblies

Note: The assembly is locked by the Visual Studio process when used by a CodeModeler project. You must restart Visual Studio if you want to modify it.

To use a custom naming convention in your model, you need to specify its assembly-qualified full type name (for example “Sample.NamingConvention.MyCustomConvention, Sample.NamingConvention”) at project level, in the “Naming Convention” attribute:

Implementing a Custom Naming Convention - Picture 359

The naming convention effect can be seen without even generating or building anything. You can use the “View Inferred Model” item in the Ribbon Bar:

Implementing a Custom Naming Convention - Picture 353

As you can see all column names are prefixed with “COL_” and all table names with “TBL_”.

Clone this wiki locally