Path: redb-route/redb.Route.Sql/Mapping/SqlRowMapperFactory.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Sql/Mapping/SqlRowMapperFactory.cs
----
using System.Collections;
using System.Data.Common;
using System.Reflection;
using redb.Route.Abstractions;
namespace redb.Route.Sql.Mapping;
///
/// Resolves the outputClass endpoint option to a .
/// The option carries a type name (assembly-qualified or plain, resolved against loaded
/// assemblies), so the mapper can only be built reflectively — this factory hides that
/// and hands back a non-generic mapping delegate plus the element type, which callers
/// need to build a correctly typed or .
///
/// Nothing is cached here: the endpoint resolves its outputClass once and keeps the mapping, through the type
/// resolver of its route context. A process-wide cache by name handed a module reloaded under the same name the
/// class of its previous version, and kept an unloaded module's type alive.
///
///
internal static class SqlRowMapperFactory
{
/// A resolved POCO mapping: the element type and a row → object mapper.
internal sealed record PocoMapping(Type ElementType, Func Map)
{
/// Creates an empty List<ElementType> to accumulate mapped rows.
public IList CreateList() => (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(ElementType))!;
}
///
/// Resolves to a mapping, or null when the option
/// is not set — in which case the caller keeps the default .
///
///
/// The type cannot be found, or has no public parameterless constructor.
///
public static PocoMapping? Resolve(string? outputClass, IBeanTypeResolver typeResolver)
{
ArgumentNullException.ThrowIfNull(typeResolver);
return string.IsNullOrWhiteSpace(outputClass) ? null : Build(outputClass, typeResolver);
}
private static PocoMapping Build(string outputClass, IBeanTypeResolver typeResolver)
{
// The context's resolver first (a host that loads modules searches the module's assemblies), then a bare
// class name matched across the loaded assemblies.
var type = typeResolver.Resolve(outputClass) ?? ResolveType(outputClass)
?? throw new InvalidOperationException(
$"outputClass '{outputClass}' could not be resolved. Use an assembly-qualified name " +
"(e.g. 'MyApp.Models.Order, MyApp') or make sure the declaring assembly is loaded.");
if (type.GetConstructor(Type.EmptyTypes) == null)
throw new InvalidOperationException(
$"outputClass '{type.FullName}' must have a public parameterless constructor.");
// A typed delegate, built once: a mapping error reaches the route as itself, not wrapped in the
// TargetInvocationException a reflective Invoke per row would add.
var map = (Func)typeof(SqlRowMapperFactory)
.GetMethod(nameof(CreateMap), BindingFlags.NonPublic | BindingFlags.Static)!
.MakeGenericMethod(type)
.Invoke(null, null)!;
return new PocoMapping(type, map);
}
private static Func CreateMap() where T : new()
{
var mapper = new PocoRowMapper();
return reader => mapper.Map(reader)!;
}
private static Type? ResolveType(string name)
{
var direct = Type.GetType(name, throwOnError: false);
if (direct != null) return direct;
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
var found = assembly.GetType(name, throwOnError: false);
if (found != null) return found;
}
// Last resort: match by short name across loaded assemblies.
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
Type[] types;
try { types = assembly.GetTypes(); }
catch (ReflectionTypeLoadException ex) { types = ex.Types.Where(t => t != null).ToArray()!; }
var found = Array.Find(types, t => t.Name.Equals(name, StringComparison.Ordinal));
if (found != null) return found;
}
return null;
}
}