Merge pull request #60 from stulzq/master

添加全局转换实体属性名方法
This commit is contained in:
2881099 2019-06-13 14:49:01 +08:00 committed by GitHub
commit ab658ca49e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 153 additions and 2 deletions

View File

@ -1,5 +1,9 @@
using System; using System;
using System.Data.Common; using System.Data.Common;
using System.Linq;
using System.Reflection;
using FreeSql.DataAnnotations;
using FreeSql.Internal;
namespace FreeSql { namespace FreeSql {
public class FreeSqlBuilder { public class FreeSqlBuilder {
@ -12,6 +16,7 @@ namespace FreeSql {
bool _isConfigEntityFromDbFirst = false; bool _isConfigEntityFromDbFirst = false;
bool _isNoneCommandParameter = false; bool _isNoneCommandParameter = false;
bool _isLazyLoading = false; bool _isLazyLoading = false;
StringConvertType _entityPropertyConvertType = StringConvertType.None;
Action<DbCommand> _aopCommandExecuting = null; Action<DbCommand> _aopCommandExecuting = null;
Action<DbCommand, string> _aopCommandExecuted = null; Action<DbCommand, string> _aopCommandExecuted = null;
@ -101,7 +106,20 @@ namespace FreeSql {
return this; return this;
} }
public IFreeSql Build() => Build<IFreeSql>(); /// <summary>
/// 自动转换实体属性名称 Entity Property -> Db Filed
/// <para></para>
/// *不会覆盖 [Column] 特性设置的Name
/// </summary>
/// <param name="convertType"></param>
/// <returns></returns>
public FreeSqlBuilder UseEntityPropertyNameConvert(StringConvertType convertType)
{
_entityPropertyConvertType = convertType;
return this;
}
public IFreeSql Build() => Build<IFreeSql>();
public IFreeSql<TMark> Build<TMark>() { public IFreeSql<TMark> Build<TMark>() {
if (string.IsNullOrEmpty(_masterConnectionString)) throw new Exception("参数 masterConnectionString 不可为空,请检查 UseConnectionString"); if (string.IsNullOrEmpty(_masterConnectionString)) throw new Exception("参数 masterConnectionString 不可为空,请检查 UseConnectionString");
IFreeSql<TMark> ret = null; IFreeSql<TMark> ret = null;
@ -138,7 +156,75 @@ namespace FreeSql {
var ado = ret.Ado as Internal.CommonProvider.AdoProvider; var ado = ret.Ado as Internal.CommonProvider.AdoProvider;
ado.AopCommandExecuting += _aopCommandExecuting; ado.AopCommandExecuting += _aopCommandExecuting;
ado.AopCommandExecuted += _aopCommandExecuted; ado.AopCommandExecuted += _aopCommandExecuted;
}
//添加实体属性名全局AOP转换处理
if (_entityPropertyConvertType != StringConvertType.None)
{
// 局部方法判断是否存在Column特性以及是否设置Name的值
bool CheckEntityPropertyColumnAttribute(PropertyInfo propertyInfo)
{
var attr = propertyInfo.GetCustomAttribute<ColumnAttribute>();
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; return ret;
} }
} }

View File

@ -0,0 +1,45 @@
namespace FreeSql.Internal
{
public enum StringConvertType
{
/// <summary>
/// 不进行任何处理
/// </summary>
None = 0,
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串
/// <para></para>
/// BigApple -> Big_Apple
/// </summary>
PascalCaseToUnderscore,
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写
/// <para></para>
/// BigApple -> BIG_APPLE
/// </summary>
PascalCaseToUnderscoreWithUpper,
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
/// <para></para>
/// BigApple -> big_apple
/// </summary>
PascalCaseToUnderscoreWithLower,
/// <summary>
/// 将字符串转换为大写
/// <para></para>
/// BigApple -> BIGAPPLE
/// </summary>
Upper,
/// <summary>
/// 将字符串转换为小写
/// <para></para>
/// BigApple -> bigapple
/// </summary>
Lower
}
}

View File

@ -0,0 +1,20 @@
using System.Linq;
namespace FreeSql.Internal
{
public static class StringUtils
{
/// <summary>
/// 将帕斯卡命名字符串转换为下划线分隔字符串
/// <para></para>
/// BigApple -> Big_Apple
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string PascalCaseToUnderScore(string str)
{
return string.Concat(str.Select((x, i) =>
i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
}
}
}