114 lines
3.1 KiB
C#
114 lines
3.1 KiB
C#
// <copyright file="FallbackTypeMapper.cs" company="alveus.dev">
|
|
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
|
|
// </copyright>
|
|
|
|
using System.Reflection;
|
|
using Dapper;
|
|
|
|
namespace Astral.DAL.Infrastructure;
|
|
|
|
/// <summary>
|
|
/// Fallback type mapper.
|
|
/// </summary>
|
|
public class FallbackTypeMapper : SqlMapper.ITypeMap
|
|
{
|
|
private readonly IEnumerable<SqlMapper.ITypeMap> _mappers;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="FallbackTypeMapper" /> class.
|
|
/// </summary>
|
|
/// <param name="mappers">Collection of <see cref="SqlMapper.ITypeMap" />.</param>
|
|
protected FallbackTypeMapper(IEnumerable<SqlMapper.ITypeMap> mappers)
|
|
{
|
|
_mappers = mappers;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Find constructor.
|
|
/// </summary>
|
|
/// <param name="names">Collection of names.</param>
|
|
/// <param name="types">Collection of types.</param>
|
|
/// <returns>Instance of <see cref="ConstructorInfo" />.</returns>
|
|
public ConstructorInfo FindConstructor(string[] names, Type[] types)
|
|
{
|
|
foreach (var mapper in _mappers)
|
|
{
|
|
try
|
|
{
|
|
var result = mapper.FindConstructor(names, types);
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Do nothing
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Find explicit constructor.
|
|
/// </summary>
|
|
/// <returns>Instance of <see cref="ConstructorInfo" />.</returns>
|
|
public ConstructorInfo FindExplicitConstructor()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get constructor parameter map.
|
|
/// </summary>
|
|
/// <param name="constructor">Instance of <see cref="ConstructorInfo" />.</param>
|
|
/// <param name="columnName">Column name.</param>
|
|
/// <returns>Instance of <see cref="SqlMapper.IMemberMap" />.</returns>
|
|
public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor, string columnName)
|
|
{
|
|
foreach (var mapper in _mappers)
|
|
{
|
|
try
|
|
{
|
|
var result = mapper.GetConstructorParameter(constructor, columnName);
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Do nothing.
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get member.
|
|
/// </summary>
|
|
/// <param name="columnName">Column name.</param>
|
|
/// <returns>Instance of <see cref="SqlMapper.IMemberMap" />.</returns>
|
|
public SqlMapper.IMemberMap GetMember(string columnName)
|
|
{
|
|
foreach (var mapper in _mappers)
|
|
{
|
|
try
|
|
{
|
|
var result = mapper.GetMember(columnName);
|
|
if (result != null)
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// Do nothing.
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|