ConceptAs Conversion
The ConceptAs conversion feature provides automatic type conversion support for Cratis Concepts in Entity Framework Core. This feature ensures that domain concepts are properly stored and retrieved from the database while maintaining type safety and domain integrity.
What it does
The ConceptAs conversion automatically configures Entity Framework Core to handle properties that are of ConceptAs<T> type. When a concept property is encountered:
- Storage: The underlying primitive value of the concept is stored in the database
- Retrieval: The primitive value is automatically converted back to the concept instance when loaded from the database
- Comparison: Proper value comparison is configured for change tracking and querying
This seamless conversion allows you to use domain concepts in your entities without any manual configuration or boilerplate code.
Why it's important
Using ConceptAs conversion provides several key benefits:
- Domain Integrity: Maintains strong typing and domain semantics throughout your application stack
- Automatic Configuration: No need for manual value converter setup for each concept property
- Database Efficiency: Stores only the primitive value, keeping database schema clean and performant
- Type Safety: Prevents mixing of different concept types that share the same underlying primitive type
- Consistency: Ensures all concept types are handled uniformly across your application
Model Usage
Your entity models can use concepts directly without any special configuration:
using Cratis.Concepts;
public class Customer
{
public CustomerId Id { get; set; }
public CustomerName Name { get; set; }
public EmailAddress Email { get; set; }
}
public class CustomerId(Guid value) : ConceptAs<Guid>(value);
public class CustomerName(string value) : ConceptAs<string>(value);
public class EmailAddress(string value) : ConceptAs<string>(value);
The conversion will automatically:
- Store
CustomerIdas aGuidin the database - Store
CustomerNameandEmailAddressasstringvalues in the database - Convert back to the appropriate concept types when loading entities
Manual Configuration
If you're not using the BaseDbContext, you can manually apply ConceptAs conversion in your DbContext:
using Cratis.Applications.EntityFrameworkCore.Concepts;
public class StoreDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConceptAsConversion(Database);
base.OnModelCreating(modelBuilder);
}
}
Note: This is automatically configured for you when using the
BaseDbContext.
How it works
The conversion system uses reflection to:
- Identify all properties in your entities that implement
ConceptAs<T> - Create appropriate
ValueConverterinstances that convert between the concept and its underlying primitive type - Configure
ValueComparerinstances for proper change tracking and equality comparisons - Apply these converters to the Entity Framework model builder
The conversion is handled by the ConceptAsConversion.ApplyConceptAsConversion() extension method, which automatically discovers and configures all concept properties in your model.