4.3.2. Named Rows
Named Rows are a mechanism that allows developers to include semantically meaningful table data in code generation, bridging the gap between the database and the application code. This helps eliminate magic numbers and ensures consistent references to data objects across environments.
⚠️ Each generation queries the live database to fetch Named Rows.
Depending on the number of tables and rows involved, this can take significant time. Plan accordingly when generating code for large datasets.
Purpose
Hardcoded IDs, magic numbers, and implicit dependencies often create fragile code. Named Rows provide a structured way to expose important database records to the generator so they can be used directly in generated code. The model knows what matters, while the generator decides how to implement it.
How It Works
- Stored in the model as two properties per table:
NamedRowsNameColumn
— the column used as the “name” or key.NamedRowsColumns
— a comma-separated list of columns to include; if empty, all columns are used (*
).
- Before each generation, OrmFactory queries the live database for rows where
NameColumn
is not null or empty. - Selected rows are serialized into XML inside the
<Table>
tag as<NamedRow>
elements:
<Table Name="UserRole"> <NamedRow Id="1" Name="Administrator" Description="System admin" /> <NamedRow Id="2" Name="Editor" Description="Can edit content" /> </Table>
The NameColumn
is always included first if it isn’t listed in NamedRowsColumns
.
Typical Use Cases
Named Rows can be used in a wide variety of scenarios:
- Сonstants:
public class UserRoles { /// <summary> /// System admin /// </summary> public static readonly int AdministratorId = 1; /// <summary> /// Can edit content /// </summary> public static readonly int EditorId = 2; }
- Enums:
public class OrderStatus { public static readonly int Pending = 0; public static readonly int Processing = 1; public static readonly int Shipped = 2; public static readonly int Delivered = 3; }
- Object references:
public static int BatId = 123; public static Organization Bat = Repos.Instance.GetCache().GetOrganization(BatId);
- Specialized configuration or lookup data for certain customers, employees, or roles.
- Infrastructure metadata: for tables that lack triggers or predefined values.