-
Notifications
You must be signed in to change notification settings - Fork 0
Naming Conventions
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.
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:
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):
The Decamelized will decamelize names using underscores:
The UpperCase will capitalize names:
The LowerCase does the opposite:
The DecamelizedUpperCase combines the Decamelized and the UpperCase ones:
The DecamelizedLowerCase combines the Decamelized and the LowerCase ones:
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:
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:
As you can see all column names are prefixed with “COL_” and all table names with “TBL_”.
- Introduction
- Architect Guide
- Concepts
- Using Visual Studio
- Overview
- Creating a CodeModeler Project
- Visual Environment
- Project Hierarchy
- Design Surface
- Customizing Design Surfaces
- Ribbon Bar
- Property Grid
- Member Format Expressions
- Model Grid
- Method Editor
- View Editor
- Instance Editor and Grid
- Resources Editor
- Inferred Model Viewer
- Building
- Project Physical Layout
- Source Control Support
- Generating
- Aspect Oriented Design (AOD)
- Developer Guide
- The Business Object Model (BOM)
- CodeModeler Query Language (CMQL)
- Starting Guide - Tutorial
- Upgrade From CFE