skip to content

Metadata, Naming & Class Demands

Where mapping rules live — on the class, in a config file, a startup API, or conventions alone — plus naming defaults and what a mapped class must give up. Probed because defaults become decisions.

on this pageshow

questions

5

Where can a data-access layer's mapping rules live, and what does convention-only mapping mean?

level: juniorimportance: must knowfreq 62%

answer

  1. four homes for the same rule
  2. class, document, startup code, or nothing
  3. conventions are defaults, not absence
  4. overrides are the escape hatch
  5. per-environment rules must live outside

basics

~20 s

Mapping rules have four possible homes: on the class itself, in an external configuration document, in a startup configuration API, or nowhere — derived by convention from class and property names. Most layers mix conventions with targeted overrides.

solid answer

~50 s

Mapping metadata answers which table backs a class, which column backs each property, and which member is the key. It can be attached in four ways. **On the class**, beside the members it describes: easy to find, moved automatically by a rename, but it puts storage detail inside the domain type and fixes one mapping per class. **In an external configuration document**: the class stays free of storage detail and a different document can be selected per environment, at the cost of a rename silently breaking a reference that no compiler checks. **Through a configuration API called once at startup**: rename-safe, testable, conditional, and usable for classes you cannot edit. **By convention alone**: no declaration at all, with identifiers derived from class and property names. Conventions are rules, not their absence, so every serious setup keeps an override path for the cases a convention gets wrong.

go deeper

for a junior

Be able to list the placements: on the class, in an external document, through startup code, or derived by convention. Say one advantage and one drawback for each.

for a middle

Explain the mechanics: what a rename does to each placement, how precedence is resolved when two sources describe the same element, and why conventions still constitute a decision.

for a senior

Show judgment about which rules belong outside the class, such as a schema name that varies by environment, and how you verify the resolved mapping matches the real schema before traffic arrives.

for a principal

Frame it as a coupling choice: how much storage detail the domain types are allowed to carry, who is allowed to change a mapping without a code review, and what that costs in refactoring safety.

A mapper has to know, for every mapped class, which table stands behind it, which column stands behind each property, which member carries the key, and how links to other classes are stored. That body of knowledge is **mapping metadata**. Nothing in the mechanism forces it into one particular place, and data-access layers genuinely differ here: some read it from the class, some from a document beside the code, some from calls made while the application boots, and some derive nearly all of it and read almost nothing. ## The four placements 1. **On the class.** The rules are written as declarations sitting next to the member each one describes. The mapping is discovered by reading the class, so it travels with the code, and a rename carried out by a refactoring tool moves the declaration with the member. 2. **In an external configuration document.** A file beside the code names the class, its table, and each property-to-column pairing. The class itself contains no storage detail at all. 3. **Through a configuration API called at startup.** The application builds the mapping in code as it boots: for this class, this table; for this property, this column. Because it is ordinary code, it can branch, loop over a set of classes, or be assembled differently in a test. 4. **By convention only.** Nothing is declared. A **naming strategy** derives the table name from the class name and each column name from the property name, and simple rules decide which members are mapped at all. ## What each placement buys and costs | Placement | Strength | Weakness | |---|---|---| | On the class | Co-located with the members; survives a rename; one place to read | Storage concerns inside the domain type; one mapping per class; changing it is a code change | | External document | Class stays clean; a different document per environment; editable without touching the class | A rename leaves a dangling name nothing checks; the mapping is far from the code it describes | | Startup API | Rename-safe, conditional, testable; works for classes you cannot edit | The mapping is spread through boot code; it is only visible by running or reading that code | | Convention only | Nothing to write or keep in sync; uniform by construction | The rule is invisible in the source; a reader must know the strategy to predict any identifier | ## Conventions are rules, not their absence The common misreading of convention-driven mapping is that "no declaration" means "no decision". It means the opposite: a decision was made once, globally, and applied to everything. The identifiers still exist, the nullability still has a value, the set of mapped members is still fixed. The only difference is that none of it is written where the reader is looking, so predicting the schema requires knowing the strategy. That is a real trade. Conventions remove a large amount of repetitive declaration and make the schema uniform. They also mean that renaming a property changes a column name, and that a member added for a purely computed purpose may be mapped unless it is marked otherwise. ## Combining them Real setups rarely pick one placement and stop: - **Convention first, override the exceptions.** The strategy handles the bulk; a handful of legacy tables and reserved-word columns get an explicit name. - **Precedence has to be defined.** When both a convention and an explicit declaration apply, the explicit one wins; when a document and an on-class declaration disagree, the layer's documented order decides, and relying on that order by accident is a defect waiting to happen. - **Per-environment differences** are the classic reason to move a rule out of the class: a schema name that differs between environments belongs in something selectable at deploy time, not compiled into the type. - **Classes you do not own** — types from a shared library — can only be mapped from outside, which is what a startup API or a document is for. ## What an interviewer is listening for A candidate who can only describe declarations on the class has used one layer and generalised from it. A strong answer names more than one placement, says what each one costs, and recognises that convention-driven mapping is a default that has to be learned rather than an absence of rules. The follow-up is almost always about overrides: how you keep one legacy name from forcing you to abandon the convention everywhere else.

  • Why can a rename break an external mapping document but not an on-class declaration?
    An on-class declaration is attached to the member, so a refactoring tool moves both together. A document refers to the member by name in text; nothing type-checks that name, so the rename leaves a reference to a member that no longer exists, and the failure appears at startup or, worse, as a silently unmapped property.
  • When is a startup configuration API worth the extra indirection?
    When you must map classes you cannot edit, when the mapping differs by environment or deployment, or when many classes share one rule that is cheaper to apply in a loop than to repeat. It also keeps the mapping under test, since it is ordinary code.
  • If everything is derived by convention, how does a reader find out what the schema will look like?
    By knowing the strategy and, in practice, by inspecting what the layer actually resolves: most layers can print or export the resolved mapping, and a startup check against the real schema turns a wrong guess into a boot failure rather than a runtime surprise.

saying these in an interview costs you the question

  • Thinks mapping rules can only be declared on the class itself
  • Believes convention-based mapping means no rules are in force
  • Assumes an external document is always safer than an on-class declaration
  • Cannot explain how to map a class the team does not own
  • Says per-environment schema differences require duplicate classes
open as a page

How does a mapper turn class and property names into table and column names, and when must you override that?

level: middleimportance: must knowfreq 57%

basics

~20 s

A naming strategy derives every identifier nobody declared, turning class names into table names and property names into column names, usually with case conversion. Replace the strategy for a schema-wide rule; declare one name for a legacy exception.

open as a page

What does a mapper typically demand of a mapped class, and what does each demand cost encapsulation?

level: middleimportance: should knowfreq 54%

basics

~20 s

Most mappers need a construction path taking no arguments, members they can read and write, and classes open enough to substitute an intercepting stand-in; assigned collections are replaced by tracked wrappers. Each demand weakens an invariant the constructor would enforce.

open as a page

When a mapped class declares nothing about a column, what defaults apply, and why is that risky over time?

level: seniorimportance: should knowfreq 48%

basics

~20 s

Undeclared elements still get values: which members are mapped, nullability, text length, numeric precision, the derived identifier. The risk is not that defaults are wrong but that a later reader cannot tell an unconsidered default from a deliberate decision.

open as a page

How would you decide between mapping the domain class directly and translating to a separate persistence model at the boundary?

level: principalimportance: should knowfreq 43%

basics

~20 s

Map the domain class directly while its shape and the row's shape stay close and the mapper's demands are tolerable. Introduce a separate persistence model when the shapes genuinely diverge — accepting duplicated types and translation that must move together.

open as a page