Linq to SQL:投影、ViewModel、不可翻译查询

发布于 2024-08-04 07:23:21 字数 902 浏览 7 评论 0原文

我的应用程序必须处理大量数据,通常选择大小约为 10000 行。为了提高性能,建议仅选择所需的数据。

当我必须进行计算或任何有意义的业务时,我很乐意选择所有数据以便正确实例化我的模型,这样我就可以依赖它的功能。

当仅查看数据(通常在表中)时,这不是我想要的,我想将检索的数据量限制到绝对最小值。

到目前为止,我已经使用以下方法从我的存储库中获取数据(下面显示的是在存储库中执行所有神奇操作的方法:

private IEnumerable<TResult> GetAllProject<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate)
{
    SetReadContext();
    var query = DataContex.Table<Order>().Where(predicate).Select(selector);

    return query.ToList();
}

这样我就可以在存储库中获得匿名类型的类型定义)调用存储库的方法,我可以透明地使用那里的类型,

每个控制器都可以准确定义要传递给视图的数据,它非常有效,因为我可以直接影响列排序等,而无需处理网格控件。我不需要 DataContext 上的任何 LoadOptions,因为它是根据选择器计算出来的。

现在的问题是,我无法控制传递到我的存储库的选择器。它也可能是这样。包含不可翻译的方法调用等。

我的问题是:

  1. 到目前为止,我已经避免创建 ViewModel,因为我担心类型爆炸。我应该使用选择器来为我进行投影吗?
  2. 我是否应该编写不检查任何内容但查询执行无异常的单元测试?

My application has to deal with large amounts of data, usual select size is about 10000 rows. In order to improve performance it is recommended only to select the data needed.

When i have to do calculations or any meaningful business i am comfortable with selecting all the data in order to instantiate my model correctly so i can rely on its functionality.

When only viewing data (most often in a table) this is not what i want, i want to limit the amount of data retrieved to the absolute minimum.

So far i've used the following approach to get data from my repositories (shown below is the method that does all the magic inside the repository:

private IEnumerable<TResult> GetAllProject<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate)
{
    SetReadContext();
    var query = DataContex.Table<Order>().Where(predicate).Select(selector);

    return query.ToList();
}

That way i have the type definition for the annonymous type in the method that invokes the repository and i can transparently work with the type there.

Every controller can define exactly what data to pass to the view, it is very efficient as i can directly influence column ordering etc. without having to deal with the Grid Control in the View to do that for me. I don't need any LoadOptions on the DataContext because it figures that out based on the selector.

The problem is now, that i dont have control the selector that is passed to my Repository.It may aswell contain method invocations etc. that are not translatable.

My question is:

  1. Thus far, i have avoided to create ViewModels because i fear type explosion. What is the best way to implement them? Should i make selectors available that do the projecting for me?
  2. Should i write unit tests that do check nothing but if the query executes without exception?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2024-08-11 07:23:21

我建议您创建 ViewModel,这样您就可以使用一组已知的类,类型爆炸并不是真正的问题,因为您目前正在使用匿名类型,这可能有点难以管理。

如果(通常)每个视图都有一个 ViewModel,那么它就会变得相当干净。在某些情况下,您甚至可以共享您的 ViewModel,尽管我建议您不要这样做,因为迟早一个消费者最终会需要更多的数据/字段,而另一个消费者最终会得到一个臃肿的 ViewModel。

I would recommend you create ViewModels so you're working with a known set of classes, Type Explosion isn't really a concern, since you're currently using anonymous types anyway, which are probably a bit harder to manage.

If you (usually) have a single ViewModel per View then it becomes resonably clean. In some cases you can even share your ViewModels, although I would recommend against it since sooner or later one of the consumers ends up needing more data/fields and the other ends up with a bloated ViewModel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文