- In the
.csproj
file where AltaSoft.DomainPrimitives.Generator is located, add the following item:
<PropertyGroup>
<DomainPrimitiveGenerator_GenerateEntityFrameworkCoreValueConverters>true</DomainPrimitiveGenerator_GenerateEntityFrameworkCoreValueConverters>
</PropertyGroup>
Note: Ensure EntityFrameworkCore is added in the references.
After this, ValueConverters for each DomainPrimitive will be generated.
Given a domain primitive AsciiString
:
/// <summary>
/// A domain primitive type representing an ASCII string.
/// </summary>
/// <remarks>
/// The AsciiString ensures that its value contains only ASCII characters.
/// </remarks>
public partial class AsciiString : IDomainValue<string>
{
/// <inheritdoc/>
public static PrimitiveValidationResult Validate(string value)
{
var input = value.AsSpan();
// ReSharper disable once ForCanBeConvertedToForeach
for (var i = 0; i < input.Length; i++)
{
if (!char.IsAscii(input[i]))
return "value contains non-ascii characters";
}
return PrimitiveValidationResult.Ok;
}
}
The following converter will be generated:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by 'AltaSoft DomainPrimitives Generator'.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
using AltaSoft.DomainPrimitives.XmlDataTypes;
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using AltaSoft.DomainPrimitives;
namespace AltaSoft.DomainPrimitives.XmlDataTypes.EntityFrameworkCore.Converters;
/// <summary>
/// ValueConverter for <see cref="AsciiString"/>
/// </summary>
public sealed class AsciiStringValueConverter : ValueConverter<AsciiString, string>
{
/// <summary>
/// Constructor to create AsciiStringValueConverter
/// </summary>
public AsciiStringValueConverter() : base(v => v, v => v)
{
}
}
Note: All Domain Primitives have implicit conversion to/from their primitive type. Therefore, no explicit conversion is required.
A helper extension method is also generated to add domain primitive conversions globally to ModelConfigurationBuilder
:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by 'AltaSoft DomainPrimitives Generator'.
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
#nullable enable
using AltaSoft.DomainPrimitives.XmlDataTypes;
using Microsoft.EntityFrameworkCore;
using AltaSoft.DomainPrimitives.XmlDataTypes.EntityFrameworkCore.Converters;
namespace AltaSoft.DomainPrimitives.XmlDataTypes.Converters.Extensions;
/// <summary>
/// Helper class providing methods to configure EntityFrameworkCore ValueConverters for DomainPrimitive types of AltaSoft.DomainPrimitives.XmlDataTypes
/// </summary>
public static class ModelConfigurationBuilderExt
{
/// <summary>
/// Adds EntityFrameworkCore ValueConverters for specific custom types to ensure proper mapping to EFCore ORM.
/// </summary>
/// <param name="configurationBuilder">The ModelConfigurationBuilder instance to which converters are added.</param>
public static ModelConfigurationBuilder AddDomainPrimitivePropertyConversions(this ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.Properties<AsciiString>().HaveConversion<AsciiStringValueConverter>();
configurationBuilder.Properties<GDay>().HaveConversion<GDayValueConverter>();
configurationBuilder.Properties<GMonth>().HaveConversion<GMonthValueConverter>();
configurationBuilder.Properties<GMonthDay>().HaveConversion<GMonthDayValueConverter>();
configurationBuilder.Properties<GYear>().HaveConversion<GYearValueConverter>();
configurationBuilder.Properties<GYearMonth>().HaveConversion<GYearMonthValueConverter>();
configurationBuilder.Properties<NegativeInteger>().HaveConversion<NegativeIntegerValueConverter>();
configurationBuilder.Properties<NonEmptyString>().HaveConversion<NonEmptyStringValueConverter>();
configurationBuilder.Properties<NonNegativeInteger>().HaveConversion<NonNegativeIntegerValueConverter>();
configurationBuilder.Properties<NonPositiveInteger>().HaveConversion<NonPositiveIntegerValueConverter>();
configurationBuilder.Properties<PositiveInteger>().HaveConversion<PositiveIntegerValueConverter>();
return configurationBuilder;
}
}
In this example, we demonstrate how to override the ConfigureConventions method in your DbContext class to add custom property conversions. By calling the AddDomainPrimitivePropertyConversions extension method, you can easily manage value conversions for your domain primitives.
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.AddDomainPrimitivePropertyConversions();
}
}