Nueva mejoras Y estabilidad
This commit is contained in:
46
MicroORM/DapperTypeMapRegistrar.cs
Normal file
46
MicroORM/DapperTypeMapRegistrar.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
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);
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user