54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
// <copyright file="DataAccessLayerSetup.cs" company="alveus.dev">
|
|
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
|
|
// </copyright>
|
|
|
|
using Astral.Core.Attributes.EntityAnnotation;
|
|
using Astral.Core.Infrastructure;
|
|
using Dapper;
|
|
using Injectio.Attributes;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Astral.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");
|
|
|
|
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)
|
|
{
|
|
SqlMapper.SetTypeMap(entity, new EntityAttributeTypeMapper(entity));
|
|
}
|
|
}
|
|
}
|