使用 DataContext.GetTable() 获取“QueryProvider”;
DataContext.GetTable() 方法将返回一个类型为
System.Data.Linq.Table
的对象通过这样做,我认为我还没有向数据库发出调用来检索整个表。否则,LINQ 的效率会有些低。
因此,我所做的就是深入研究强类型 Datacontext 类(例如 dbDataContext),以获取其“Customers”属性等句柄,该属性代表 SQL Server 中的 Customers 表。
然后我可以从 GetTable() 返回的对象中获取 IQueryable,但仍然没有访问数据库。即,我的“服务层”代码将是 LINQ to Objects 而不是 Linq to Sql。
通过完成这一切,我将减少所需的存储库数量。
问题:
上述假设正确吗?
注意:
我正在尝试找出一种方法来使用接口和泛型来构建我的查询,以使其可测试以及所有这些。
因此,按照 @zowen 的回应思考:
我正在尝试实现
public interface IQueryProvider<T>
{
TResult Query<TResult>(Func<IQueryable<T>, TResult> query);
}
我知道这并不是绝对必要的,但我正在经历学习曲线并查看适合我和我的想法的架构选项。
我正在尝试做的事情:
我正在尝试为 SQL Server 而不是 MongoDb 实现以下内容:
public class MongoQueryProvider<T> : IQueryProvider<T>
{
private readonly IMongoCollection<T> collection;
public MongoQueryProvider(IMongoDatabase database)
{
this.collection = database.GetCollection<T>();
}
public TResult Query<TResult>(Func<IQueryable<T>, TResult> query)
{
return query(this.collection.Linq());
}
}
我想要的是获取 GetTable() 的句柄,然后针对该句柄编写我的服务层 Linq 代码。
我怀疑我必须编写一个包装器接口才能获得 IMongoDatabase 数据库变量的等效项。
然而,问题是上面的问题,而不是其他问题。就像我说的,我只是在这里学习。这部电影中不会伤害任何制作代码。
The DataContext.GetTable() method will return an object of type:
System.Data.Linq.Table
By doing that, I presume I haven't issued a call to the database to retrieve the entire table. Otherwise, LINQ would be somewhat inefficient.
Therefore, all I have done is drill into my strongly typed Datacontext class (eg, dbDataContext) to grab a handle on, for example, its "Customers" property, that represents the Customers table in SQL Server.
I can then get an IQueryable off the object returned by GetTable() and still not have hit the database. Ie, my 'Service Layer' code will be LINQ to Objects rather than Linq to Sql.
By doing all this, I will reduce the number of repositories that I need.
Question:
Are the above assumptions correct?
Note:
I am trying to figure out a way to build my Queries using interfaces and generics to make it testable and all that doo dah.
So, thinking along the lines of @zowen's response to:
Repository pattern: One repository class for each entity?
I am trying to implement
public interface IQueryProvider<T>
{
TResult Query<TResult>(Func<IQueryable<T>, TResult> query);
}
I know not strictly necessary, but I am going through the learning curve and looking at the architectural options that suit me and how I think.
What I'm trying to do:
I am trying to implement the following for SQL Server instead of MongoDb:
public class MongoQueryProvider<T> : IQueryProvider<T>
{
private readonly IMongoCollection<T> collection;
public MongoQueryProvider(IMongoDatabase database)
{
this.collection = database.GetCollection<T>();
}
public TResult Query<TResult>(Func<IQueryable<T>, TResult> query)
{
return query(this.collection.Linq());
}
}
What I want is to get a handle on GetTable() and then write my Service Layer Linq code against that.
I suspect I will have to write a wrapper interface to get the equivalent of the IMongoDatabase database variable.
However, the question is the one above, not this other issue. Like I say, I'm just learning here. No production code will be hurt in this movie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对你的问题的简短回答是你的假设是正确的。您不通过
DataContext
的GetTable<>()
方法访问数据库。而且,当您获得IQueryable
时,您也不会访问数据库,除非您枚举它。另外,看看这是否对您的任务有帮助:为每个对象创建通用存储库与特定存储库的优点?
The short answer to your question is that your assumptions are correct. You do not access the database through the
GetTable<>()
method of theDataContext
. And, when you get anIQueryable
you also don't access the database until you enumerate on it.Also, see if this helps you on your quest: Advantage of creating a generic repository vs. specific repository for each object?