DapperFluentMap by rodri-oliveira-dev
NuGet / site data
Details
Info
Name: DapperFluentMap
Roslyn source generators for Dapper.FluentMap that generate supported map registration and materialization helpers at build time.
Author: rodri-oliveira-dev
NuGet: https://www.nuget.org/packages/FluentMap.Generators/
You can find more details at https://github.com/rodri-oliveira-dev/Dapper-FluentMap
Source: https://github.com/rodri-oliveira-dev/Dapper-FluentMap
Author
rodri-oliveira-dev

Original Readme
FluentMap

Map your data. Keep your models clean.
English · Português (Brasil)
Getting Started · Documentation · NuGet · Examples · GitHub Releases · Contributing
FluentMap provides fluent, strongly typed mappings for applications that use Dapper. It connects .NET properties to database columns without adding persistence attributes to domain entities.
FluentMap complements Dapper; it does not replace it and is not a full ORM. SQL, connections, transactions, migrations, and entity tracking remain outside its scope.
Version status:
3.6.0is the latest published stable release. The repository is configured for3.6.1, which is the next release being prepared and is not published yet. Until publication, unversioned installation commands resolve to the latest version available on the selected package source.
Why FluentMap?
Imagine a .NET application with a Customer entity whose properties are Id and Name, while its database returns customer_id and customer_name. FluentMap keeps those physical column names in a dedicated, strongly typed map instead of coupling the entity to persistence details.
That gives you:
- cleaner domain models;
- strongly typed configuration;
- explicit, reusable mappings;
- direct integration with Dapper;
- support for reusable conventions;
- better organization of persistence code.
Quick Start
Install the core package and, for this self-contained SQLite example, the database provider:
dotnet add package Dapper.FluentMap
dotnet add package Microsoft.Data.Sqlite
Define the entity and its map, register it once during startup, then query normally with Dapper:
using Dapper;
using Dapper.FluentMap;
using Dapper.FluentMap.Mapping;
using Microsoft.Data.Sqlite;
FluentMapper.Initialize(config => config.AddMap<CustomerMap>());
FluentMapper.Validate();
using var connection = new SqliteConnection("Data Source=:memory:");
var customer = connection.QuerySingle<Customer>(
"SELECT 7 AS customer_id, 'Ada' AS customer_name;");
public sealed class Customer
{
public int Id \{ get; set; }
public string Name \{ get; set; \} = string.Empty;
}
public sealed class CustomerMap : EntityMap<Customer>
{
public CustomerMap()
{
Map(customer => customer.Id).ToColumn("customer_id");
Map(customer => customer.Name).ToColumn("customer_name");
}
}
Initialize the global configuration before concurrent queries and treat it as read-only afterward. See the Quick Start source if the planned portal is not yet deployed.
Key Features
| Feature | Scope | Learn more |
|---|---|---|
| Explicit property mapping | Core | EntityMap<T> and property mapping |
| Naming conventions | Core | Conventions |
| Immutable objects | Core | Immutable objects |
| Mapping profiles | Core | Mapping profiles |
| Runtime isolation | Core | FluentMapRuntime |
| Dependency Injection | Optional package | DI integration |
| Dommel integration | Optional package | Dommel integration |
| Source generators | Optional package | Source generators |
| Roslyn analyzers | Optional package | Roslyn analyzers |
The repository-local usage guide remains available as a fallback for detailed API examples.
Packages
| Package | Purpose |
|---|---|
Dapper.FluentMap | Core mapping, conventions, profiles, materialization, and runtime APIs. |
Dapper.FluentMap.Dommel | Optional Dommel metadata and persistence integration. |
FluentMap.DependencyInjection | Registration of immutable configuration and isolated runtimes with Microsoft DI. |
FluentMap.Analyzers | Roslyn diagnostics for statically detectable mapping problems. |
FluentMap.Generators | Generated map registration and supported materializers. |
Package IDs are distribution identities. Assemblies, namespaces, and public APIs remain under Dapper.FluentMap.* where documented.
The current compatibility contract requires Dapper [2.1.89,3.0.0). This is a minimum-version range, not an exact pin: consumers may resolve a compatible Dapper 2.x release at or above 2.1.89. See Compatibility and Migration before upgrading an application that still uses Dapper 2.1.79.
Documentation
The bilingual documentation portal is planned for https://rodri-oliveira-dev.github.io/Dapper-FluentMap/. Until its deployment is confirmed, use the linked repository-local sources as fallbacks.
| Topic | Portal source | Repository guide |
|---|---|---|
| Getting Started | Start here | Quick Start |
| Basic Mapping | Property mapping | Usage guide |
| Advanced Mapping | Advanced mapping | Usage guide |
| Integrations | Integrations | Package overview |
| Examples | Examples | SQLite example |
| Migration Guide | Migration | MIGRATION.md |
| Compatibility | Compatibility matrix | COMPATIBILITY.md |
| API Reference | API overview | Configuration reference |
Project Status
FluentMap is actively maintained. The 3.x line preserves the main historical contracts—including EntityMap<T>, FluentMapper.Initialize(...), and the Dapper type-map bridge—while newer capabilities remain opt-in. Review the compatibility documentation before upgrading.
For published versions and release history, see GitHub Releases and the changelog.
Contributing
Contributions are welcome. You can report a bug or suggest an improvement, help improve the documentation, or submit a focused pull request.
If FluentMap helps your project, consider giving the repository a GitHub star.
License
FluentMap is licensed under the MIT License.
About
Generating Dapper.FluentMap map registration and supported row materializers at build time.
How to use
Example (source csproj, source files)
- CSharp Project
- DapperFluentMapDemo.csproj
- Program.cs
- Customer.cs
- CustomerMap.cs
This is the CSharp Project that references DapperFluentMap
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper.FluentMap" Version="3.6.1" />
<PackageReference Include="FluentMap.Generators" Version="3.6.1" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.14" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of DapperFluentMap in DapperFluentMapDemo.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper.FluentMap" Version="3.6.1" />
<PackageReference Include="FluentMap.Generators" Version="3.6.1" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.14" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of DapperFluentMap in Program.cs
using Dapper.FluentMap;
using Microsoft.Data.Sqlite;
FluentMapper.Initialize(config => config.AddGeneratedMappings());
FluentMapper.Validate();
using var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
var customer = connection.QueryMappedSingle<Customer>(
"SELECT 42 AS customer_id, 'Ada Lovelace' AS customer_name;");
Console.WriteLine($"{customer.Id}: {customer.Name}");
This is the use of DapperFluentMap in Customer.cs
public sealed class Customer
{
public int Id \{ get; set; }
public string Name \{ get; set; \} = string.Empty;
}
This is the use of DapperFluentMap in CustomerMap.cs
using Dapper.FluentMap.Mapping;
public sealed class CustomerMap : EntityMap<Customer>
{
public CustomerMap()
{
Map(customer => customer.Id).ToColumn("customer_id");
Map(customer => customer.Name).ToColumn("customer_name");
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- DapperFluentMapGeneratedRegistration.g.cs
// <auto-generated/>
namespace Dapper.FluentMap
{
[global::System.CodeDom.Compiler.GeneratedCode("Dapper.FluentMap.Generators", "2.0.0")]
internal static class DapperFluentMapGeneratedRegistration
{
public static global::Dapper.FluentMap.Configuration.FluentMapConfiguration AddGeneratedMappings(
this global::Dapper.FluentMap.Configuration.FluentMapConfiguration configuration)
{
if (configuration == null)
{
throw new global::System.ArgumentNullException(nameof(configuration));
}
return configuration
.AddMap<global::CustomerMap>()
.AddGeneratedMaterializer<global::Customer>(
new global::Dapper.FluentMap.Materialization.GeneratedMaterializerColumn[]
{
global::Dapper.FluentMap.Materialization.GeneratedMaterializerColumn.Map("customer_id", "Id"),
global::Dapper.FluentMap.Materialization.GeneratedMaterializerColumn.Map("customer_name", "Name")
},
global::Dapper.FluentMap.DapperFluentMapGeneratedMaterializers.Read0)
;
}
}
[global::System.CodeDom.Compiler.GeneratedCode("Dapper.FluentMap.Generators", "2.0.0")]
internal static class DapperFluentMapGeneratedMaterializers
{
internal static global::Customer Read0(global::System.Data.IDataRecord record)
{
if (record == null)
{
throw new global::System.ArgumentNullException(nameof(record));
}
var entity = new global::Customer();
entity.Id = Read<int>(record, 0, "global::Customer", null, "Id", "customer_id", "int");
entity.Name = Read<string>(record, 1, "global::Customer", null, "Name", "customer_name", "string");
return entity;
}
private static T Read<T>(
global::System.Data.IDataRecord record,
int ordinal,
string entityTypeName,
string profileTypeName,
string memberPath,
string columnName,
string targetTypeName)
{
if (record.IsDBNull(ordinal))
{
return default(T);
}
var value = record.GetValue(ordinal);
try
{
return ConvertValue<T>(value);
}
catch (global::System.Exception exception) when (!(exception is global::Dapper.FluentMap.FluentMapConfigurationException))
{
var profileContext = profileTypeName == null
? string.Empty
: " Profile: '" + profileTypeName + "'.";
throw new global::Dapper.FluentMap.FluentMapConfigurationException(
"Generated materialization failed for entity '" + entityTypeName + "'." + profileContext +
" Member path: '" + memberPath + "'. Column: '" + columnName + "'. Provider CLR type: '" + value.GetType().FullName +
"'. Target type: '" + targetTypeName + "'. Generated materializers do not fall back to runtime reflection conversion.",
exception);
}
}
private static TTarget ReadConverted<TDatabase, TProperty, TTarget>(
global::System.Data.IDataRecord record,
int ordinal,
global::Dapper.FluentMap.Mapping.IReadPropertyConverter<TDatabase, TProperty> converter,
string entityTypeName,
string profileTypeName,
string memberPath,
string columnName,
string converterTypeName,
string converterDatabaseTypeName,
string converterPropertyTypeName,
string targetTypeName)
{
var value = record.GetValue(ordinal);
if (value == null || value == global::System.DBNull.Value)
{
return default(TTarget);
}
try
{
var converted = converter.ConvertFromDatabase(ConvertValue<TDatabase>(value));
if ((object)converted == null && (object)default(TTarget) != null)
{
throw new global::System.InvalidOperationException(
"Read converter '" + converterTypeName + "' returned null for non-nullable target type '" + targetTypeName + "'.");
}
return (TTarget)(object)converted;
}
catch (global::System.Exception exception) when (!(exception is global::Dapper.FluentMap.FluentMapConfigurationException))
{
var profileContext = profileTypeName == null
? string.Empty
: " Profile: '" + profileTypeName + "'.";
throw new global::Dapper.FluentMap.FluentMapConfigurationException(
"Read converter failed for entity '" + entityTypeName + "'." + profileContext +
" Member path: '" + memberPath + "'. Column: '" + columnName + "'. Converter: '" + converterTypeName +
"'. Source type: '" + value.GetType().FullName + "'. Converter database type: '" + converterDatabaseTypeName +
"'. Converter property type: '" + converterPropertyTypeName + "'. Target type: '" + targetTypeName +
"'. See the inner exception for the converter failure.",
exception);
}
}
private static T ConvertValue<T>(object value)
{
if (value is T typedValue)
{
return typedValue;
}
var targetType = typeof(T);
var conversionType = global::System.Nullable.GetUnderlyingType(targetType) ?? targetType;
#if NET6_0_OR_GREATER
if (conversionType == typeof(global::System.DateOnly) && value is global::System.DateTime dateTimeForDate)
{
return (T)(object)global::System.DateOnly.FromDateTime(dateTimeForDate);
}
if (conversionType == typeof(global::System.TimeOnly))
{
if (value is global::System.DateTime dateTimeForTime)
{
return (T)(object)global::System.TimeOnly.FromDateTime(dateTimeForTime);
}
if (value is global::System.TimeSpan timeSpanForTime)
{
return (T)(object)global::System.TimeOnly.FromTimeSpan(timeSpanForTime);
}
}
if (conversionType == typeof(global::System.DateTime) && value is global::System.DateOnly dateOnly)
{
return (T)(object)dateOnly.ToDateTime(default(global::System.TimeOnly));
}
if (conversionType == typeof(global::System.TimeSpan) && value is global::System.TimeOnly timeOnly)
{
return (T)(object)timeOnly.ToTimeSpan();
}
#endif
if (conversionType.IsEnum)
{
var enumValue = value is string text
? global::System.Enum.Parse(conversionType, text)
: global::System.Enum.ToObject(conversionType, value);
return (T)enumValue;
}
if (conversionType == typeof(global::System.Guid) && value is string guidText)
{
return (T)(object)new global::System.Guid(guidText);
}
return (T)global::System.Convert.ChangeType(value, conversionType, global::System.Globalization.CultureInfo.InvariantCulture);
}
}
}
Useful
Download Example (.NET C#)
Share DapperFluentMap
https://ignatandrei.github.io/RSCG_Examples/v2/docs/DapperFluentMap
Category "Mapper" has the following generators:
1 AutoDTO
2023-08-24
2 AutoGen
2024-02-22
3 DapperFluentMap
2026-11-13
4 DevSource.ObjectMapping
2026-11-16
5 DotNetBrightener.Mapper
2026-11-14
6 DynamicsMapper
2023-10-16
7 Facet
2025-08-17
8 GenericDtoSourceGenerator
2026-11-24
9 LightweightObjectMapper
2024-09-18
10 lomapper
2026-04-07
11 MagicMap
2023-10-08
12 MapItEasy
2026-09-10
13 Mappa.Generator
2026-10-03
14 mapperly
2023-04-16
15 MapTo
2023-10-05
16 Najlot.Map.SourceGenerator
2026-08-29
17 NextGenMapper
2023-08-16
18 Synthetix
2026-11-18