using Galaeth.Core.Attributes.EntityAnnotation;
using Galaeth.Core.Infrastructure;
using Injectio.Attributes;
using Microsoft.Extensions.Logging;
namespace Galaeth.DAL.Infrastructure;
///
[RegisterScoped]
public class DataAccessLayerSetup : IDataAccessLayerSetup
{
private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// Instance of .
public DataAccessLayerSetup(ILogger logger)
{
_logger = logger;
}
///
public Task Setup()
{
_logger.LogInformation("Setting up data access layer");
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
SetupMappings();
return Task.CompletedTask;
}
///
/// Setup custom mapping on all entities decorated with .
///
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));
}
}
}