Skip to content

Commit

Permalink
- 优化 UseMappingPriority 与实体元数据逻辑;#1247
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Sep 19, 2022
1 parent 903a309 commit 0e6945c
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 44 deletions.
27 changes: 27 additions & 0 deletions Examples/aspnetcore_transaction/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:64375/",
"sslPort": 44336
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"aspnetcore_transaction": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
27 changes: 27 additions & 0 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FreeSql;
using FreeSql.DataAnnotations;
using FreeSql.Extensions;
using FreeSql.Internal;
using FreeSql.Internal.CommonProvider;
using FreeSql.Internal.Model;
using Newtonsoft.Json;
Expand Down Expand Up @@ -341,8 +342,12 @@ public class BBB
{
public int bb { get; set; }
}
[Table(Name = "AAA_attr")]
[Index("xxx1", nameof(aa))]
[Index("xxx2", nameof(aa))]
public class AAA
{
[Column(Name = "aa_attr")]
public int aa { get; set; }
}

Expand All @@ -359,6 +364,8 @@ static void Main(string[] args)
var fsql = new FreeSql.FreeSqlBuilder()
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseNameConvert(NameConvertType.ToLower)
.UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop)


.UseConnectionString(FreeSql.DataType.Sqlite, "data source=:memory:")
Expand Down Expand Up @@ -402,6 +409,26 @@ static void Main(string[] args)
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion

fsql.Aop.ConfigEntity += (_, e) =>
{
Console.WriteLine("Aop.ConfigEntity: " + e.ModifyResult.Name);
e.ModifyIndexResult.Add(new IndexAttribute("xxx2", "aa", true));
};
fsql.Aop.ConfigEntityProperty += (_, e) =>
{
Console.WriteLine("Aop.ConfigEntityProperty: " + e.ModifyResult.Name);
};
fsql.CodeFirst.ConfigEntity<AAA>(t =>
{
t.Name("AAA_fluentapi");
t.Property(a => a.aa).Name("AA_fluentapi");
});
fsql.Select<AAA>();

fsql.Select<AAA>();



var sqlskdfj = fsql.Select<object>().AsType(typeof(BBB)).ToSql(a => new CCC());


Expand Down
18 changes: 0 additions & 18 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 15 additions & 15 deletions FreeSql/FreeSqlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,36 +356,36 @@ public IFreeSql<TMark> Build<TMark>()
//添加实体属性名全局AOP转换处理
if (_nameConvertType != NameConvertType.None)
{
string PascalCaseToUnderScore(string str) => string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
//string UnderScorePascalCase(string str) => string.Join("", str.Split('_').Select(a => a.Length > 0 ? string.Concat(char.ToUpper(a[0]), a.Substring(1)) : ""));
string PascalCaseToUnderScore(string str) => string.IsNullOrWhiteSpace(str) ? str : string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
//string UnderScorePascalCase(string str) => string.IsNullOrWhiteSpace(str) ? str : string.Join("", str.Split('_').Select(a => a.Length > 0 ? string.Concat(char.ToUpper(a[0]), a.Substring(1)) : ""));

switch (_nameConvertType)
{
case NameConvertType.ToLower:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.EntityType.Name.ToLower();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToLower();
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
ret.CodeFirst.IsSyncStructureToLower = true;
break;
case NameConvertType.ToUpper:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.EntityType.Name.ToUpper();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToUpper();
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
ret.CodeFirst.IsSyncStructureToUpper = true;
break;
case NameConvertType.PascalCaseToUnderscore:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name);
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name);
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
break;
case NameConvertType.PascalCaseToUnderscoreWithLower:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name).ToLower();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToLower();
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
break;
case NameConvertType.PascalCaseToUnderscoreWithUpper:
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.EntityType.Name).ToUpper();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToUpper();
ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
break;
//case NameConvertType.UnderscoreToPascalCase:
// ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.EntityType.Name);
// ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.Property.Name);
// ret.Aop.ConfigEntity += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.ModifyResult.Name);
// ret.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = UnderScorePascalCase(e.ModifyResult.Name);
// break;
default:
break;
Expand Down Expand Up @@ -508,7 +508,7 @@ public IFreeSql<TMark> Build<TMark>()
else if (string.IsNullOrEmpty(name) == false)
e.ModifyResult.Name = name;
else if (string.IsNullOrEmpty(schema) == false)
e.ModifyResult.Name = $"{schema}.{e.EntityType.Name}";
e.ModifyResult.Name = $"{schema}.{e.ModifyResult.Name}";
}
});

Expand Down
12 changes: 6 additions & 6 deletions FreeSql/FreeSqlBuilder_Obsolete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ void EntityPropertyNameConvert(IFreeSql fsql)
//添加实体属性名全局AOP转换处理
if (_entityPropertyConvertType != StringConvertType.None)
{
string PascalCaseToUnderScore(string str) => string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));
string PascalCaseToUnderScore(string str) => string.IsNullOrWhiteSpace(str) ? str : string.Concat(str.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString()));

switch (_entityPropertyConvertType)
{
case StringConvertType.Lower:
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToLower();
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToLower();
break;
case StringConvertType.Upper:
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.Property.Name.ToUpper();
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = e.ModifyResult.Name?.ToUpper();
break;
case StringConvertType.PascalCaseToUnderscore:
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name);
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name);
break;
case StringConvertType.PascalCaseToUnderscoreWithLower:
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToLower();
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToLower();
break;
case StringConvertType.PascalCaseToUnderscoreWithUpper:
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.Property.Name).ToUpper();
fsql.Aop.ConfigEntityProperty += (_, e) => e.ModifyResult.Name = PascalCaseToUnderScore(e.ModifyResult.Name)?.ToUpper();
break;
default:
break;
Expand Down
18 changes: 13 additions & 5 deletions FreeSql/Internal/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public TableAttribute GetConfigEntity(Type type)
}

public MappingPriorityType[] _mappingPriorityTypes = new[] { MappingPriorityType.Aop, MappingPriorityType.FluentApi, MappingPriorityType.Attribute };
ConcurrentDictionary<Type, Dictionary<string, IndexAttribute>> dicAopConfigEntityIndex = new ConcurrentDictionary<Type, Dictionary<string, IndexAttribute>>();
public TableAttribute GetEntityTableAttribute(Type type)
{
var attr = new TableAttribute();
Expand All @@ -133,12 +134,21 @@ public TableAttribute GetEntityTableAttribute(Type type)
AsTable = attr.AsTable
}
};
_orm.Aop.ConfigEntityHandler(_orm, aope);
_orm.Aop.ConfigEntityHandler(_orm, aope);
var tryattr = aope.ModifyResult;
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
if (tryattr._DisableSyncStructure != null) attr._DisableSyncStructure = tryattr.DisableSyncStructure;
if (!string.IsNullOrEmpty(tryattr.AsTable)) attr.AsTable = tryattr.AsTable;

var indexs = new Dictionary<string, IndexAttribute>();
foreach (var idxattr in aope.ModifyIndexResult)
if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
{
if (indexs.ContainsKey(idxattr.Name)) indexs.Remove(idxattr.Name);
indexs.Add(idxattr.Name, new IndexAttribute(idxattr.Name, idxattr.Fields) { _IsUnique = idxattr._IsUnique });
}
dicAopConfigEntityIndex.AddOrUpdate(type, indexs, (_, old) => indexs);
}
break;
case MappingPriorityType.FluentApi:
Expand Down Expand Up @@ -357,11 +367,9 @@ public IndexAttribute[] GetEntityIndexAttribute(Type type)
switch (mp)
{
case MappingPriorityType.Aop:
if (_orm.Aop.ConfigEntityHandler != null)
if (dicAopConfigEntityIndex.TryGetValue(type, out var tryidxs))
{
var aope = new Aop.ConfigEntityEventArgs(type);
_orm.Aop.ConfigEntityHandler(_orm, aope);
foreach (var idxattr in aope.ModifyIndexResult)
foreach (var idxattr in tryidxs.Values)
if (!string.IsNullOrEmpty(idxattr.Name) && !string.IsNullOrEmpty(idxattr.Fields))
{
if (ret.ContainsKey(idxattr.Name)) ret.Remove(idxattr.Name);
Expand Down

0 comments on commit 0e6945c

Please sign in to comment.