galaeth-draft/Galaeth.DAL/Infrastructure/EntityAttributeTypeMapper.cs

32 lines
1.1 KiB
C#
Raw Permalink Normal View History

2024-11-17 10:31:01 +01:00
using System.Reflection;
using Dapper;
using Galaeth.Core.Attributes.EntityAnnotation;
namespace Galaeth.DAL.Infrastructure;
/// <summary>
/// Entity attribute type mapper.
/// </summary>
public class EntityAttributeTypeMapper : FallbackTypeMapper
{
/// <summary>
/// Initializes a new instance of the <see cref="EntityAttributeTypeMapper"/> class.
/// </summary>
/// <param name="entityType">The entity type.</param>
public EntityAttributeTypeMapper(Type entityType)
: base([
new CustomPropertyTypeMap(
entityType,
(type, columnName) =>
type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault(
prop =>
prop.GetCustomAttributes(false)
.OfType<ColumnMappingAttribute>()
.Any(attr => attr.Name.Equals(columnName, StringComparison.OrdinalIgnoreCase)))),
new DefaultTypeMap(entityType)
])
{
}
}