50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
|
using Galaeth.Core.Attributes.EntityAnnotation;
|
||
|
using Galaeth.Core.Infrastructure;
|
||
|
using Injectio.Attributes;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
|
||
|
namespace Galaeth.DAL.Infrastructure;
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
[RegisterScoped]
|
||
|
public class DataAccessLayerSetup : IDataAccessLayerSetup
|
||
|
{
|
||
|
private readonly ILogger<DataAccessLayerSetup> _logger;
|
||
|
|
||
|
/// <summary>
|
||
|
/// Initializes a new instance of the <see cref="DataAccessLayerSetup"/> class.
|
||
|
/// </summary>
|
||
|
/// <param name="logger">Instance of <see cref="ILogger"/>.</param>
|
||
|
public DataAccessLayerSetup(ILogger<DataAccessLayerSetup> logger)
|
||
|
{
|
||
|
_logger = logger;
|
||
|
}
|
||
|
|
||
|
/// <inheritdoc />
|
||
|
public Task Setup()
|
||
|
{
|
||
|
_logger.LogInformation("Setting up data access layer");
|
||
|
|
||
|
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||
|
|
||
|
SetupMappings();
|
||
|
|
||
|
return Task.CompletedTask;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Setup custom mapping on all entities decorated with <see cref="TableMappingAttribute"/>.
|
||
|
/// </summary>
|
||
|
private static void SetupMappings()
|
||
|
{
|
||
|
// Find all entities annotated with Table attribute.
|
||
|
var entityTypes = typeof(IDataAccessLayerSetup).Assembly.GetTypes()
|
||
|
.Where(type => type.GetCustomAttributes(typeof(TableMappingAttribute), true).Any());
|
||
|
|
||
|
foreach (var entity in entityTypes)
|
||
|
{
|
||
|
Dapper.SqlMapper.SetTypeMap(entity, new EntityAttributeTypeMapper(entity));
|
||
|
}
|
||
|
}
|
||
|
}
|