46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Reflection;
|
|
using Dapper;
|
|
|
|
namespace MicroORM;
|
|
|
|
public static class DapperTypeMapRegistrar
|
|
{
|
|
public static void RegisterFor(Type type)
|
|
{
|
|
SqlMapper.SetTypeMap(
|
|
type,
|
|
new CustomPropertyTypeMap(
|
|
type,
|
|
(t, columnName) =>
|
|
{
|
|
// [Column]
|
|
var prop = t.GetProperties()
|
|
.FirstOrDefault(p =>
|
|
p.GetCustomAttribute<ColumnAttribute>()?.Name
|
|
.Equals(columnName, StringComparison.OrdinalIgnoreCase) == true
|
|
);
|
|
|
|
if (prop != null)
|
|
return prop;
|
|
|
|
// Directo
|
|
prop = t.GetProperty(columnName,
|
|
BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
|
|
|
|
if (prop != null)
|
|
return prop;
|
|
|
|
// snake_case
|
|
var pascal = string.Concat(
|
|
columnName.Split('_')
|
|
.Select(s => char.ToUpperInvariant(s[0]) + s[1..])
|
|
);
|
|
|
|
return t.GetProperty(pascal,
|
|
BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
|
|
}
|
|
)
|
|
);
|
|
}
|
|
} |