Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否为ISelect提供OrderBy(string propertyName)的支持 #197

Closed
tky753 opened this issue Jan 31, 2020 · 7 comments
Closed

能否为ISelect提供OrderBy(string propertyName)的支持 #197

tky753 opened this issue Jan 31, 2020 · 7 comments

Comments

@tky753
Copy link
Contributor

tky753 commented Jan 31, 2020

需要为order by做一个通用封装,传入属性名称,自动生成 order by sql语句。
尝试为ISelect<>写扩展方法,但因为需要一个 属性名-->字段名 的映射,
所以需要CodeFirst.GetTableByEntity(XXX)获取TableInfo后,再获取到字段名,然后通过OrderBy(string sql, object parms = null)来处理。
但不把ICodeFirst做成静态的话,每次都要先获取TableInfo,
然后调用 OrderByProp(string propName, TableInfo tableInfo),还是有点冗余,
如果FreeSql能直接提供相应的支持,就方便多了。

@2881099
Copy link
Collaborator

2881099 commented Jan 31, 2020

这个和 OrderBy(string sql, object parms = null) 有什么区别

OrderBy(“属性”)

@tky753
Copy link
Contributor Author

tky753 commented Feb 1, 2020

能不能让封装的TableInfo被简单地获取到呢,比如让ISelect实现类 实现一个接口

interface ISelectTableInfoInfoAccessor
{
   IReadOnlyList<SelectTableInfo> SelectTbInfos {get;}
}
class Select0Provider<> : ISelectTableInfoInfoAccessor
{
   protected List<SelectTableInfo> _tables = new List<SelectTableInfo>();
    // 想要隐藏的话可以显式实现
    IReadOnlyList<SelectTableInfo> ITableInfoInfoAccessor.SelectTbInfos {get => _tables; }
}

然后就可以方便地写扩展方法了

public static ISelect<T> OrderByProps<T>(this ISelect<T> select,
            Dictionary<string, bool> sorts) where T : class
{
    var tableInfo = (select as ITableInfoInfoAccessor).SelectTbInfos[0].Table;
   ......
}

@2881099 2881099 added the enhancement New feature or request label Feb 1, 2020
@2881099
Copy link
Collaborator

2881099 commented Feb 12, 2020

IReadOnlyList 提示不支持 4.0,我们的默认要考虑 4.0 支持。

可以用下面的反射获取:

var tbs = select.GetType().GetField("_tables", 
    BindingFlags.Instance | BindingFlags.NonPublic)
    .GetValue(select) as List<SelectTableInfo>;

@2881099 2881099 removed the enhancement New feature or request label Feb 12, 2020
@tky753
Copy link
Contributor Author

tky753 commented Feb 12, 2020

好的,问一下,我看 EfCore源码里很少用internal的,基本都是用 接口的显示实现来隐藏方法和属性的。反射调用和接口的显示实现,哪个效率更高呢?

@2881099
Copy link
Collaborator

2881099 commented Feb 12, 2020

用接口的快,反射慢80倍左右。

个人不喜欢定义过多的接口,除非是常用的。人力有限,见谅。

@tky753 tky753 closed this as completed Mar 12, 2020
@hessonsu
Copy link

OrderBy(string sql, object parms = null)
如果是表的排序字段是由前端web 控制的,有注入的风险吧?这里有无过滤下呢?

@2881099
Copy link
Collaborator

2881099 commented Jun 19, 2020

OrderBy(string sql, object parms = null)
如果是表的排序字段是由前端web 控制的,有注入的风险吧?这里有无过滤下呢?

是的,简单字段排序可以判断,order by count(xx) 类似这种表达式就不好判断了。所以我选择了让使用者自己判断,判方法:fsql.CodeFisrt.GetTableByEntity(typeof(实体类)).ColumnsByCs 这个是字典类型,对应实体的属性。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants