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

111 lines
2.9 KiB
C#
Raw Permalink Normal View History

2024-11-17 10:31:01 +01:00
using System.Reflection;
using Dapper;
namespace Galaeth.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>
public 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 paramter 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;
}
}