﻿namespace Dapper.FastCrud.Mappings
{
    /// <summary>
    /// Discovers the orm mapping for a particular entity type.
    /// </summary>
    internal class AutoGeneratedEntityMapping<TEntity>:EntityMapping<TEntity>
    {
        /// <summary>
        /// Default constructor.
        /// </summary>
        public AutoGeneratedEntityMapping()
        {
            var currentConventions = OrmConfiguration.Conventions;

            var tableName = currentConventions.GetTableName(this.EntityType);
            this.SetTableName(tableName);

            var schemaName = currentConventions.GetSchemaName(this.EntityType);
            {
                if (!string.IsNullOrEmpty(schemaName))
                {
                    this.SetSchemaName(schemaName);
                }
            }

            foreach (var propDescriptor in currentConventions.GetEntityProperties(this.EntityType))
            {
                var propMapping = this.SetPropertyInternal(propDescriptor);
                currentConventions.ConfigureEntityPropertyMapping(propMapping);
            }
        }
    }
}
