Skip to content

Commit ab658ca

Browse files
authored
Merge pull request #60 from stulzq/master
添加全局转换实体属性名方法
2 parents fe6d632 + d54ebf3 commit ab658ca

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

FreeSql/FreeSqlBuilder.cs

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using System;
22
using System.Data.Common;
3+
using System.Linq;
4+
using System.Reflection;
5+
using FreeSql.DataAnnotations;
6+
using FreeSql.Internal;
37

48
namespace FreeSql {
59
public class FreeSqlBuilder {
@@ -12,6 +16,7 @@ public class FreeSqlBuilder {
1216
bool _isConfigEntityFromDbFirst = false;
1317
bool _isNoneCommandParameter = false;
1418
bool _isLazyLoading = false;
19+
StringConvertType _entityPropertyConvertType = StringConvertType.None;
1520
Action<DbCommand> _aopCommandExecuting = null;
1621
Action<DbCommand, string> _aopCommandExecuted = null;
1722

@@ -101,7 +106,20 @@ public FreeSqlBuilder UseMonitorCommand(Action<DbCommand> executing, Action<DbCo
101106
return this;
102107
}
103108

104-
public IFreeSql Build() => Build<IFreeSql>();
109+
/// <summary>
110+
/// 自动转换实体属性名称 Entity Property -> Db Filed
111+
/// <para></para>
112+
/// *不会覆盖 [Column] 特性设置的Name
113+
/// </summary>
114+
/// <param name="convertType"></param>
115+
/// <returns></returns>
116+
public FreeSqlBuilder UseEntityPropertyNameConvert(StringConvertType convertType)
117+
{
118+
_entityPropertyConvertType = convertType;
119+
return this;
120+
}
121+
122+
public IFreeSql Build() => Build<IFreeSql>();
105123
public IFreeSql<TMark> Build<TMark>() {
106124
if (string.IsNullOrEmpty(_masterConnectionString)) throw new Exception("参数 masterConnectionString 不可为空,请检查 UseConnectionString");
107125
IFreeSql<TMark> ret = null;
@@ -138,7 +156,75 @@ public IFreeSql<TMark> Build<TMark>() {
138156
var ado = ret.Ado as Internal.CommonProvider.AdoProvider;
139157
ado.AopCommandExecuting += _aopCommandExecuting;
140158
ado.AopCommandExecuted += _aopCommandExecuted;
141-
}
159+
160+
//添加实体属性名全局AOP转换处理
161+
if (_entityPropertyConvertType != StringConvertType.None)
162+
{
163+
// 局部方法判断是否存在Column特性以及是否设置Name的值
164+
bool CheckEntityPropertyColumnAttribute(PropertyInfo propertyInfo)
165+
{
166+
var attr = propertyInfo.GetCustomAttribute<ColumnAttribute>();
167+
if (attr == null || string.IsNullOrEmpty(attr.Name))
168+
{
169+
return true;
170+
}
171+
172+
return false;
173+
}
174+
175+
switch (_entityPropertyConvertType)
176+
{
177+
case StringConvertType.Lower:
178+
ret.Aop.ConfigEntityProperty = (s, e) =>
179+
{
180+
if (CheckEntityPropertyColumnAttribute(e.Property))
181+
{
182+
e.ModifyResult.Name = e.Property.Name.ToLower();
183+
}
184+
};
185+
break;
186+
case StringConvertType.Upper:
187+
ret.Aop.ConfigEntityProperty = (s, e) =>
188+
{
189+
if (CheckEntityPropertyColumnAttribute(e.Property))
190+
{
191+
e.ModifyResult.Name = e.Property.Name.ToUpper();
192+
}
193+
};
194+
break;
195+
case StringConvertType.PascalCaseToUnderscore:
196+
ret.Aop.ConfigEntityProperty = (s, e) =>
197+
{
198+
if (CheckEntityPropertyColumnAttribute(e.Property))
199+
{
200+
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name);
201+
}
202+
};
203+
break;
204+
case StringConvertType.PascalCaseToUnderscoreWithLower:
205+
ret.Aop.ConfigEntityProperty = (s, e) =>
206+
{
207+
if (CheckEntityPropertyColumnAttribute(e.Property))
208+
{
209+
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToLower();
210+
}
211+
};
212+
break;
213+
case StringConvertType.PascalCaseToUnderscoreWithUpper:
214+
ret.Aop.ConfigEntityProperty = (s, e) =>
215+
{
216+
if (CheckEntityPropertyColumnAttribute(e.Property))
217+
{
218+
e.ModifyResult.Name = StringUtils.PascalCaseToUnderScore(e.Property.Name).ToUpper();
219+
}
220+
};
221+
break;
222+
default:
223+
break;
224+
}
225+
}
226+
}
227+
142228
return ret;
143229
}
144230
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
namespace FreeSql.Internal
2+
{
3+
public enum StringConvertType
4+
{
5+
/// <summary>
6+
/// 不进行任何处理
7+
/// </summary>
8+
None = 0,
9+
10+
/// <summary>
11+
/// 将帕斯卡命名字符串转换为下划线分隔字符串
12+
/// <para></para>
13+
/// BigApple -> Big_Apple
14+
/// </summary>
15+
PascalCaseToUnderscore,
16+
17+
/// <summary>
18+
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全大写
19+
/// <para></para>
20+
/// BigApple -> BIG_APPLE
21+
/// </summary>
22+
PascalCaseToUnderscoreWithUpper,
23+
24+
/// <summary>
25+
/// 将帕斯卡命名字符串转换为下划线分隔字符串,且转换为全小写
26+
/// <para></para>
27+
/// BigApple -> big_apple
28+
/// </summary>
29+
PascalCaseToUnderscoreWithLower,
30+
31+
/// <summary>
32+
/// 将字符串转换为大写
33+
/// <para></para>
34+
/// BigApple -> BIGAPPLE
35+
/// </summary>
36+
Upper,
37+
38+
/// <summary>
39+
/// 将字符串转换为小写
40+
/// <para></para>
41+
/// BigApple -> bigapple
42+
/// </summary>
43+
Lower
44+
}
45+
}

FreeSql/Internal/StringUtils.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Linq;
2+
3+
namespace FreeSql.Internal
4+
{
5+
public static class StringUtils
6+
{
7+
/// <summary>
8+
/// 将帕斯卡命名字符串转换为下划线分隔字符串
9+
/// <para></para>
10+
/// BigApple -> Big_Apple
11+
/// </summary>
12+
/// <param name="str"></param>
13+
/// <returns></returns>
14+
public static string PascalCaseToUnderScore(string str)
15+
{
16+
return string.Concat(str.Select((x, i) =>
17+
i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)