astral-api/Astral.DAL/Infrastructure/EntityAttributeTypeMapper.cs
2024-12-11 20:36:30 +00:00

35 lines
1.3 KiB
C#

// <copyright file="EntityAttributeTypeMapper.cs" company="alveus.dev">
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
// </copyright>
using System.Reflection;
using Astral.Core.Attributes.EntityAnnotation;
using Dapper;
namespace Astral.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)
])
{
}
}