Skip to main content

DapperFluentMap by rodri-oliveira-dev

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

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​

note

rodri-oliveira-dev Alt text

Original Readme​

note

FluentMap​

Dapper FluentMap — Map your data. Keep your models clean.

Map your data. Keep your models clean.

English · Português (Brasil)

CINuGetLicense: MITGitHub stars

CodeQLQuality GateCoverage

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.0 is the latest published stable release. The repository is configured for 3.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​
FeatureScopeLearn more
Explicit property mappingCoreEntityMap<T> and property mapping
Naming conventionsCoreConventions
Immutable objectsCoreImmutable objects
Mapping profilesCoreMapping profiles
Runtime isolationCoreFluentMapRuntime
Dependency InjectionOptional packageDI integration
Dommel integrationOptional packageDommel integration
Source generatorsOptional packageSource generators
Roslyn analyzersOptional packageRoslyn analyzers

The repository-local usage guide remains available as a fallback for detailed API examples.

Packages​
PackagePurpose
Dapper.FluentMapCore mapping, conventions, profiles, materialization, and runtime APIs.
Dapper.FluentMap.DommelOptional Dommel metadata and persistence integration.
FluentMap.DependencyInjectionRegistration of immutable configuration and isolated runtimes with Microsoft DI.
FluentMap.AnalyzersRoslyn diagnostics for statically detectable mapping problems.
FluentMap.GeneratorsGenerated 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.

TopicPortal sourceRepository guide
Getting StartedStart hereQuick Start
Basic MappingProperty mappingUsage guide
Advanced MappingAdvanced mappingUsage guide
IntegrationsIntegrationsPackage overview
ExamplesExamplesSQLite example
Migration GuideMigrationMIGRATION.md
CompatibilityCompatibility matrixCOMPATIBILITY.md
API ReferenceAPI overviewConfiguration 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​

note

Generating Dapper.FluentMap map registration and supported row materializers at build time.

How to use​

Example (source csproj, source files)​

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>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

// <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 Nuget GitHub Repo stars 2023-08-24

2 AutoGen Nuget GitHub Repo stars 2024-02-22

3 DapperFluentMap Nuget GitHub Repo stars 2026-11-13

4 DevSource.ObjectMapping Nuget GitHub Repo stars 2026-11-16

5 DotNetBrightener.Mapper Nuget GitHub Repo stars 2026-11-14

6 DynamicsMapper Nuget GitHub Repo stars 2023-10-16

7 Facet Nuget GitHub Repo stars 2025-08-17

8 GenericDtoSourceGenerator Nuget GitHub Repo stars 2026-11-24

9 LightweightObjectMapper Nuget GitHub Repo stars 2024-09-18

10 lomapper Nuget GitHub Repo stars 2026-04-07

11 MagicMap Nuget GitHub Repo stars 2023-10-08

12 MapItEasy Nuget GitHub Repo stars 2026-09-10

13 Mappa.Generator Nuget GitHub Repo stars 2026-10-03

14 mapperly Nuget GitHub Repo stars 2023-04-16

15 MapTo Nuget GitHub Repo stars 2023-10-05

16 Najlot.Map.SourceGenerator Nuget GitHub Repo stars 2026-08-29

17 NextGenMapper Nuget GitHub Repo stars 2023-08-16

18 Synthetix Nuget GitHub Repo stars 2026-11-18

See category​

Mapper