DapperLib / Dapper.Contrib

Dapper community contributions - additional extensions for Dapper
Other
268 stars 99 forks source link

Add support ColumnAttribute for different column name #130

Open denisenko93 opened 2 years ago

denisenko93 commented 2 years ago

Added Attribute [Column(string)] for when the column in the DB is named differently from the Property in the model. Fixed #129

Breaking changes:

JoeBerkley commented 1 year ago

Is it possible to get some movement on this PR? Would be an immensely helpful feature for my use case

feiazifeiazi commented 5 months ago

@denisenko93 I used your branch of the code. Thank you.

feiazifeiazi commented 5 months ago

@denisenko93 This line of code has a bug: "sqlStringBuilder.Remove(sqlStringBuilder.Length - 3, 2);". The code is intended to remove the last comma, which works correctly on the Windows platform. However, on the Linux platform, it removes extra characters, resulting in issues with the SQL statement.

feiazifeiazi commented 5 months ago

@denisenko93

Provide a feature suggestion to add not only the Attribute [Column(string)] but also a DbMetaDataNameCaseAttribute(enum DbMetaDataNameCase) class. When a class or class property has this attribute, it should convert the field to SnakeCase.

/// <summary>
/// Database metadata naming rules
/// </summary>
public enum DbMetaDataNameCase
{
/// <summary>
/// Snake case naming (snake_case)
/// For example: example_name
/// </summary>
SnakeCase,
}

 [AttributeUsage(AttributeTargets.Class| AttributeTargets.Property)]
 public class DbMetaDataNameCaseAttribute : Attribute
 {

     public DbMetaDataNameCaseAttribute(DbMetaDataNameCase dbMetaDataNameCase)
     {
         DbMetaDataNameCase = dbMetaDataNameCase;
     }

     public DbMetaDataNameCase DbMetaDataNameCase { get; set; }
 }

private static string ConvertCamelCaseToSnakeCase(PropertyInfo property)
{
    string name = property.Name;
    string resultName = Regex.Replace(name, "([a-z])([A-Z])", "$1_$2").ToLower();
    return resultName;
}