Implementing a Custom Naming Convention in CodeFluent Entities
We previously introduced naming conventions in CodeFluent Entities and the ones built-in in the product, and in this previous post we mentioned the fact that you could create your own naming convention.
To do so you can either derive from the INamingConvention interface and start it from scratch, or derive from an existing one to extend it.
For instance here’s a custom naming convention, deriving form the built-in BaseNamingConvention, and which names column names as Default_[TableName]_[ColumnName], and names constraints as “myx_[ConstraintType]_[TableName]_[ReferencedTableName]”:
public class MyCustomConvention: BaseNamingConvention
{
public override string GetName(INamedObject obj, IDictionary context)
{
Column column = obj as Column;
if (column != null)
{
string name = (string)context["name"];
Table table = (Table)context["table"];
if (table != null)
{
column.DefaultName = "Default_" + table.Name + "_" + name;
}
}
return base.GetName(obj, context);
}
public override string GetShortName(IShortNamedObject obj, IDictionary context)
{
Constraint constraint = obj as Constraint;
if (constraint == null)
return base.GetShortName(obj, context);
return "myx_" + constraint.ConstraintType + "_" + constraint.Table.Name + "_" + constraint.ReferencedTable.Name;
}
}
You can then use the custom naming convention either in an assembly or a XML file, and reuse it in all your models.
In fact, you’ll find a complete example of this in the product documentation: Naming Conventions.
-
April 19, 2012 at 8:46 am | #1Designing Databases with CodeFluent Entities: Names « The CodeFluent Entities Blog