diff --git a/FreeSql/FreeSqlBuilder.cs b/FreeSql/FreeSqlBuilder.cs index 9b2d4315..6574371e 100644 --- a/FreeSql/FreeSqlBuilder.cs +++ b/FreeSql/FreeSqlBuilder.cs @@ -1,5 +1,9 @@ using System; using System.Data.Common; +using System.Linq; +using System.Reflection; +using FreeSql.DataAnnotations; +using FreeSql.Internal; namespace FreeSql { public class FreeSqlBuilder { @@ -12,6 +16,7 @@ namespace FreeSql { bool _isConfigEntityFromDbFirst = false; bool _isNoneCommandParameter = false; bool _isLazyLoading = false; + StringConvertType _entityPropertyConvertType = StringConvertType.None; Action _aopCommandExecuting = null; Action _aopCommandExecuted = null; @@ -101,7 +106,20 @@ namespace FreeSql { return this; } - public IFreeSql Build() => Build(); + /// + /// 自动转换实体属性名称 Entity Property -> Db Filed + /// + /// *不会覆盖 [Column] 特性设置的Name + /// + /// + /// + public FreeSqlBuilder UseEntityPropertyNameConvert(StringConvertType convertType) + { + _entityPropertyConvertType = convertType; + return this; + } + + public IFreeSql Build() => Build(); public IFreeSql Build() { if (string.IsNullOrEmpty(_masterConnectionString)) throw new Exception("参数 masterConnectionString 不可为空,请检查 UseConnectionString"); IFreeSql ret = null; @@ -138,7 +156,75 @@ namespace FreeSql { var ado = ret.Ado as Internal.CommonProvider.AdoProvider; ado.AopCommandExecuting += _aopCommandExecuting; ado.AopCommandExecuted += _aopCommandExecuted; - } + + //添加实体属性名全局AOP转换处理 + if (_entityPropertyConvertType != StringConvertType.None) + { + // 局部方法判断是否存在Column特性以及是否设置Name的值 + bool CheckEntityPropertyColumnAttribute(PropertyInfo propertyInfo) + { + var attr = propertyInfo.GetCustomAttribute(); + if (attr == null || string.IsNullOrEmpty(attr.Name)) + { + return true; + } + + return false; + } + + switch (_entityPropertyConvertType) + { + case StringConvertType.Lower: + ret.Aop.ConfigEntityProperty = (s, e) => + { + if (CheckEntityPropertyColumnAttribute(e.Property)) + { + e.ModifyResult.Name = e.Property.Name.ToLower(); + } + }; + break; + case StringConvertType.Upper: + ret.Aop.ConfigEntityProperty = (s, e) => + { + if (CheckEntityPropertyColumnAttribute(e.Property)) + { + e.ModifyResult.Name = e.Property.Name.ToUpper(); + } + }; + break; + case StringConvertType.PascalCaseToUnderscore: + ret.Aop.ConfigEntityProperty = (s, e) => + { + if (CheckEntityPropertyColumnAttribute(e.Property)) + { + e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name); + } + }; + break; + case StringConvertType.PascalCaseToUnderscoreWithLower: + ret.Aop.ConfigEntityProperty = (s, e) => + { + if (CheckEntityPropertyColumnAttribute(e.Property)) + { + e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToLower(); + } + }; + break; + case StringConvertType.PascalCaseToUnderscoreWithUpper: + ret.Aop.ConfigEntityProperty = (s, e) => + { + if (CheckEntityPropertyColumnAttribute(e.Property)) + { + e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToUpper(); + } + }; + break; + default: + break; + } + } + } + return ret; } } diff --git a/FreeSql/Internal/StringConvertType.cs b/FreeSql/Internal/StringConvertType.cs new file mode 100644 index 00000000..281fe8fa --- /dev/null +++ b/FreeSql/Internal/StringConvertType.cs @@ -0,0 +1,45 @@ +namespace FreeSql.Internal +{ + public enum StringConvertType + { + /// + /// 不进行任何处理 + /// + None = 0, + + /// + /// 将帕斯卡命名字符串转换为下划线分隔字符串 + /// + /// BigApple -> Big_Apple + /// + PascalCaseToUnderscore, + + /// + /// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写 + /// + /// BigApple -> BIG_APPLE + /// + PascalCaseToUnderscoreWithUpper, + + /// + /// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写 + /// + /// BigApple -> big_apple + /// + PascalCaseToUnderscoreWithLower, + + /// + /// 将字符串转换为大写 + /// + /// BigApple -> BIGAPPLE + /// + Upper, + + /// + /// 将字符串转换为小写 + /// + /// BigApple -> bigapple + /// + Lower + } +} \ No newline at end of file diff --git a/FreeSql/Internal/StringUtils.cs b/FreeSql/Internal/StringUtils.cs new file mode 100644 index 00000000..6ec4819b --- /dev/null +++ b/FreeSql/Internal/StringUtils.cs @@ -0,0 +1,20 @@ +using System.Linq; + +namespace FreeSql.Internal +{ + public static class StringUtils + { + /// + /// 将帕斯卡命名字符串转换为下划线分隔字符串 + /// + /// BigApple -> Big_Apple + /// + /// + /// + public static string PascalCaseToUnderScore(string str) + { + return string.Concat(str.Select((x, i) => + i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())); + } + } +} \ No newline at end of file