-
-
Notifications
You must be signed in to change notification settings - Fork 0
Key Mapping
Casey edited this page May 5, 2025
·
1 revision
EntityAxis introduces IKeyMapper<TAppKey, TDbKey> to support scenarios where your domain entity identifier (e.g., string) differs from your database key type (e.g., Guid, int).
While many applications use matching key types between the domain and persistence layers, decoupling these keys provides important flexibility and long-term maintainability.
Some examples of why this is valuable:
- Your application layer can use flexible string-based identifiers (e.g.,
"ORD-1024"), which are easy to work with across APIs, logs, and UI. - Your database layer can optimize for performance using efficient key types like
Guidorintwithout leaking those concerns into your domain model. - You can evolve your database schema independently of your application, or even support multiple backing stores (e.g., a read database with
intkeys and a write database withGuids). - You can enforce business-specific ID formats in your application without impacting database key constraints.
In short: using key mapping gives you the best of both worlds — performance and stability in your infrastructure, and flexibility and clarity in your application code.
EntityAxis provides several built-in mappers:
-
IdentityKeyMapper<T>– no conversion; used when both keys are the same -
StringToGuidKeyMapper– converts betweenstringandGuid -
StringToIntKeyMapper– converts betweenstringandint
You can register one globally, or per-entity as needed.
services.AddSingleton<IKeyMapper<Guid, Guid>, IdentityKeyMapper<Guid>>();