封装EF,使用仓储模式所遇到的问题

1 whereif在仓储模式下的封装问题

直接在Repository中定义方法这样可以直接使用

 

2 不同数据库的连接查询

这个暂时没有想到(8.14更新想到的办法是把数据查询出来放到内存中,然后用join或者groupjoin来进行联合查询)

3IEnumerable和IQueryable 区别

简单来说就是对于所有IEnumerable的过滤、排序等操作,都发生在内存里,也就是说数据已经从数据库中获取到内存中,只是在内存中进行处理

所有对于IQueryable的过滤,排序等操作,只有在数据真正用到的时候才会到数据库中查询。这也是Linq的延迟加载核心所在。

IQueryable该接口会把查询表达式先缓存到表达式树Expression 中,只有当真正遍历发生的时候,才会由IQueryProvider解析表达式树,生成sql语句执行数据库查询操作。(离线集合)

IEnumable 该接口会立即返回需要的集合。(本地集合)

IQueryable接口是继承自IEnumerable的接口的.

namespace System.Linq
{
    public interface IQueryable : IEnumerable
    {
        Type ElementType { get; }
        Expression Expression { get; }
        IQueryProvider Provider { get; }
    }
}

 

4Expression<Func<T, bool>> predicate和 Func<T, bool> predicate 区别

Func<TObject, bool>是委托(delegate)

Expression<Func<TObject, bool>>是表达式

Expression编译后就会变成delegate,才能运行。比如

Expression<Func<int, bool>> ex = x=>x.id>1;

Func<int, bool> func = ex.Compile(); 

 //
    // 摘要:
    //     Represents a strongly typed lambda expression as a data structure in the form
    //     of an expression tree. This class cannot be inherited.
    //
    // 类型参数:
    //   TDelegate:
    //     The type of the delegate that the System.Linq.Expressions.Expression`1 represents.
    public sealed class Expression<TDelegate> : LambdaExpression
    {
        public TDelegate Compile();
        
        public TDelegate Compile(bool preferInterpretation);
        
        public TDelegate Compile(DebugInfoGenerator debugInfoGenerator);
       
        public Expression<TDelegate> Update(Expression body, IEnumerable<ParameterExpression> parameters);
protected internal override Expression Accept(ExpressionVisitor visitor); }

从元数据可以看出,Expression是密封类,不能被继承,里面有个Compile方法,调用这个方法才能生成delegate,

 

posted @ 2020-07-30 11:34  青兰柳  阅读(309)  评论(0编辑  收藏  举报