- 修复 ISelect.ToList(true) 无效的 bug;

- 增加 IAop.ConfigEntity 配置实体特性,可实现使用其他 ORM 的实体特性,#36;
This commit is contained in:
28810
2019-04-24 15:09:32 +08:00
parent b16218d779
commit e2d33e943f
10 changed files with 164 additions and 36 deletions

View File

@ -9,5 +9,7 @@ namespace FreeSql.Internal.CommonProvider {
public EventHandler<AopToListEventArgs> ToList { get; set; }
public EventHandler<AopWhereEventArgs> Where { get; set; }
public EventHandler<AopParseExpressionEventArgs> ParseExpression { get; set; }
public EventHandler<AopConfigEntityEventArgs> ConfigEntity { get; set; }
public EventHandler<AopConfigEntityPropertyEventArgs> ConfigEntityProperty { get; set; }
}
}

View File

@ -288,8 +288,10 @@ namespace FreeSql.Internal.CommonProvider {
return ret;
});
}
public List<T1> ToList(bool includeNestedMembers = false) => this.ToListPrivate(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevel2());
public Task<List<T1>> ToListAsync(bool includeNestedMembers = false) => this.ToListPrivateAsync(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevel2());
public List<T1> ToList(bool includeNestedMembers = false) =>
this.ToListPrivate(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevelAll());
public Task<List<T1>> ToListAsync(bool includeNestedMembers = false) =>
this.ToListPrivateAsync(includeNestedMembers == false ? this.GetAllFieldExpressionTreeLevel2() : this.GetAllFieldExpressionTreeLevelAll());
public T1 ToOne() {
this.Limit(1);
return this.ToList().FirstOrDefault();
@ -350,7 +352,7 @@ namespace FreeSql.Internal.CommonProvider {
public Func<IFreeSql, DbDataReader, T1> Read { get; set; }
}
protected GetAllFieldExpressionTreeInfo GetAllFieldExpressionTreeLevelAll() {
return _dicGetAllFieldExpressionTree.GetOrAdd(string.Join("+", _tables.Select(a => $"{_orm.Ado.DataType}-{a.Table.DbName}-{a.Alias}-{a.Type}")), s => {
return _dicGetAllFieldExpressionTree.GetOrAdd($"*{string.Join("+", _tables.Select(a => $"{_orm.Ado.DataType}-{a.Table.DbName}-{a.Alias}-{a.Type}"))}", s => {
var type = _tables.First().Table.TypeLazy ?? _tables.First().Table.Type;
var ormExp = Expression.Parameter(typeof(IFreeSql), "orm");
var rowExp = Expression.Parameter(typeof(DbDataReader), "row");

View File

@ -58,31 +58,74 @@ namespace FreeSql.Internal {
return dicConfigEntity.TryGetValue(type, out var trytb) ? trytb : null;
}
internal TableAttribute GetEntityTableAttribute(Type type) {
var attr = type.GetCustomAttributes(typeof(TableAttribute), false).LastOrDefault() as TableAttribute;
if (dicConfigEntity.TryGetValue(type, out var trytb) == false) return attr;
TableAttribute attr = null;
if (_orm.Aop.ConfigEntity != null) {
var aope = new AopConfigEntityEventArgs(type);
_orm.Aop.ConfigEntity(_orm, aope);
attr = aope.ModifyResult;
}
if (attr == null) attr = new TableAttribute();
if (string.IsNullOrEmpty(attr.Name)) attr.Name = trytb.Name;
if (string.IsNullOrEmpty(attr.OldName)) attr.OldName = trytb.OldName;
if (string.IsNullOrEmpty(attr.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
return attr;
if (dicConfigEntity.TryGetValue(type, out var trytb)) {
if (!string.IsNullOrEmpty(trytb.Name)) attr.Name = trytb.Name;
if (!string.IsNullOrEmpty(trytb.OldName)) attr.OldName = trytb.OldName;
if (!string.IsNullOrEmpty(trytb.SelectFilter)) attr.SelectFilter = trytb.SelectFilter;
}
var attrs = type.GetCustomAttributes(typeof(TableAttribute), false);
foreach (var tryattrobj in attrs) {
var tryattr = tryattrobj as TableAttribute;
if (tryattr == null) continue;
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
if (!string.IsNullOrEmpty(tryattr.SelectFilter)) attr.SelectFilter = tryattr.SelectFilter;
}
if (!string.IsNullOrEmpty(attr.Name)) return attr;
if (!string.IsNullOrEmpty(attr.OldName)) return attr;
if (!string.IsNullOrEmpty(attr.SelectFilter)) return attr;
return null;
}
internal ColumnAttribute GetEntityColumnAttribute(Type type, PropertyInfo proto) {
var attr = proto.GetCustomAttributes(typeof(ColumnAttribute), false).LastOrDefault() as ColumnAttribute;
if (dicConfigEntity.TryGetValue(type, out var trytb) == false) return attr;
if (trytb._columns.TryGetValue(proto.Name, out var trycol) == false) return attr;
ColumnAttribute attr = null;
if (_orm.Aop.ConfigEntityProperty != null) {
var aope = new AopConfigEntityPropertyEventArgs(type, proto);
_orm.Aop.ConfigEntityProperty(_orm, aope);
attr = aope.ModifyResult;
}
if (attr == null) attr = new ColumnAttribute();
if (string.IsNullOrEmpty(attr.Name)) attr.Name = trycol.Name;
if (string.IsNullOrEmpty(attr.OldName)) attr.OldName = trycol.OldName;
if (string.IsNullOrEmpty(attr.DbType)) attr.DbType = trycol.DbType;
if (attr._IsPrimary == null) attr._IsPrimary = trycol.IsPrimary;
if (attr._IsIdentity == null) attr._IsIdentity = trycol.IsIdentity;
if (attr._IsNullable == null) attr._IsNullable = trycol.IsNullable;
if (attr._IsIgnore == null) attr._IsIgnore = trycol.IsIgnore;
if (attr._IsVersion == null) attr._IsVersion = trycol.IsVersion;
if (attr.DbDefautValue == null) attr.DbDefautValue = trycol.DbDefautValue;
return attr;
if (dicConfigEntity.TryGetValue(type, out var trytb) && trytb._columns.TryGetValue(proto.Name, out var trycol)) {
if (!string.IsNullOrEmpty(trycol.Name)) attr.Name = trycol.Name;
if (!string.IsNullOrEmpty(trycol.OldName)) attr.OldName = trycol.OldName;
if (!string.IsNullOrEmpty(trycol.DbType)) attr.DbType = trycol.DbType;
if (trycol._IsPrimary != null) attr._IsPrimary = trycol.IsPrimary;
if (trycol._IsIdentity != null) attr._IsIdentity = trycol.IsIdentity;
if (trycol._IsNullable != null) attr._IsNullable = trycol.IsNullable;
if (trycol._IsIgnore != null) attr._IsIgnore = trycol.IsIgnore;
if (trycol._IsVersion != null) attr._IsVersion = trycol.IsVersion;
if (trycol.DbDefautValue != null) attr.DbDefautValue = trycol.DbDefautValue;
}
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
foreach (var tryattrobj in attrs) {
var tryattr = tryattrobj as ColumnAttribute;
if (tryattr == null) continue;
if (!string.IsNullOrEmpty(tryattr.Name)) attr.Name = tryattr.Name;
if (!string.IsNullOrEmpty(tryattr.OldName)) attr.OldName = tryattr.OldName;
if (!string.IsNullOrEmpty(tryattr.DbType)) attr.DbType = tryattr.DbType;
if (tryattr._IsPrimary != null) attr._IsPrimary = tryattr.IsPrimary;
if (tryattr._IsIdentity != null) attr._IsIdentity = tryattr.IsIdentity;
if (tryattr._IsNullable != null) attr._IsNullable = tryattr.IsNullable;
if (tryattr._IsIgnore != null) attr._IsIgnore = tryattr.IsIgnore;
if (tryattr._IsVersion != null) attr._IsVersion = tryattr.IsVersion;
if (tryattr.DbDefautValue != null) attr.DbDefautValue = tryattr.DbDefautValue;
}
if (!string.IsNullOrEmpty(attr.Name)) return attr;
if (!string.IsNullOrEmpty(attr.OldName)) return attr;
if (!string.IsNullOrEmpty(attr.DbType)) return attr;
if (attr._IsPrimary != null) return attr;
if (attr._IsIdentity != null) return attr;
if (attr._IsNullable != null) return attr;
if (attr._IsIgnore != null) return attr;
if (attr._IsVersion != null) return attr;
if (attr.DbDefautValue != null) return attr;
return null;
}
internal string WhereObject(TableInfo table, string aliasAndDot, object dywhere) {