Skip to content

Commit

Permalink
- 优化 实体基类的属性位置,优先排在最前面; #164
Browse files Browse the repository at this point in the history
- 整理 实体类 Ctor 有构造函数的映射处理;#164 [wiki](https://github.com/2881099/FreeSql/wiki/%e8%bf%94%e5%9b%9e%e6%95%b0%e6%8d%ae#dto-%E6%98%A0%E5%B0%84%E6%9F%A5%E8%AF%A2)
- 优化 实体属性,支持 protected set 属性;#164
  • Loading branch information
28810 authored and 28810 committed Dec 22, 2019
1 parent 738eeb8 commit d5ed1c8
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 175 deletions.
2 changes: 1 addition & 1 deletion FreeSql.DbContext/Extensions/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static IServiceCollection AddFreeDbContext(this IServiceCollection services, Typ
DbContext ctx = null;
try
{
var ctor = dbContextType.GetConstructors().FirstOrDefault();
var ctor = dbContextType. GetConstructors().FirstOrDefault();
var ctorParams = ctor.GetParameters().Select(a => sp.GetService(a.ParameterType)).ToArray();
ctx = Activator.CreateInstance(dbContextType, ctorParams) as DbContext;
}
Expand Down
7 changes: 0 additions & 7 deletions FreeSql.DbContext/FreeSql.DbContext.xml

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

17 changes: 17 additions & 0 deletions FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,23 @@ public void AsTable()
a.Id,
a.Clicks
});

Assert.Equal(@"SELECT a.`Id` as1, a.`Clicks` as2
FROM (SELECT * from (SELECT a.`Id` Id, a.`Clicks` Clicks
FROM `tb_topic_1` a) ftb
UNION ALL
SELECT * from (SELECT a.`Id` Id, a.`Clicks` Clicks
FROM `tb_topic_2` a) ftb) a
limit 0,20", select
.AsTable((type, old) => type == typeof(Topic) ? $"({sqlsss})" : null)
.Page(1, 20)
.ToSql(a => new
{
a.Id,
a.Clicks
}));
}

public class TestInclude_OneToManyModel1
Expand Down
17 changes: 9 additions & 8 deletions FreeSql.Tests/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ public class MySqlCodeFirstTest
var sql = g.mysql.CodeFirst.GetComparisonDDLStatements<测试中文表2>();
g.mysql.CodeFirst.SyncStructure<测试中文表2>();

var item = new 测试中文表2
{
标题 = "测试标题",
创建时间 = DateTime.Now
};
var item = 测试中文表2.Create("测试标题", DateTime.Now);
Assert.Equal(1, g.mysql.Insert<测试中文表2>().AppendData(item).ExecuteAffrows());
Assert.NotEqual(Guid.Empty, item.编号);
var item2 = g.mysql.Select<测试中文表2>().Where(a => a.编号 == item.编号).First();
Expand All @@ -32,12 +28,17 @@ public class MySqlCodeFirstTest
class 测试中文表2
{
[Column(IsPrimary = true)]
public Guid 编号 { get; set; }
public Guid 编号 { get; protected set; }

public string 标题 { get; set; }
public string 标题 { get; protected set; }

[Column(ServerTime = DateTimeKind.Local)]
public DateTime 创建时间 { get; set; }
public DateTime 创建时间 { get; protected set; }

public static 测试中文表2 Create(string title, DateTime ctm)
{
return new 测试中文表2 { 标题 = title, 创建时间 = ctm };
}
}

[Fact]
Expand Down
20 changes: 17 additions & 3 deletions FreeSql/Extensions/FreeSqlGlobalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,35 @@ public static partial class FreeSqlGlobalExtensions
public static bool IsArrayOrList(this Type that) => that == null ? false : (that.IsArray || typeof(IList).IsAssignableFrom(that));
public static Type NullableTypeOrThis(this Type that) => that?.IsNullableType() == true ? that.GetGenericArguments().First() : that;
internal static string NotNullAndConcat(this string that, params object[] args) => string.IsNullOrEmpty(that) ? null : string.Concat(new object[] { that }.Concat(args));
static ConcurrentDictionary<Type, ParameterInfo[]> _dicGetDefaultValueFirstConstructorsParameters = new ConcurrentDictionary<Type, ParameterInfo[]>();
public static object CreateInstanceGetDefaultValue(this Type that)
{
if (that == null) return null;
if (that == typeof(string)) return default(string);
if (that.IsArray) return Array.CreateInstance(that, 0);
var ctorParms = _dicGetDefaultValueFirstConstructorsParameters.GetOrAdd(that, tp => tp.GetConstructors().FirstOrDefault()?.GetParameters());
var ctorParms = that.InternalGetTypeConstructor0OrFirst(false)?.GetParameters();
if (ctorParms == null || ctorParms.Any() == false) return Activator.CreateInstance(that, null);
return Activator.CreateInstance(that, ctorParms.Select(a => Activator.CreateInstance(a.ParameterType, null)).ToArray());
}
internal static NewExpression InternalNewExpression(this Type that)
{
var ctor = that.InternalGetTypeConstructor0OrFirst();
return Expression.New(ctor, ctor.GetParameters().Select(a => Expression.Constant(a.ParameterType.CreateInstanceGetDefaultValue(), a.ParameterType)));
}

static ConcurrentDictionary<Type, ConstructorInfo> _dicInternalGetTypeConstructor0OrFirst = new ConcurrentDictionary<Type, ConstructorInfo>();
internal static ConstructorInfo InternalGetTypeConstructor0OrFirst(this Type that, bool isThrow = true)
{
var ret = _dicInternalGetTypeConstructor0OrFirst.GetOrAdd(that, tp =>
tp.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new Type[0], null) ??
tp.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).FirstOrDefault());
if (ret == null && isThrow) throw new ArgumentException($"{that.FullName} 类型无方法访问构造函数");
return ret;
}

static ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>> _dicGetPropertiesDictIgnoreCase = new ConcurrentDictionary<Type, Dictionary<string, PropertyInfo>>();
public static Dictionary<string, PropertyInfo> GetPropertiesDictIgnoreCase(this Type that) => that == null ? null : _dicGetPropertiesDictIgnoreCase.GetOrAdd(that, tp =>
{
var props = that.GetProperties();
var props = that.GetProperties().GroupBy(p => p.DeclaringType).Reverse().SelectMany(p => p); //将基类的属性位置放在前面 #164
var dict = new Dictionary<string, PropertyInfo>(StringComparer.CurrentCultureIgnoreCase);
foreach (var prop in props)
{
Expand Down
Loading

0 comments on commit d5ed1c8

Please sign in to comment.